1 | // $Id: WeNNI.cc 172 2004-09-28 09:53:45Z jari $ |
---|
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(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_{j}}=\frac{ \sum_{i,N} \frac{w_{ij}*x_i}{d_{ij}} } |
---|
25 | // { \sum_{i,N} \frac{w_{ij} }{d_{ij}} } |
---|
26 | void WeNNI::estimate(void) |
---|
27 | { |
---|
28 | using namespace std; |
---|
29 | for (unsigned int i=0; i<data_.rows(); i++) { |
---|
30 | // Jari, avoid copying in next line |
---|
31 | vector<pair<u_int,double> > distance=calculate_distances(i); |
---|
32 | sort(distance.begin(),distance.end(), |
---|
33 | pair_value_compare<u_int,double>()); |
---|
34 | for (unsigned int j=0; j<data_.columns(); j++) { |
---|
35 | vector<u_int> knn=nearest_neighbours(j,distance); |
---|
36 | double new_value=0.0; |
---|
37 | double norm=0.0; |
---|
38 | for (vector<u_int>::const_iterator k=knn.begin(); k!=knn.end(); k++) { |
---|
39 | new_value+=1.0*data_(distance[*k].first,j)/(distance[*k].second); |
---|
40 | norm+=1.0/(distance[*k].second); |
---|
41 | } |
---|
42 | // No impute if no contributions from neighbours. |
---|
43 | if (norm) |
---|
44 | imputed_data_(i,j)=weight_(i,j)*data_(i,j)+(1-weight_(i,j))*new_value/norm; |
---|
45 | } |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | }} // of namespace cpptools and namespace theplu |
---|