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