PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // NeighborhoodBoxVolumeDensityEstimator.cc 00004 // 00005 // Copyright (C) 2006 Pascal Vincent 00006 // 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // 1. Redistributions of source code must retain the above copyright 00011 // notice, this list of conditions and the following disclaimer. 00012 // 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // 3. The name of the authors may not be used to endorse or promote 00018 // products derived from this software without specific prior written 00019 // permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // This file is part of the PLearn library. For more information on the PLearn 00033 // library, go to the PLearn Web site at www.plearn.org 00034 00035 /* ******************************************************* 00036 * $Id: NeighborhoodBoxVolumeDensityEstimator.cc 5668 2006-05-25 17:50:24Z plearner $ 00037 ******************************************************* */ 00038 00039 // Authors: Pascal Vincent 00040 00044 #include "NeighborhoodBoxVolumeDensityEstimator.h" 00045 #include <plearn/vmat/ConcatColumnsVMatrix.h> 00046 #include <plearn/vmat/MemoryVMatrix.h> 00047 #include <plearn_learners/nearest_neighbors/ExhaustiveNearestNeighbors.h> 00048 #include <plearn/base/tostring.h> 00049 00050 namespace PLearn { 00051 using namespace std; 00052 00054 // NeighborhoodBoxVolumeDensityEstimator // 00056 NeighborhoodBoxVolumeDensityEstimator::NeighborhoodBoxVolumeDensityEstimator() 00057 :nneighbors(1), 00058 min_radius(1e-6) 00059 { 00060 // for default use Exhaustive search and default Euclidean distance 00061 NN = new ExhaustiveNearestNeighbors(); 00062 } 00063 00064 PLEARN_IMPLEMENT_OBJECT(NeighborhoodBoxVolumeDensityEstimator, 00065 "Simple density estimation based on the volume of the smallest symmetic box, " 00066 "centered on the test point, and containing its k neighbors.", 00067 "This estimator is not guaranteed to sum to 1." 00068 ); 00069 00071 // declareOptions // 00073 void NeighborhoodBoxVolumeDensityEstimator::declareOptions(OptionList& ol) 00074 { 00075 // ### Declare all of this object's options here 00076 // ### For the "flags" of each option, you should typically specify 00077 // ### one of OptionBase::buildoption, OptionBase::learntoption or 00078 // ### OptionBase::tuningoption. Another possible flag to be combined with 00079 // ### is OptionBase::nosave 00080 00081 declareOption(ol, "nneighbors", &NeighborhoodBoxVolumeDensityEstimator::nneighbors, OptionBase::buildoption, 00082 "Number of neighbors to consider to determine the boxed region centered on the test point"); 00083 00084 declareOption(ol, "min_radius", &NeighborhoodBoxVolumeDensityEstimator::min_radius, OptionBase::buildoption, 00085 "This is added to each dimension of the found box (as some dimensions may be initially 0)"); 00086 00087 declareOption(ol, "train_set", &NeighborhoodBoxVolumeDensityEstimator::train_set, OptionBase::learntoption, 00088 "We need to store the training set, as this learner is memory-based..."); 00089 00090 declareOption(ol, "NN", &NeighborhoodBoxVolumeDensityEstimator::NN, OptionBase::buildoption, 00091 "The nearest neighbor search method to use (default uses ehaustive search with Euclidean distance)"); 00092 00093 // Now call the parent class' declareOptions(). 00094 inherited::declareOptions(ol); 00095 } 00096 00098 // build // 00100 void NeighborhoodBoxVolumeDensityEstimator::build() 00101 { 00102 // ### Nothing to add here, simply calls build_(). 00103 inherited::build(); 00104 build_(); 00105 } 00106 00108 // build_ // 00110 void NeighborhoodBoxVolumeDensityEstimator::build_() 00111 { 00112 // ### This method should do the real building of the object, 00113 // ### according to set 'options', in *any* situation. 00114 // ### Typical situations include: 00115 // ### - Initial building of an object from a few user-specified options 00116 // ### - Building of a "reloaded" object: i.e. from the complete set of all serialised options. 00117 // ### - Updating or "re-building" of an object after a few "tuning" options have been modified. 00118 // ### You should assume that the parent class' build_() has already been called. 00119 00120 // ### If the distribution is conditional, you should finish build_() by: 00121 // PDistribution::finishConditionalBuild(); 00122 00123 NN->num_neighbors = nneighbors; 00124 NN->copy_input = false; 00125 NN->copy_target = false; 00126 NN->copy_weight = false; 00127 NN->copy_index = true; 00128 NN->build(); 00129 if(!train_set.isNull()) 00130 { 00131 NN->setTrainingSet(train_set); 00132 NN->train(); 00133 } 00134 NN_outputs.resize(nneighbors); 00135 } 00136 00138 // log_density // 00140 bool NeighborhoodBoxVolumeDensityEstimator::box_contains(const Vec& center, const Vec& radius, const Vec& input) const 00141 { 00142 int w = center.length(); 00143 for(int j=0; j<w; j++) 00144 if(fabs(input[j]-center[j])>radius[j]) 00145 return false; 00146 return true; 00147 } 00148 00149 00150 real NeighborhoodBoxVolumeDensityEstimator::log_density(const Vec& y) const 00151 { 00152 int l = train_set.length(); 00153 int w = inputsize(); 00154 int ws = train_set->weightsize(); 00155 trainsample.resize(w+ws); 00156 Vec input = trainsample.subVec(0,w); 00157 00158 // Find distance d to width_neighbor neighbour 00159 NN->computeOutput(y, NN_outputs); 00160 00161 Vec radius(w); 00162 for(int k=0; k<NN_outputs.length(); k++) 00163 { 00164 train_set->getRow((int)NN_outputs[k],trainsample); 00165 for(int j=0; j<w; j++) 00166 { 00167 real d = fabs(y[j]-input[j]); 00168 radius[j] = max(radius[j],d); 00169 } 00170 } 00171 radius += min_radius; 00172 00173 int region_count = 0; 00174 for(int i=0; i<l; i++) 00175 { 00176 train_set->getRow(i,trainsample); 00177 if(box_contains(y,radius,input)) 00178 region_count++; 00179 } 00180 00181 double log_region_volume = 0; 00182 for(int j=0; j<w; j++) 00183 log_region_volume += pl_log(radius[j]); 00184 00185 return pl_log(double(region_count))-log_region_volume-pl_log(double(l)); 00186 } 00187 00189 // makeDeepCopyFromShallowCopy // 00191 void NeighborhoodBoxVolumeDensityEstimator::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00192 { 00193 inherited::makeDeepCopyFromShallowCopy(copies); 00194 00195 // ### Call deepCopyField on all "pointer-like" fields 00196 // ### that you wish to be deepCopied rather than 00197 // ### shallow-copied. 00198 // ### ex: 00199 // deepCopyField(trainvec, copies); 00200 deepCopyField(NN, copies); 00201 } 00202 00203 00204 // ### Remove this method, if your distribution does not implement it. 00206 // train // 00208 void NeighborhoodBoxVolumeDensityEstimator::train() 00209 { 00210 NN->setTrainingSet(train_set); 00211 NN->train(); 00212 } 00213 00214 00215 void NeighborhoodBoxVolumeDensityEstimator::forget() 00216 { 00217 NN->forget(); 00218 } 00219 00220 00221 } 00222 00223 00224 /* 00225 Local Variables: 00226 mode:c++ 00227 c-basic-offset:4 00228 c-file-style:"stroustrup" 00229 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00230 indent-tabs-mode:nil 00231 fill-column:79 00232 End: 00233 */ 00234 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :