# source:trunk/src/kNNI.cc@174

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

Added checks on perfect match rows, i.e. avoid zero (distance) division.

• Property svn:eol-style set to native
• Property svn:keywords set to Author Date Id Revision
File size: 1.6 KB
Line
1// $Id: kNNI.cc 174 2004-09-29 12:54:38Z 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            // Jari, a small number needed here, use something standardized.
47            // Avoid division with zero (perfect match vectors)
48            double d=(distance[*k].second ? distance[*k].second : 1e-10);
49            new_value+=data_(distance[*k].first,j)/d;
50            norm+=1.0/d;
51          }
52          // No impute if no contributions from neighbours.
53          if (norm)
54            imputed_data_(mv_rows_[i],j)=new_value/norm;
55        }
56    }
57  }
58
59
60}} // of namespace cpptools and namespace theplu
Note: See TracBrowser for help on using the repository browser.