source: trunk/c++_tools/utility/kNNI.cc @ 675

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

References #83. Changing project name to yat. Compilation will fail in this revision.

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