source: trunk/src/kNNI.cc @ 172

Last change on this file since 172 was 172, checked in by Jari Häkkinen, 19 years ago

Fixed stupid bug; now the original data is kept during whole impute
algorithm, i.e. imputed values are not used to impute other values.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 1.4 KB
Line 
1// $Id: kNNI.cc 172 2004-09-28 09:53:45Z jari $
2
3#include "kNNI.h"
4
5#include <algorithm>
6#include <cmath>
7#include <fstream>
8#include <vector>
9
10#include "stl_utility.h"
11
12namespace theplu {
13namespace cpptools {
14
15  kNNI::kNNI(gslapi::matrix& matrix,const gslapi::matrix& flag,
16             const u_int neighbours)
17    : NNI(matrix,flag,neighbours)
18  {
19    for (unsigned int i=0; i<weight_.rows(); i++)
20      for (unsigned int j=0; j<weight_.columns(); j++)
21        if (!weight_(i,j)) {
22          mv_rows_.push_back(i);
23          break;
24        }
25    estimate();
26  }
27
28
29
30// \hat{x_{j}}=\frac{ \sum_{1,k} \frac{x_i}{d_{ij}} }
31//                  { \sum_{1,k} \frac{1  }{d_{ij}} },
32  void kNNI::estimate(void)
33  {
34    using namespace std;
35    for (unsigned int i=0; i<mv_rows_.size(); i++) {
36      // Jari, avoid copying in next line
37      vector<pair<u_int,double> > distance=calculate_distances(mv_rows_[i]);
38      sort(distance.begin(),distance.end(),
39                pair_value_compare<u_int,double>());
40      for (unsigned int j=0; j<data_.columns(); j++)
41        if (!weight_(mv_rows_[i],j)) {
42          vector<u_int> knn=nearest_neighbours(j,distance);
43          double new_value=0.0;
44          double norm=0.0;
45          for (vector<u_int>::const_iterator k=knn.begin(); k!=knn.end(); k++) {
46            new_value+=data_(distance[*k].first,j)/(distance[*k].second);
47            norm+=1.0/(distance[*k].second);
48          }
49          // No impute if no contributions from neighbours.
50          if (norm)
51            imputed_data_(mv_rows_[i],j)=new_value/norm;
52        }
53    }
54  }
55
56
57}} // of namespace cpptools and namespace theplu
Note: See TracBrowser for help on using the repository browser.