1 | // $Id: WeNNI.cc 1725 2009-01-15 16:57:36Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2004 Jari Häkkinen |
---|
5 | Copyright (C) 2005 Peter Johansson |
---|
6 | Copyright (C) 2006 Jari Häkkinen |
---|
7 | Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson |
---|
8 | Copyright (C) 2009 Jari Häkkinen |
---|
9 | |
---|
10 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
11 | |
---|
12 | The yat library is free software; you can redistribute it and/or |
---|
13 | modify it under the terms of the GNU General Public License as |
---|
14 | published by the Free Software Foundation; either version 3 of the |
---|
15 | License, or (at your option) any later version. |
---|
16 | |
---|
17 | The yat library is distributed in the hope that it will be useful, |
---|
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
20 | General Public License for more details. |
---|
21 | |
---|
22 | You should have received a copy of the GNU General Public License |
---|
23 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
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 | #include <limits> |
---|
34 | |
---|
35 | namespace theplu { |
---|
36 | namespace yat { |
---|
37 | namespace utility { |
---|
38 | |
---|
39 | |
---|
40 | WeNNI::WeNNI(const utility::Matrix& matrix,const utility::Matrix& flag, |
---|
41 | const unsigned int neighbours) |
---|
42 | : NNI(matrix,flag,neighbours), imputed_data_raw_(matrix) |
---|
43 | { |
---|
44 | //estimate(); |
---|
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 | unsigned int WeNNI::estimate(void) |
---|
53 | { |
---|
54 | double small_number=std::numeric_limits<double>::epsilon(); |
---|
55 | for (size_t i=0; i<data_.rows(); i++) { |
---|
56 | std::vector<std::pair<size_t,double> > distance(calculate_distances(i)); |
---|
57 | std::sort(distance.begin(),distance.end(), |
---|
58 | pair_value_compare<size_t,double>()); |
---|
59 | bool row_imputed=true; |
---|
60 | for (size_t j=0; j<data_.columns(); j++) { |
---|
61 | std::vector<size_t> knn=nearest_neighbours(j,distance); |
---|
62 | double new_value=0.0; |
---|
63 | double norm=0.0; |
---|
64 | for (std::vector<size_t>::const_iterator k=knn.begin(); k!=knn.end(); |
---|
65 | ++k) { |
---|
66 | // Avoid division with zero (perfect match vectors) |
---|
67 | double d=(distance[*k].second ? distance[*k].second : small_number); |
---|
68 | double w=weight_(distance[*k].first,j)/d; |
---|
69 | if (w) { |
---|
70 | new_value += w*data_(distance[*k].first,j); |
---|
71 | norm += w; |
---|
72 | } |
---|
73 | } |
---|
74 | // No impute if no contributions from neighbours. |
---|
75 | if (norm) { |
---|
76 | imputed_data_raw_(i,j) = new_value/norm; |
---|
77 | double w=weight_(i,j); |
---|
78 | if (w) |
---|
79 | imputed_data_(i,j) = w*data_(i,j) + (1-w)*imputed_data_raw_(i,j); |
---|
80 | else |
---|
81 | imputed_data_(i,j) = imputed_data_raw_(i,j); |
---|
82 | } |
---|
83 | else |
---|
84 | row_imputed=false; |
---|
85 | } |
---|
86 | if (!row_imputed) |
---|
87 | not_imputed_.push_back(i); |
---|
88 | } |
---|
89 | return not_imputed_.size(); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | }}} // of namespace utility, yat, and theplu |
---|