1 | // $Id: kNNI.cc 1437 2008-08-25 17:55:00Z 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 Jari Häkkinen, Peter Johansson |
---|
8 | Copyright (C) 2008 Peter Johansson |
---|
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 2 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 this program; if not, write to the Free Software |
---|
24 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
25 | 02111-1307, USA. |
---|
26 | */ |
---|
27 | |
---|
28 | #include "kNNI.h" |
---|
29 | #include "stl_utility.h" |
---|
30 | |
---|
31 | #include <algorithm> |
---|
32 | #include <cmath> |
---|
33 | #include <fstream> |
---|
34 | #include <vector> |
---|
35 | |
---|
36 | namespace theplu { |
---|
37 | namespace yat { |
---|
38 | namespace utility { |
---|
39 | |
---|
40 | kNNI::kNNI(const utility::Matrix& matrix,const utility::Matrix& flag, |
---|
41 | const unsigned int neighbours) |
---|
42 | : NNI(matrix,flag,neighbours) |
---|
43 | { |
---|
44 | for (unsigned int i=0; i<weight_.rows(); i++) |
---|
45 | for (unsigned int j=0; j<weight_.columns(); j++) |
---|
46 | if (!weight_(i,j)) { |
---|
47 | mv_rows_.push_back(i); |
---|
48 | break; |
---|
49 | } |
---|
50 | //estimate(); |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | |
---|
55 | // \hat{x_{ij}}=\frac{ \sum_{k=1,N} \frac{x_{kj}}{d_{ki}} } |
---|
56 | // { \sum_{k=1,N} \frac{1 }{d_{ki}} }, |
---|
57 | // where N is defined in the paper cited in the NNI class definition |
---|
58 | // documentation. |
---|
59 | unsigned int kNNI::estimate(void) |
---|
60 | { |
---|
61 | for (size_t i=0; i<mv_rows_.size(); i++) { |
---|
62 | std::vector<std::pair<size_t,double> > |
---|
63 | distance(calculate_distances(mv_rows_[i])); |
---|
64 | std::sort(distance.begin(),distance.end(), |
---|
65 | pair_value_compare<size_t,double>()); |
---|
66 | for (size_t j=0; j<data_.columns(); j++) |
---|
67 | if (!weight_(mv_rows_[i],j)) { |
---|
68 | std::vector<size_t> knn=nearest_neighbours(j,distance); |
---|
69 | double new_value=0.0; |
---|
70 | double norm=0.0; |
---|
71 | for (std::vector<size_t>::const_iterator k=knn.begin(); k!=knn.end(); |
---|
72 | ++k) { |
---|
73 | // Avoid division with zero (perfect match vectors) |
---|
74 | double d=(distance[*k].second ? distance[*k].second : 1e-10); |
---|
75 | new_value+=data_(distance[*k].first,j)/d; |
---|
76 | norm+=1.0/d; |
---|
77 | } |
---|
78 | // No impute if no contributions from neighbours. |
---|
79 | if (norm) |
---|
80 | imputed_data_(mv_rows_[i],j)=new_value/norm; |
---|
81 | else { |
---|
82 | not_imputed_.push_back(i); |
---|
83 | // if norm is zero for one column it is zero for all columns |
---|
84 | // having zero weight |
---|
85 | break; |
---|
86 | } |
---|
87 | } |
---|
88 | } |
---|
89 | return not_imputed_.size(); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | }}} // of namespace utility, yat, and theplu |
---|