# source:trunk/src/WeNNI.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.4 KB
Line
1// $Id: WeNNI.cc 174 2004-09-29 12:54:38Z jari$
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(gslapi::matrix& matrix,const gslapi::matrix& flag,
16               const u_int neighbours)
17    : NNI(matrix,flag,neighbours)
18  {
19    estimate();
20  }
21
22
23
24// \hat{x_{j}}=\frac{ \sum_{i,N} \frac{w_{ij}*x_i}{d_{ij}} }
25//                  { \sum_{i,N} \frac{w_{ij}    }{d_{ij}} }
26  void WeNNI::estimate(void)
27  {
28    using namespace std;
29    for (unsigned int i=0; i<data_.rows(); i++) {
30      // Jari, avoid copying in next line
31      vector<pair<u_int,double> > distance=calculate_distances(i);
32      sort(distance.begin(),distance.end(),
33                pair_value_compare<u_int,double>());
34      for (unsigned int j=0; j<data_.columns(); j++) {
35        vector<u_int> knn=nearest_neighbours(j,distance);
36        double new_value=0.0;
37        double norm=0.0;
38        for (vector<u_int>::const_iterator k=knn.begin(); k!=knn.end(); k++) {
39          // Jari, a small number needed here, use something standardized.
40          // Avoid division with zero (perfect match vectors)
41          double d=(distance[*k].second ? distance[*k].second : 1e-10);
42          new_value+=1.0*data_(distance[*k].first,j)/d;
43          norm+=1.0/d;
44        }
45        // No impute if no contributions from neighbours.
46        if (norm)
47          imputed_data_(i,j)=weight_(i,j)*data_(i,j)+(1-weight_(i,j))*new_value/norm;
48      }
49    }
50  }
51
52
53}} // of namespace cpptools and namespace theplu
Note: See TracBrowser for help on using the repository browser.