1 | // $Id: WeNNI.cc 797 2007-03-12 22:53:38Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2004 Jari Häkkinen |
---|
5 | Copyright (C) 2005 Jari Häkkinen, Peter Johansson |
---|
6 | Copyright (C) 2006 Jari Häkkinen |
---|
7 | |
---|
8 | This file is part of the yat library, http://lev.thep.lu.se/trac/yat |
---|
9 | |
---|
10 | The yat library is free software; you can redistribute it and/or |
---|
11 | modify it under the terms of the GNU General Public License as |
---|
12 | published by the Free Software Foundation; either version 2 of the |
---|
13 | License, or (at your option) any later version. |
---|
14 | |
---|
15 | The yat library is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License |
---|
21 | along with this program; if not, write to the Free Software |
---|
22 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
23 | 02111-1307, USA. |
---|
24 | */ |
---|
25 | |
---|
26 | #include "WeNNI.h" |
---|
27 | #include "matrix.h" |
---|
28 | #include "stl_utility.h" |
---|
29 | |
---|
30 | #include <algorithm> |
---|
31 | #include <cmath> |
---|
32 | #include <fstream> |
---|
33 | |
---|
34 | namespace theplu { |
---|
35 | namespace yat { |
---|
36 | namespace utility { |
---|
37 | |
---|
38 | |
---|
39 | WeNNI::WeNNI(const utility::matrix& matrix,const utility::matrix& flag, |
---|
40 | const u_int neighbours) |
---|
41 | : NNI(matrix,flag,neighbours), imputed_data_raw_(matrix) |
---|
42 | { |
---|
43 | //estimate(); |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | |
---|
48 | // \hat{x_{ij}}=\frac{ \sum_{k=1,N} \frac{w_{kj}*x_{kj}}{d_{ki}} } |
---|
49 | // { \sum_{k=1,N} \frac{w_{kj} }{d_{ki}} } |
---|
50 | // where N is defined in the paper cited in the NNI class definition |
---|
51 | // documentation. |
---|
52 | u_int WeNNI::estimate(void) |
---|
53 | { |
---|
54 | for (unsigned int i=0; i<data_.rows(); i++) { |
---|
55 | std::vector<std::pair<u_int,double> > distance(calculate_distances(i)); |
---|
56 | std::sort(distance.begin(),distance.end(), |
---|
57 | pair_value_compare<u_int,double>()); |
---|
58 | bool row_imputed=true; |
---|
59 | for (unsigned int j=0; j<data_.columns(); j++) { |
---|
60 | std::vector<u_int> knn=nearest_neighbours(j,distance); |
---|
61 | double new_value=0.0; |
---|
62 | double norm=0.0; |
---|
63 | for (std::vector<u_int>::const_iterator k=knn.begin(); k!=knn.end(); |
---|
64 | ++k) { |
---|
65 | // Avoid division with zero (perfect match vectors) |
---|
66 | double d=(distance[*k].second ? distance[*k].second : 1e-10); |
---|
67 | new_value+=(weight_(distance[*k].first,j) * |
---|
68 | data_(distance[*k].first,j)/d); |
---|
69 | norm+=weight_(distance[*k].first,j)/d; |
---|
70 | } |
---|
71 | // No impute if no contributions from neighbours. |
---|
72 | if (norm){ |
---|
73 | imputed_data_raw_(i,j) = new_value/norm; |
---|
74 | imputed_data_(i,j)= |
---|
75 | weight_(i,j)*data_(i,j) + (1-weight_(i,j))* imputed_data_raw_(i,j); |
---|
76 | } |
---|
77 | else |
---|
78 | row_imputed=false; |
---|
79 | } |
---|
80 | if (!row_imputed) |
---|
81 | not_imputed_.push_back(i); |
---|
82 | } |
---|
83 | return not_imputed_.size(); |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | }}} // of namespace utility, yat, and theplu |
---|