source: trunk/src/WeNNI.cc @ 233

Last change on this file since 233 was 233, checked in by Peter, 18 years ago

function added

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 1.8 KB
Line 
1// $Id: WeNNI.cc 233 2005-02-21 14:52:30Z peter $
2
3#include "WeNNI.h"
4
5#include <algorithm>
6#include <cmath>
7#include <fstream>
8
9#include "stl_utility.h"
10
11namespace theplu {
12namespace cpptools {
13
14
15  WeNNI::WeNNI(const gslapi::matrix& matrix,const gslapi::matrix& flag,
16               const u_int neighbours)
17    : NNI(matrix,flag,neighbours), imputed_data_raw_(matrix)
18  {
19    //estimate();
20  }
21
22
23
24  // \hat{x_{ij}}=\frac{ \sum_{k=1,N} \frac{w_{kj}*x_{kj}}{d_{ki}} }
25  //                   { \sum_{k=1,N} \frac{w_{kj}       }{d_{ki}} }
26  // where N is defined in the paper cited in the NNI class definition
27  // documentation.
28  u_int WeNNI::estimate(void)
29  {
30    using namespace std;
31    for (unsigned int i=0; i<data_.rows(); i++) {
32      // Jari, avoid copying in next line
33      vector<pair<u_int,double> > distance=calculate_distances(i);
34      sort(distance.begin(),distance.end(),
35                pair_value_compare<u_int,double>());
36      bool row_imputed=true;
37      for (unsigned int j=0; j<data_.columns(); j++) {
38        vector<u_int> knn=nearest_neighbours(j,distance);
39        double new_value=0.0;
40        double norm=0.0;
41        for (vector<u_int>::const_iterator k=knn.begin(); k!=knn.end(); k++) {
42          // Jari, a small number needed here, use something standardized.
43          // Avoid division with zero (perfect match vectors)
44          double d=(distance[*k].second ? distance[*k].second : 1e-10);
45          new_value+=(weight_(distance[*k].first,j) *
46                      data_(distance[*k].first,j)/d);
47          norm+=weight_(distance[*k].first,j)/d;
48        }
49        // No impute if no contributions from neighbours.
50        if (norm){
51          imputed_data_raw_(i,j) = new_value/norm;
52          imputed_data_(i,j)=
53            weight_(i,j)*data_(i,j) + (1-weight_(i,j))* imputed_data_raw_(i,j);
54        }
55        else
56          row_imputed=false;
57      }
58      if (!row_imputed)
59        not_imputed_.push_back(i);
60    }
61    return not_imputed_.size();
62  }
63
64
65}} // of namespace cpptools and namespace theplu
Note: See TracBrowser for help on using the repository browser.