1 | // $Id: WeNNI.cc 228 2005-02-01 14:06:51Z peter $ |
---|
2 | |
---|
3 | #include "WeNNI.h" |
---|
4 | |
---|
5 | #include <algorithm> |
---|
6 | #include <cmath> |
---|
7 | #include <fstream> |
---|
8 | |
---|
9 | #include "stl_utility.h" |
---|
10 | |
---|
11 | namespace theplu { |
---|
12 | namespace cpptools { |
---|
13 | |
---|
14 | |
---|
15 | WeNNI::WeNNI(const gslapi::matrix& matrix,const gslapi::matrix& flag, |
---|
16 | const u_int neighbours) |
---|
17 | : NNI(matrix,flag,neighbours) |
---|
18 | { |
---|
19 | //estimate(); |
---|
20 | } |
---|
21 | |
---|
22 | |
---|
23 | |
---|
24 | // \hat{x_{ij}}=\frac{ \sum_{k=1,N} \frac{w_{kj}*x_{kj}}{d_{ki}} } |
---|
25 | // { \sum_{k=1,N} \frac{w_{kj} }{d_{ki}} } |
---|
26 | // where N is defined in the paper cited in the NNI class definition |
---|
27 | // documentation. |
---|
28 | u_int WeNNI::estimate(void) |
---|
29 | { |
---|
30 | using namespace std; |
---|
31 | for (unsigned int i=0; i<data_.rows(); i++) { |
---|
32 | // Jari, avoid copying in next line |
---|
33 | vector<pair<u_int,double> > distance=calculate_distances(i); |
---|
34 | sort(distance.begin(),distance.end(), |
---|
35 | pair_value_compare<u_int,double>()); |
---|
36 | for (unsigned int j=0; j<data_.columns(); j++) { |
---|
37 | vector<u_int> knn=nearest_neighbours(j,distance); |
---|
38 | double new_value=0.0; |
---|
39 | double norm=0.0; |
---|
40 | for (vector<u_int>::const_iterator k=knn.begin(); k!=knn.end(); k++) { |
---|
41 | // Jari, a small number needed here, use something standardized. |
---|
42 | // Avoid division with zero (perfect match vectors) |
---|
43 | double d=(distance[*k].second ? distance[*k].second : 1e-10); |
---|
44 | new_value+=(weight_(distance[*k].first,j) * |
---|
45 | data_(distance[*k].first,j)/d); |
---|
46 | norm+=weight_(distance[*k].first,j)/d; |
---|
47 | } |
---|
48 | // No impute if no contributions from neighbours. |
---|
49 | if (norm) |
---|
50 | imputed_data_(i,j)= |
---|
51 | weight_(i,j)*data_(i,j) + (1-weight_(i,j)) * new_value/norm; |
---|
52 | else |
---|
53 | not_imputed_.push_back(i); |
---|
54 | } |
---|
55 | } |
---|
56 | return not_imputed_.size(); |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | }} // of namespace cpptools and namespace theplu |
---|