145 |
05 Sep 04 |
jari |
// $Id$ |
145 |
05 Sep 04 |
jari |
2 |
|
570 |
05 Apr 06 |
jari |
3 |
/* |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2004 Jari Häkkinen |
570 |
05 Apr 06 |
jari |
Copyright (C) 2005 Peter Johansson |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2006 Jari Häkkinen |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2007 Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2008 Jari Häkkinen, Peter Johansson |
4207 |
26 Aug 22 |
peter |
Copyright (C) 2012, 2022 Peter Johansson |
570 |
05 Apr 06 |
jari |
10 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
570 |
05 Apr 06 |
jari |
12 |
|
675 |
10 Oct 06 |
jari |
The yat library is free software; you can redistribute it and/or |
675 |
10 Oct 06 |
jari |
modify it under the terms of the GNU General Public License as |
1486 |
09 Sep 08 |
jari |
published by the Free Software Foundation; either version 3 of the |
675 |
10 Oct 06 |
jari |
License, or (at your option) any later version. |
570 |
05 Apr 06 |
jari |
17 |
|
675 |
10 Oct 06 |
jari |
The yat library is distributed in the hope that it will be useful, |
675 |
10 Oct 06 |
jari |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
675 |
10 Oct 06 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
570 |
05 Apr 06 |
jari |
General Public License for more details. |
570 |
05 Apr 06 |
jari |
22 |
|
570 |
05 Apr 06 |
jari |
You should have received a copy of the GNU General Public License |
1487 |
10 Sep 08 |
jari |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
570 |
05 Apr 06 |
jari |
25 |
*/ |
570 |
05 Apr 06 |
jari |
26 |
|
2881 |
18 Nov 12 |
peter |
27 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
28 |
|
680 |
11 Oct 06 |
jari |
29 |
#include "NNI.h" |
680 |
11 Oct 06 |
jari |
30 |
#include "stl_utility.h" |
295 |
29 Apr 05 |
peter |
31 |
|
145 |
05 Sep 04 |
jari |
32 |
#include <algorithm> |
145 |
05 Sep 04 |
jari |
33 |
#include <cmath> |
145 |
05 Sep 04 |
jari |
34 |
#include <fstream> |
145 |
05 Sep 04 |
jari |
35 |
|
145 |
05 Sep 04 |
jari |
36 |
namespace theplu { |
680 |
11 Oct 06 |
jari |
37 |
namespace yat { |
301 |
30 Apr 05 |
peter |
38 |
namespace utility { |
145 |
05 Sep 04 |
jari |
39 |
|
177 |
01 Oct 04 |
jari |
// For a discussion and motivation for various algorithm |
177 |
01 Oct 04 |
jari |
// implementations here see the paper cited in the class definition |
177 |
01 Oct 04 |
jari |
// documentation. |
4125 |
14 Jan 22 |
peter |
43 |
NNI::NNI(const MatrixBase& matrix, const MatrixBase& weight, |
1271 |
09 Apr 08 |
peter |
44 |
const unsigned int neighbours) |
172 |
28 Sep 04 |
jari |
45 |
: data_(matrix), imputed_data_(matrix), neighbours_(neighbours), |
172 |
28 Sep 04 |
jari |
46 |
weight_(weight) |
145 |
05 Sep 04 |
jari |
47 |
{ |
145 |
05 Sep 04 |
jari |
48 |
} |
145 |
05 Sep 04 |
jari |
49 |
|
145 |
05 Sep 04 |
jari |
50 |
|
177 |
01 Oct 04 |
jari |
// d_{ij}^2=\frac {\sum_{k=1,C} w_{ik} w_{jk} (x_{ik}-x_{jk})^2 } |
177 |
01 Oct 04 |
jari |
// {\sum_{k=l,C} w_{ik} w_{jk} } |
177 |
01 Oct 04 |
jari |
// where C is the number of columns |
1271 |
09 Apr 08 |
peter |
54 |
std::vector<std::pair<size_t,double> > |
1271 |
09 Apr 08 |
peter |
55 |
NNI::calculate_distances(const size_t row) const |
145 |
05 Sep 04 |
jari |
56 |
{ |
1271 |
09 Apr 08 |
peter |
57 |
std::vector<std::pair<size_t,double> > distance; |
173 |
28 Sep 04 |
jari |
58 |
for (size_t i=0; i<data_.rows(); i++) |
145 |
05 Sep 04 |
jari |
59 |
if (i!=row) { |
145 |
05 Sep 04 |
jari |
60 |
double contribs=0; |
1271 |
09 Apr 08 |
peter |
61 |
std::pair<size_t,double> this_distance(i,0.0); |
173 |
28 Sep 04 |
jari |
62 |
for (size_t j=0; j<data_.columns(); j++) |
145 |
05 Sep 04 |
jari |
// 0 contribution for missing values |
145 |
05 Sep 04 |
jari |
64 |
if (weight_(i,j) && weight_(row,j)) { |
145 |
05 Sep 04 |
jari |
65 |
double weight_factor=weight_(i,j)*weight_(row,j); |
145 |
05 Sep 04 |
jari |
66 |
this_distance.second+=( weight_factor * |
145 |
05 Sep 04 |
jari |
67 |
(data_(i,j)-data_(row,j)) * |
145 |
05 Sep 04 |
jari |
68 |
(data_(i,j)-data_(row,j)) ); |
145 |
05 Sep 04 |
jari |
69 |
contribs+=weight_factor; |
145 |
05 Sep 04 |
jari |
70 |
} |
145 |
05 Sep 04 |
jari |
71 |
if (contribs) { // ignore lines without any contributions |
145 |
05 Sep 04 |
jari |
72 |
this_distance.second=sqrt(this_distance.second/contribs); |
145 |
05 Sep 04 |
jari |
73 |
distance.push_back(this_distance); |
145 |
05 Sep 04 |
jari |
74 |
} |
145 |
05 Sep 04 |
jari |
75 |
} |
145 |
05 Sep 04 |
jari |
76 |
return distance; |
145 |
05 Sep 04 |
jari |
77 |
} |
145 |
05 Sep 04 |
jari |
78 |
|
145 |
05 Sep 04 |
jari |
79 |
|
1121 |
22 Feb 08 |
peter |
80 |
const utility::Matrix& NNI::imputed_data(void) const |
718 |
26 Dec 06 |
jari |
81 |
{ |
718 |
26 Dec 06 |
jari |
82 |
return imputed_data_; |
718 |
26 Dec 06 |
jari |
83 |
} |
145 |
05 Sep 04 |
jari |
84 |
|
718 |
26 Dec 06 |
jari |
85 |
|
718 |
26 Dec 06 |
jari |
86 |
const std::vector<size_t>& NNI::not_imputed(void) const |
718 |
26 Dec 06 |
jari |
87 |
{ |
718 |
26 Dec 06 |
jari |
88 |
return not_imputed_; |
718 |
26 Dec 06 |
jari |
89 |
} |
718 |
26 Dec 06 |
jari |
90 |
|
718 |
26 Dec 06 |
jari |
91 |
|
177 |
01 Oct 04 |
jari |
// Contributing nearest neighbours are added up to the user set |
177 |
01 Oct 04 |
jari |
// number, and neighbours are disqualified if their element (column) |
177 |
01 Oct 04 |
jari |
// weight is zero |
4200 |
19 Aug 22 |
peter |
95 |
std::vector<size_t> |
1271 |
09 Apr 08 |
peter |
96 |
NNI::nearest_neighbours(const size_t column, |
1271 |
09 Apr 08 |
peter |
97 |
const std::vector<std::pair<size_t,double> >& d) const |
145 |
05 Sep 04 |
jari |
98 |
{ |
1271 |
09 Apr 08 |
peter |
99 |
std::vector<size_t> index; |
145 |
05 Sep 04 |
jari |
100 |
double contribs=0; |
1271 |
09 Apr 08 |
peter |
101 |
for (size_t i=0; ((i<d.size()) && |
145 |
05 Sep 04 |
jari |
102 |
(contribs+=weight_(d[i].first,column))<=neighbours_); i++) |
145 |
05 Sep 04 |
jari |
103 |
if (weight_(d[i].first,column)) |
145 |
05 Sep 04 |
jari |
104 |
index.push_back(i); |
145 |
05 Sep 04 |
jari |
105 |
return index; |
145 |
05 Sep 04 |
jari |
106 |
} |
145 |
05 Sep 04 |
jari |
107 |
|
687 |
16 Oct 06 |
jari |
108 |
}}} // of namespace utility, yat, and theplu |