69 |
11 Feb 06 |
jari |
// $Id$ |
69 |
11 Feb 06 |
jari |
2 |
|
95 |
05 Apr 06 |
jari |
3 |
/* |
95 |
05 Apr 06 |
jari |
Copyright (C) 2004 Jari Häkkinen |
95 |
05 Apr 06 |
jari |
Copyright (C) 2005 Jari Häkkinen, Peter Johansson |
95 |
05 Apr 06 |
jari |
Copyright (C) 2006 Jari Häkkinen |
95 |
05 Apr 06 |
jari |
7 |
|
95 |
05 Apr 06 |
jari |
This file is part of the thep c++ tools library, |
95 |
05 Apr 06 |
jari |
http://lev.thep.lu.se/trac/c++_tools |
95 |
05 Apr 06 |
jari |
10 |
|
95 |
05 Apr 06 |
jari |
The c++ tools library is free software; you can redistribute it |
95 |
05 Apr 06 |
jari |
and/or modify it under the terms of the GNU General Public License |
824 |
26 Nov 08 |
jari |
as published by the Free Software Foundation; either version 3 of |
95 |
05 Apr 06 |
jari |
the License, or (at your option) any later version. |
95 |
05 Apr 06 |
jari |
15 |
|
95 |
05 Apr 06 |
jari |
The c++ tools library is distributed in the hope that it will be |
95 |
05 Apr 06 |
jari |
useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
95 |
05 Apr 06 |
jari |
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
95 |
05 Apr 06 |
jari |
General Public License for more details. |
95 |
05 Apr 06 |
jari |
20 |
|
95 |
05 Apr 06 |
jari |
You should have received a copy of the GNU General Public License |
824 |
26 Nov 08 |
jari |
along with WeNNI. If not, see <http://www.gnu.org/licenses/>. |
95 |
05 Apr 06 |
jari |
23 |
*/ |
95 |
05 Apr 06 |
jari |
24 |
|
69 |
11 Feb 06 |
jari |
25 |
#include <c++_tools/utility/WeNNI.h> |
69 |
11 Feb 06 |
jari |
26 |
|
69 |
11 Feb 06 |
jari |
27 |
#include <c++_tools/utility/stl_utility.h> |
69 |
11 Feb 06 |
jari |
28 |
|
69 |
11 Feb 06 |
jari |
29 |
#include <algorithm> |
69 |
11 Feb 06 |
jari |
30 |
#include <cmath> |
69 |
11 Feb 06 |
jari |
31 |
#include <fstream> |
69 |
11 Feb 06 |
jari |
32 |
|
69 |
11 Feb 06 |
jari |
33 |
namespace theplu { |
69 |
11 Feb 06 |
jari |
34 |
namespace utility { |
69 |
11 Feb 06 |
jari |
35 |
|
69 |
11 Feb 06 |
jari |
36 |
|
69 |
11 Feb 06 |
jari |
37 |
WeNNI::WeNNI(const gslapi::matrix& matrix,const gslapi::matrix& flag, |
819 |
24 Nov 08 |
jari |
38 |
const unsigned int neighbours) |
69 |
11 Feb 06 |
jari |
39 |
: NNI(matrix,flag,neighbours), imputed_data_raw_(matrix) |
69 |
11 Feb 06 |
jari |
40 |
{ |
69 |
11 Feb 06 |
jari |
//estimate(); |
69 |
11 Feb 06 |
jari |
42 |
} |
69 |
11 Feb 06 |
jari |
43 |
|
69 |
11 Feb 06 |
jari |
44 |
|
69 |
11 Feb 06 |
jari |
45 |
|
69 |
11 Feb 06 |
jari |
// \hat{x_{ij}}=\frac{ \sum_{k=1,N} \frac{w_{kj}*x_{kj}}{d_{ki}} } |
69 |
11 Feb 06 |
jari |
// { \sum_{k=1,N} \frac{w_{kj} }{d_{ki}} } |
69 |
11 Feb 06 |
jari |
// where N is defined in the paper cited in the NNI class definition |
69 |
11 Feb 06 |
jari |
// documentation. |
819 |
24 Nov 08 |
jari |
50 |
unsigned int WeNNI::estimate(void) |
69 |
11 Feb 06 |
jari |
51 |
{ |
69 |
11 Feb 06 |
jari |
52 |
using namespace std; |
69 |
11 Feb 06 |
jari |
53 |
for (unsigned int i=0; i<data_.rows(); i++) { |
69 |
11 Feb 06 |
jari |
// Jari, avoid copying in next line |
819 |
24 Nov 08 |
jari |
55 |
vector<pair<unsigned int,double> > distance=calculate_distances(i); |
69 |
11 Feb 06 |
jari |
56 |
sort(distance.begin(),distance.end(), |
819 |
24 Nov 08 |
jari |
57 |
pair_value_compare<unsigned int,double>()); |
69 |
11 Feb 06 |
jari |
58 |
bool row_imputed=true; |
69 |
11 Feb 06 |
jari |
59 |
for (unsigned int j=0; j<data_.columns(); j++) { |
819 |
24 Nov 08 |
jari |
60 |
vector<unsigned int> knn=nearest_neighbours(j,distance); |
69 |
11 Feb 06 |
jari |
61 |
double new_value=0.0; |
69 |
11 Feb 06 |
jari |
62 |
double norm=0.0; |
819 |
24 Nov 08 |
jari |
63 |
for (vector<unsigned int>::const_iterator k=knn.begin(); k!=knn.end(); k++) { |
69 |
11 Feb 06 |
jari |
// Jari, a small number needed here, use something standardized. |
69 |
11 Feb 06 |
jari |
// Avoid division with zero (perfect match vectors) |
69 |
11 Feb 06 |
jari |
66 |
double d=(distance[*k].second ? distance[*k].second : 1e-10); |
69 |
11 Feb 06 |
jari |
67 |
new_value+=(weight_(distance[*k].first,j) * |
69 |
11 Feb 06 |
jari |
68 |
data_(distance[*k].first,j)/d); |
69 |
11 Feb 06 |
jari |
69 |
norm+=weight_(distance[*k].first,j)/d; |
69 |
11 Feb 06 |
jari |
70 |
} |
69 |
11 Feb 06 |
jari |
// No impute if no contributions from neighbours. |
69 |
11 Feb 06 |
jari |
72 |
if (norm){ |
69 |
11 Feb 06 |
jari |
73 |
imputed_data_raw_(i,j) = new_value/norm; |
69 |
11 Feb 06 |
jari |
74 |
imputed_data_(i,j)= |
69 |
11 Feb 06 |
jari |
75 |
weight_(i,j)*data_(i,j) + (1-weight_(i,j))* imputed_data_raw_(i,j); |
69 |
11 Feb 06 |
jari |
76 |
} |
69 |
11 Feb 06 |
jari |
77 |
else |
69 |
11 Feb 06 |
jari |
78 |
row_imputed=false; |
69 |
11 Feb 06 |
jari |
79 |
} |
69 |
11 Feb 06 |
jari |
80 |
if (!row_imputed) |
69 |
11 Feb 06 |
jari |
81 |
not_imputed_.push_back(i); |
69 |
11 Feb 06 |
jari |
82 |
} |
69 |
11 Feb 06 |
jari |
83 |
return not_imputed_.size(); |
69 |
11 Feb 06 |
jari |
84 |
} |
69 |
11 Feb 06 |
jari |
85 |
|
69 |
11 Feb 06 |
jari |
86 |
|
69 |
11 Feb 06 |
jari |
87 |
}} // of namespace utility and namespace theplu |