plugins/base1/se.lu.thep.wenni/trunk/lib/c++_tools/utility/NNI.cc

Code
Comments
Other
Rev Date Author Line
69 11 Feb 06 jari 1 // $Id$
69 11 Feb 06 jari 2
95 05 Apr 06 jari 3 /*
95 05 Apr 06 jari 4   Copyright (C) 2004 Jari Häkkinen
95 05 Apr 06 jari 5   Copyright (C) 2005 Peter Johansson
95 05 Apr 06 jari 6   Copyright (C) 2006 Jari Häkkinen
95 05 Apr 06 jari 7
95 05 Apr 06 jari 8   This file is part of the thep c++ tools library,
95 05 Apr 06 jari 9                                 http://lev.thep.lu.se/trac/c++_tools
95 05 Apr 06 jari 10
95 05 Apr 06 jari 11   The c++ tools library is free software; you can redistribute it
95 05 Apr 06 jari 12   and/or modify it under the terms of the GNU General Public License
824 26 Nov 08 jari 13   as published by the Free Software Foundation; either version 3 of
95 05 Apr 06 jari 14   the License, or (at your option) any later version.
95 05 Apr 06 jari 15
95 05 Apr 06 jari 16   The c++ tools library is distributed in the hope that it will be
95 05 Apr 06 jari 17   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
95 05 Apr 06 jari 18   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
95 05 Apr 06 jari 19   General Public License for more details.
95 05 Apr 06 jari 20
95 05 Apr 06 jari 21   You should have received a copy of the GNU General Public License
824 26 Nov 08 jari 22   along with WeNNI. If not, see <http://www.gnu.org/licenses/>.
95 05 Apr 06 jari 23 */
95 05 Apr 06 jari 24
69 11 Feb 06 jari 25 #include <c++_tools/utility/NNI.h>
69 11 Feb 06 jari 26
69 11 Feb 06 jari 27 #include <c++_tools/utility/stl_utility.h>
69 11 Feb 06 jari 28
69 11 Feb 06 jari 29 #include <algorithm>
69 11 Feb 06 jari 30 #include <cmath>
69 11 Feb 06 jari 31 #include <fstream>
69 11 Feb 06 jari 32
69 11 Feb 06 jari 33
69 11 Feb 06 jari 34 namespace theplu {
69 11 Feb 06 jari 35 namespace utility {
69 11 Feb 06 jari 36
69 11 Feb 06 jari 37   using namespace std;
69 11 Feb 06 jari 38
69 11 Feb 06 jari 39   // For a discussion and motivation for various algorithm
69 11 Feb 06 jari 40   // implementations here see the paper cited in the class definition
69 11 Feb 06 jari 41   // documentation.
69 11 Feb 06 jari 42   NNI::NNI(const gslapi::matrix& matrix,const gslapi::matrix& weight,
819 24 Nov 08 jari 43            const unsigned int neighbours)
69 11 Feb 06 jari 44     : data_(matrix), imputed_data_(matrix), neighbours_(neighbours),
69 11 Feb 06 jari 45       weight_(weight)
69 11 Feb 06 jari 46   {
69 11 Feb 06 jari 47   }
69 11 Feb 06 jari 48
69 11 Feb 06 jari 49
69 11 Feb 06 jari 50   // d_{ij}^2=\frac {\sum_{k=1,C} w_{ik} w_{jk} (x_{ik}-x_{jk})^2 }
69 11 Feb 06 jari 51   //                {\sum_{k=l,C} w_{ik} w_{jk} }
69 11 Feb 06 jari 52   // where C is the number of columns
819 24 Nov 08 jari 53   vector<pair<unsigned int,double> > NNI::calculate_distances(const unsigned int row) const
69 11 Feb 06 jari 54   {
819 24 Nov 08 jari 55     vector<pair<unsigned int,double> > distance;
69 11 Feb 06 jari 56     for (size_t i=0; i<data_.rows(); i++)
69 11 Feb 06 jari 57       if (i!=row) {
69 11 Feb 06 jari 58         double contribs=0;
819 24 Nov 08 jari 59         pair<unsigned int,double> this_distance(i,0.0);
69 11 Feb 06 jari 60         for (size_t j=0; j<data_.columns(); j++)
69 11 Feb 06 jari 61           // 0 contribution for missing values
69 11 Feb 06 jari 62           if (weight_(i,j) && weight_(row,j)) {
69 11 Feb 06 jari 63             double weight_factor=weight_(i,j)*weight_(row,j);
69 11 Feb 06 jari 64             this_distance.second+=( weight_factor *
69 11 Feb 06 jari 65                                     (data_(i,j)-data_(row,j)) *
69 11 Feb 06 jari 66                                     (data_(i,j)-data_(row,j)) );
69 11 Feb 06 jari 67             contribs+=weight_factor;
69 11 Feb 06 jari 68           }
69 11 Feb 06 jari 69         if (contribs) {  // ignore lines without any contributions
69 11 Feb 06 jari 70           this_distance.second=sqrt(this_distance.second/contribs);
69 11 Feb 06 jari 71           distance.push_back(this_distance);
69 11 Feb 06 jari 72         }
69 11 Feb 06 jari 73       }
69 11 Feb 06 jari 74     return distance;
69 11 Feb 06 jari 75   }
69 11 Feb 06 jari 76
69 11 Feb 06 jari 77
69 11 Feb 06 jari 78
69 11 Feb 06 jari 79   // Contributing nearest neighbours are added up to the user set
69 11 Feb 06 jari 80   // number, and neighbours are disqualified if their element (column)
69 11 Feb 06 jari 81   // weight is zero
819 24 Nov 08 jari 82   vector<unsigned int> 
819 24 Nov 08 jari 83   NNI::nearest_neighbours(const unsigned int column,
819 24 Nov 08 jari 84                           const vector<pair<unsigned int,double> >& d) const
69 11 Feb 06 jari 85   {
819 24 Nov 08 jari 86     vector<unsigned int> index;
69 11 Feb 06 jari 87     double contribs=0;
819 24 Nov 08 jari 88     for (unsigned int i=0; ((i<d.size()) &&
69 11 Feb 06 jari 89                      (contribs+=weight_(d[i].first,column))<=neighbours_); i++)
69 11 Feb 06 jari 90       if (weight_(d[i].first,column))
69 11 Feb 06 jari 91         index.push_back(i);
69 11 Feb 06 jari 92     return index;
69 11 Feb 06 jari 93   }
69 11 Feb 06 jari 94
69 11 Feb 06 jari 95
69 11 Feb 06 jari 96 }} // of namespace utility and namespace theplu