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