source: trunk/yat/utility/WeNNI.cc @ 1703

Last change on this file since 1703 was 1554, checked in by Jari Häkkinen, 13 years ago

Fixes #192. Using std::numeric_limits, since impute algorithms are slightly changed template results also changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.7 KB
Line 
1// $Id: WeNNI.cc 1554 2008-10-09 18:31:34Z jari $
2
3/*
4  Copyright (C) 2004 Jari Häkkinen
5  Copyright (C) 2005 Peter Johansson
6  Copyright (C) 2006 Jari Häkkinen
7  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
8
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include "WeNNI.h"
26#include "Matrix.h"
27#include "stl_utility.h"
28
29#include <algorithm>
30#include <cmath>
31#include <fstream>
32#include <limits>
33
34namespace theplu {
35namespace yat {
36namespace utility {
37
38
39  WeNNI::WeNNI(const utility::Matrix& matrix,const utility::Matrix& flag,
40               const unsigned int neighbours)
41    : NNI(matrix,flag,neighbours), imputed_data_raw_(matrix)
42  {
43    //estimate();
44  }
45
46
47
48  // \hat{x_{ij}}=\frac{ \sum_{k=1,N} \frac{w_{kj}*x_{kj}}{d_{ki}} }
49  //                   { \sum_{k=1,N} \frac{w_{kj}       }{d_{ki}} }
50  // where N is defined in the paper cited in the NNI class definition
51  // documentation.
52  unsigned int WeNNI::estimate(void)
53  {
54    double small_number=std::numeric_limits<double>::epsilon();
55    for (size_t i=0; i<data_.rows(); i++) {
56      std::vector<std::pair<size_t,double> > distance(calculate_distances(i));
57      std::sort(distance.begin(),distance.end(),
58                pair_value_compare<size_t,double>());
59      bool row_imputed=true;
60      for (size_t j=0; j<data_.columns(); j++) {
61        std::vector<size_t> knn=nearest_neighbours(j,distance);
62        double new_value=0.0;
63        double norm=0.0;
64        for (std::vector<size_t>::const_iterator k=knn.begin(); k!=knn.end();
65             ++k) {
66          // Avoid division with zero (perfect match vectors)
67          double d=(distance[*k].second ? distance[*k].second : small_number);
68          new_value+=(weight_(distance[*k].first,j) *
69                      data_(distance[*k].first,j)/d);
70          norm+=weight_(distance[*k].first,j)/d;
71        }
72        // No impute if no contributions from neighbours.
73        if (norm){
74          imputed_data_raw_(i,j) = new_value/norm;
75          imputed_data_(i,j)=
76            weight_(i,j)*data_(i,j) + (1-weight_(i,j))* imputed_data_raw_(i,j);
77        }
78        else
79          row_imputed=false;
80      }
81      if (!row_imputed)
82        not_imputed_.push_back(i);
83    }
84    return not_imputed_.size();
85  }
86
87
88}}} // of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.