1 | // $Id: WeNNI.cc 4207 2022-08-26 04:36:28Z peter $ |
---|
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 | Copyright (C) 2011, 2012, 2022 Peter Johansson |
---|
10 | |
---|
11 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
12 | |
---|
13 | The yat library is free software; you can redistribute it and/or |
---|
14 | modify it under the terms of the GNU General Public License as |
---|
15 | published by the Free Software Foundation; either version 3 of the |
---|
16 | License, or (at your option) any later version. |
---|
17 | |
---|
18 | The yat library is distributed in the hope that it will be useful, |
---|
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
21 | General Public License for more details. |
---|
22 | |
---|
23 | You should have received a copy of the GNU General Public License |
---|
24 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
25 | */ |
---|
26 | |
---|
27 | #include <config.h> |
---|
28 | |
---|
29 | #include "WeNNI.h" |
---|
30 | #include "Matrix.h" |
---|
31 | #include "stl_utility.h" |
---|
32 | |
---|
33 | #include <algorithm> |
---|
34 | #include <cmath> |
---|
35 | #include <fstream> |
---|
36 | #include <limits> |
---|
37 | |
---|
38 | namespace theplu { |
---|
39 | namespace yat { |
---|
40 | namespace utility { |
---|
41 | |
---|
42 | |
---|
43 | WeNNI::WeNNI(const MatrixBase& matrix, const MatrixBase& flag, |
---|
44 | const unsigned int neighbours) |
---|
45 | : NNI(matrix,flag,neighbours), imputed_data_raw_(matrix) |
---|
46 | { |
---|
47 | //estimate(); |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | // \hat{x_{ij}}=\frac{ \sum_{k=1,N} \frac{w_{kj}*x_{kj}}{d_{ki}} } |
---|
52 | // { \sum_{k=1,N} \frac{w_{kj} }{d_{ki}} } |
---|
53 | // where N is defined in the paper cited in the NNI class definition |
---|
54 | // documentation. |
---|
55 | unsigned int WeNNI::estimate(void) |
---|
56 | { |
---|
57 | double small_number=std::numeric_limits<double>::epsilon(); |
---|
58 | for (size_t i=0; i<data_.rows(); i++) { |
---|
59 | std::vector<std::pair<size_t,double> > distance(calculate_distances(i)); |
---|
60 | std::sort(distance.begin(),distance.end(), |
---|
61 | pair_value_compare<size_t,double>()); |
---|
62 | bool row_imputed=true; |
---|
63 | for (size_t j=0; j<data_.columns(); j++) { |
---|
64 | std::vector<size_t> knn=nearest_neighbours(j,distance); |
---|
65 | double new_value=0.0; |
---|
66 | double norm=0.0; |
---|
67 | for (std::vector<size_t>::const_iterator k=knn.begin(); k!=knn.end(); |
---|
68 | ++k) { |
---|
69 | // Avoid division with zero (perfect match vectors) |
---|
70 | double d=(distance[*k].second ? distance[*k].second : small_number); |
---|
71 | double w=weight_(distance[*k].first,j)/d; |
---|
72 | if (w) { |
---|
73 | new_value += w*data_(distance[*k].first,j); |
---|
74 | norm += w; |
---|
75 | } |
---|
76 | } |
---|
77 | // No impute if no contributions from neighbours. |
---|
78 | if (norm) { |
---|
79 | imputed_data_raw_(i,j) = new_value/norm; |
---|
80 | double w=weight_(i,j); |
---|
81 | if (w) |
---|
82 | imputed_data_(i,j) = w*data_(i,j) + (1-w)*imputed_data_raw_(i,j); |
---|
83 | else |
---|
84 | imputed_data_(i,j) = imputed_data_raw_(i,j); |
---|
85 | } |
---|
86 | else |
---|
87 | row_imputed=false; |
---|
88 | } |
---|
89 | if (!row_imputed) |
---|
90 | not_imputed_.push_back(i); |
---|
91 | } |
---|
92 | return not_imputed_.size(); |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | const utility::Matrix& WeNNI::imputed_data_raw(void) const |
---|
97 | { |
---|
98 | return imputed_data_raw_; |
---|
99 | } |
---|
100 | |
---|
101 | }}} // of namespace utility, yat, and theplu |
---|