// $Id: WeNNI.cc 1554 2008-10-09 18:31:34Z jari$ /* Copyright (C) 2004 Jari Häkkinen Copyright (C) 2005 Peter Johansson Copyright (C) 2006 Jari Häkkinen Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson This file is part of the yat library, http://dev.thep.lu.se/yat The yat library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. The yat library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with yat. If not, see . */ #include "WeNNI.h" #include "Matrix.h" #include "stl_utility.h" #include #include #include #include namespace theplu { namespace yat { namespace utility { WeNNI::WeNNI(const utility::Matrix& matrix,const utility::Matrix& flag, const unsigned int neighbours) : NNI(matrix,flag,neighbours), imputed_data_raw_(matrix) { //estimate(); } // \hat{x_{ij}}=\frac{ \sum_{k=1,N} \frac{w_{kj}*x_{kj}}{d_{ki}} } // { \sum_{k=1,N} \frac{w_{kj} }{d_{ki}} } // where N is defined in the paper cited in the NNI class definition // documentation. unsigned int WeNNI::estimate(void) { double small_number=std::numeric_limits::epsilon(); for (size_t i=0; i > distance(calculate_distances(i)); std::sort(distance.begin(),distance.end(), pair_value_compare()); bool row_imputed=true; for (size_t j=0; j knn=nearest_neighbours(j,distance); double new_value=0.0; double norm=0.0; for (std::vector::const_iterator k=knn.begin(); k!=knn.end(); ++k) { // Avoid division with zero (perfect match vectors) double d=(distance[*k].second ? distance[*k].second : small_number); new_value+=(weight_(distance[*k].first,j) * data_(distance[*k].first,j)/d); norm+=weight_(distance[*k].first,j)/d; } // No impute if no contributions from neighbours. if (norm){ imputed_data_raw_(i,j) = new_value/norm; imputed_data_(i,j)= weight_(i,j)*data_(i,j) + (1-weight_(i,j))* imputed_data_raw_(i,j); } else row_imputed=false; } if (!row_imputed) not_imputed_.push_back(i); } return not_imputed_.size(); } }}} // of namespace utility, yat, and theplu