1 | // $Id: NNI.cc 1487 2008-09-10 08:41: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 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 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 "NNI.h" |
---|
27 | #include "stl_utility.h" |
---|
28 | |
---|
29 | #include <algorithm> |
---|
30 | #include <cmath> |
---|
31 | #include <fstream> |
---|
32 | |
---|
33 | namespace theplu { |
---|
34 | namespace yat { |
---|
35 | namespace utility { |
---|
36 | |
---|
37 | // For a discussion and motivation for various algorithm |
---|
38 | // implementations here see the paper cited in the class definition |
---|
39 | // documentation. |
---|
40 | NNI::NNI(const utility::Matrix& matrix,const utility::Matrix& weight, |
---|
41 | const unsigned int neighbours) |
---|
42 | : data_(matrix), imputed_data_(matrix), neighbours_(neighbours), |
---|
43 | weight_(weight) |
---|
44 | { |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | // d_{ij}^2=\frac {\sum_{k=1,C} w_{ik} w_{jk} (x_{ik}-x_{jk})^2 } |
---|
49 | // {\sum_{k=l,C} w_{ik} w_{jk} } |
---|
50 | // where C is the number of columns |
---|
51 | std::vector<std::pair<size_t,double> > |
---|
52 | NNI::calculate_distances(const size_t row) const |
---|
53 | { |
---|
54 | std::vector<std::pair<size_t,double> > distance; |
---|
55 | for (size_t i=0; i<data_.rows(); i++) |
---|
56 | if (i!=row) { |
---|
57 | double contribs=0; |
---|
58 | std::pair<size_t,double> this_distance(i,0.0); |
---|
59 | for (size_t j=0; j<data_.columns(); j++) |
---|
60 | // 0 contribution for missing values |
---|
61 | if (weight_(i,j) && weight_(row,j)) { |
---|
62 | double weight_factor=weight_(i,j)*weight_(row,j); |
---|
63 | this_distance.second+=( weight_factor * |
---|
64 | (data_(i,j)-data_(row,j)) * |
---|
65 | (data_(i,j)-data_(row,j)) ); |
---|
66 | contribs+=weight_factor; |
---|
67 | } |
---|
68 | if (contribs) { // ignore lines without any contributions |
---|
69 | this_distance.second=sqrt(this_distance.second/contribs); |
---|
70 | distance.push_back(this_distance); |
---|
71 | } |
---|
72 | } |
---|
73 | return distance; |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | const utility::Matrix& NNI::imputed_data(void) const |
---|
78 | { |
---|
79 | return imputed_data_; |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | const std::vector<size_t>& NNI::not_imputed(void) const |
---|
84 | { |
---|
85 | return not_imputed_; |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | // Contributing nearest neighbours are added up to the user set |
---|
90 | // number, and neighbours are disqualified if their element (column) |
---|
91 | // weight is zero |
---|
92 | std::vector<size_t> |
---|
93 | NNI::nearest_neighbours(const size_t column, |
---|
94 | const std::vector<std::pair<size_t,double> >& d) const |
---|
95 | { |
---|
96 | std::vector<size_t> index; |
---|
97 | double contribs=0; |
---|
98 | for (size_t i=0; ((i<d.size()) && |
---|
99 | (contribs+=weight_(d[i].first,column))<=neighbours_); i++) |
---|
100 | if (weight_(d[i].first,column)) |
---|
101 | index.push_back(i); |
---|
102 | return index; |
---|
103 | } |
---|
104 | |
---|
105 | }}} // of namespace utility, yat, and theplu |
---|