source: trunk/yat/utility/kNNI.cc

Last change on this file was 2919, checked in by Peter, 9 years ago

update copyright years

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.8 KB
Line 
1// $Id: kNNI.cc 2919 2012-12-19 06:54:23Z 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, 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2012 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 3 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 yat. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include <config.h>
27
28#include "kNNI.h"
29#include "stl_utility.h"
30
31#include <algorithm>
32#include <cmath>
33#include <fstream>
34#include <vector>
35#include <limits>
36
37namespace theplu {
38namespace yat {
39namespace utility {
40
41  kNNI::kNNI(const utility::Matrix& matrix,const utility::Matrix& flag,
42             const unsigned int neighbours)
43    : NNI(matrix,flag,neighbours)
44  {
45    for (unsigned int i=0; i<weight_.rows(); i++)
46      for (unsigned int j=0; j<weight_.columns(); j++)
47        if (!weight_(i,j)) {
48          mv_rows_.push_back(i);
49          break;
50        }
51    //estimate();
52  }
53
54
55
56  // \hat{x_{ij}}=\frac{ \sum_{k=1,N} \frac{x_{kj}}{d_{ki}} }
57  //                   { \sum_{k=1,N} \frac{1     }{d_{ki}} },
58  // where N is defined in the paper cited in the NNI class definition
59  // documentation.
60  unsigned int kNNI::estimate(void)
61  {
62    double small_number=std::numeric_limits<double>::epsilon();
63    for (size_t i=0; i<mv_rows_.size(); i++) {
64      std::vector<std::pair<size_t,double> >
65        distance(calculate_distances(mv_rows_[i]));
66      std::sort(distance.begin(),distance.end(),
67                pair_value_compare<size_t,double>());
68      for (size_t j=0; j<data_.columns(); j++)
69        if (!weight_(mv_rows_[i],j)) {
70          std::vector<size_t> knn=nearest_neighbours(j,distance);
71          double new_value=0.0;
72          double norm=0.0;
73          for (std::vector<size_t>::const_iterator k=knn.begin(); k!=knn.end();
74               ++k) {
75            // Avoid division with zero (perfect match vectors)
76            double d=(distance[*k].second ? distance[*k].second : small_number);
77            new_value+=data_(distance[*k].first,j)/d;
78            norm+=1.0/d;
79          }
80          // No impute if no contributions from neighbours.
81          if (norm)
82            imputed_data_(mv_rows_[i],j)=new_value/norm;
83          else {
84            not_imputed_.push_back(i);
85            // if norm is zero for one column it is zero for all columns
86            // having zero weight
87            break;
88          }
89        }
90    }
91    return not_imputed_.size();
92  }
93
94
95}}} // of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.