yat/utility/WeNNI.cc

Code
Comments
Other
Rev Date Author Line
145 05 Sep 04 jari 1 // $Id$
145 05 Sep 04 jari 2
570 05 Apr 06 jari 3 /*
2119 12 Dec 09 peter 4   Copyright (C) 2004 Jari Häkkinen
831 27 Mar 07 peter 5   Copyright (C) 2005 Peter Johansson
2119 12 Dec 09 peter 6   Copyright (C) 2006 Jari Häkkinen
2119 12 Dec 09 peter 7   Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
2119 12 Dec 09 peter 8   Copyright (C) 2009 Jari Häkkinen
4207 26 Aug 22 peter 9   Copyright (C) 2011, 2012, 2022 Peter Johansson
570 05 Apr 06 jari 10
1437 25 Aug 08 peter 11   This file is part of the yat library, http://dev.thep.lu.se/yat
570 05 Apr 06 jari 12
675 10 Oct 06 jari 13   The yat library is free software; you can redistribute it and/or
675 10 Oct 06 jari 14   modify it under the terms of the GNU General Public License as
1486 09 Sep 08 jari 15   published by the Free Software Foundation; either version 3 of the
675 10 Oct 06 jari 16   License, or (at your option) any later version.
570 05 Apr 06 jari 17
675 10 Oct 06 jari 18   The yat library is distributed in the hope that it will be useful,
675 10 Oct 06 jari 19   but WITHOUT ANY WARRANTY; without even the implied warranty of
675 10 Oct 06 jari 20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
570 05 Apr 06 jari 21   General Public License for more details.
570 05 Apr 06 jari 22
570 05 Apr 06 jari 23   You should have received a copy of the GNU General Public License
1487 10 Sep 08 jari 24   along with yat. If not, see <http://www.gnu.org/licenses/>.
570 05 Apr 06 jari 25 */
570 05 Apr 06 jari 26
2881 18 Nov 12 peter 27 #include <config.h>
2881 18 Nov 12 peter 28
680 11 Oct 06 jari 29 #include "WeNNI.h"
1121 22 Feb 08 peter 30 #include "Matrix.h"
680 11 Oct 06 jari 31 #include "stl_utility.h"
145 05 Sep 04 jari 32
145 05 Sep 04 jari 33 #include <algorithm>
145 05 Sep 04 jari 34 #include <cmath>
145 05 Sep 04 jari 35 #include <fstream>
1554 09 Oct 08 jari 36 #include <limits>
145 05 Sep 04 jari 37
145 05 Sep 04 jari 38 namespace theplu {
680 11 Oct 06 jari 39 namespace yat {
301 30 Apr 05 peter 40 namespace utility {
145 05 Sep 04 jari 41
145 05 Sep 04 jari 42
4125 14 Jan 22 peter 43   WeNNI::WeNNI(const MatrixBase& matrix, const MatrixBase& flag,
1271 09 Apr 08 peter 44                const unsigned int neighbours)
233 21 Feb 05 peter 45     : NNI(matrix,flag,neighbours), imputed_data_raw_(matrix)
145 05 Sep 04 jari 46   {
228 01 Feb 05 peter 47     //estimate();
145 05 Sep 04 jari 48   }
145 05 Sep 04 jari 49
145 05 Sep 04 jari 50
177 01 Oct 04 jari 51   // \hat{x_{ij}}=\frac{ \sum_{k=1,N} \frac{w_{kj}*x_{kj}}{d_{ki}} }
177 01 Oct 04 jari 52   //                   { \sum_{k=1,N} \frac{w_{kj}       }{d_{ki}} }
177 01 Oct 04 jari 53   // where N is defined in the paper cited in the NNI class definition
177 01 Oct 04 jari 54   // documentation.
1271 09 Apr 08 peter 55   unsigned int WeNNI::estimate(void)
145 05 Sep 04 jari 56   {
1554 09 Oct 08 jari 57     double small_number=std::numeric_limits<double>::epsilon();
1271 09 Apr 08 peter 58     for (size_t i=0; i<data_.rows(); i++) {
1271 09 Apr 08 peter 59       std::vector<std::pair<size_t,double> > distance(calculate_distances(i));
616 31 Aug 06 jari 60       std::sort(distance.begin(),distance.end(),
1271 09 Apr 08 peter 61                 pair_value_compare<size_t,double>());
233 21 Feb 05 peter 62       bool row_imputed=true;
1271 09 Apr 08 peter 63       for (size_t j=0; j<data_.columns(); j++) {
1271 09 Apr 08 peter 64         std::vector<size_t> knn=nearest_neighbours(j,distance);
145 05 Sep 04 jari 65         double new_value=0.0;
145 05 Sep 04 jari 66         double norm=0.0;
1271 09 Apr 08 peter 67         for (std::vector<size_t>::const_iterator k=knn.begin(); k!=knn.end();
616 31 Aug 06 jari 68              ++k) {
174 29 Sep 04 jari 69           // Avoid division with zero (perfect match vectors)
1554 09 Oct 08 jari 70           double d=(distance[*k].second ? distance[*k].second : small_number);
1725 15 Jan 09 jari 71           double w=weight_(distance[*k].first,j)/d;
1725 15 Jan 09 jari 72           if (w) {
1725 15 Jan 09 jari 73             new_value += w*data_(distance[*k].first,j);
1725 15 Jan 09 jari 74             norm      += w;
1725 15 Jan 09 jari 75           }
145 05 Sep 04 jari 76         }
157 15 Sep 04 jari 77         // No impute if no contributions from neighbours.
1725 15 Jan 09 jari 78         if (norm) {
233 21 Feb 05 peter 79           imputed_data_raw_(i,j) = new_value/norm;
1725 15 Jan 09 jari 80           double w=weight_(i,j);
1725 15 Jan 09 jari 81           if (w)
1725 15 Jan 09 jari 82             imputed_data_(i,j) = w*data_(i,j) + (1-w)*imputed_data_raw_(i,j);
1725 15 Jan 09 jari 83           else
1725 15 Jan 09 jari 84             imputed_data_(i,j) = imputed_data_raw_(i,j);
233 21 Feb 05 peter 85         }
228 01 Feb 05 peter 86         else
233 21 Feb 05 peter 87           row_imputed=false;
145 05 Sep 04 jari 88       }
233 21 Feb 05 peter 89       if (!row_imputed)
233 21 Feb 05 peter 90         not_imputed_.push_back(i);
145 05 Sep 04 jari 91     }
228 01 Feb 05 peter 92     return not_imputed_.size();
145 05 Sep 04 jari 93   }
145 05 Sep 04 jari 94
145 05 Sep 04 jari 95
4200 19 Aug 22 peter 96   const utility::Matrix& WeNNI::imputed_data_raw(void) const
4200 19 Aug 22 peter 97   {
4200 19 Aug 22 peter 98     return imputed_data_raw_;
2509 08 Jul 11 peter 99   }
2509 08 Jul 11 peter 100
687 16 Oct 06 jari 101 }}} // of namespace utility, yat, and theplu