1 | // $Id: NNI.cc 1000 2007-12-23 20:09:15Z 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 yat library, http://trac.thep.lu.se/yat |
---|
9 | |
---|
10 | The yat library is free software; you can redistribute it and/or |
---|
11 | modify it under the terms of the GNU General Public License as |
---|
12 | published by the Free Software Foundation; either version 2 of the |
---|
13 | License, or (at your option) any later version. |
---|
14 | |
---|
15 | The yat library is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License |
---|
21 | along with this program; if not, write to the Free Software |
---|
22 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
23 | 02111-1307, USA. |
---|
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 u_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<u_int,double> > |
---|
52 | NNI::calculate_distances(const u_int row) const |
---|
53 | { |
---|
54 | std::vector<std::pair<u_int,double> > distance; |
---|
55 | for (size_t i=0; i<data_.rows(); i++) |
---|
56 | if (i!=row) { |
---|
57 | double contribs=0; |
---|
58 | std::pair<u_int,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<u_int> |
---|
93 | NNI::nearest_neighbours(const u_int column, |
---|
94 | const std::vector<std::pair<u_int,double> >& d) const |
---|
95 | { |
---|
96 | std::vector<u_int> index; |
---|
97 | double contribs=0; |
---|
98 | for (u_int 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 |
---|