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

Last change on this file since 1437 was 1437, checked in by Peter, 13 years ago

merge patch release 0.4.2 to trunk. Delta 0.4.2-0.4.1

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