PLearn 0.1
LimitedGaussianSmoother.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // LimitedGaussianSmoother.cc
00004 // 
00005 // Copyright (C) 2002 Xavier Saint-Mleux
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: LimitedGaussianSmoother.cc 6861 2007-04-09 19:04:15Z saintmlx $ 
00037  ******************************************************* */
00038 
00040 #include "LimitedGaussianSmoother.h"
00041 #include "pl_erf.h"
00042 
00043 namespace PLearn {
00044 using namespace std;
00045 
00046 LimitedGaussianSmoother::LimitedGaussianSmoother() 
00047     :Smoother()
00048 /* ### Initialise all fields to their default value */
00049 {
00050     // ...
00051 
00052     // ### You may or may not want to call build_() to finish building the object
00053     // build_();
00054 }
00055 
00056 LimitedGaussianSmoother::LimitedGaussianSmoother(real window_size_wrt_sigma_, real sigma_bin_)
00057     :Smoother(), window_size_wrt_sigma(window_size_wrt_sigma_), sigma_bin(sigma_bin_)
00058 {}
00059 
00060 PLEARN_IMPLEMENT_OBJECT(LimitedGaussianSmoother, "ONE LINE DESCR", "NO HELP");
00061 
00062 void LimitedGaussianSmoother::declareOptions(OptionList& ol)
00063 {
00064     // ### Declare all of this object's options here
00065     // ### For the "flags" of each option, you should typically specify  
00066     // ### one of OptionBase::buildoption, OptionBase::learntoption or 
00067     // ### OptionBase::tuningoption. Another possible flag to be combined with
00068     // ### is OptionBase::nosave
00069 
00070     // ### ex:
00071     // declareOption(ol, "myoption", &LimitedGaussianSmoother::myoption, OptionBase::buildoption,
00072     //               "Help text describing this option");
00073     // ...
00074 
00075     // Now call the parent class' declareOptions
00076     inherited::declareOptions(ol);
00077 }
00078 
00079 void LimitedGaussianSmoother::build_()
00080 {
00081     // ### This method should do the real building of the object,
00082     // ### according to set 'options', in *any* situation. 
00083     // ### Typical situations include:
00084     // ###  - Initial building of an object from a few user-specified options
00085     // ###  - Building of a "reloaded" object: i.e. from the complete set of all serialised options.
00086     // ###  - Updating or "re-building" of an object after a few "tuning" options have been modified.
00087     // ### You should assume that the parent class' build_() has already been called.
00088 }
00089 
00090 // ### Nothing to add here, simply calls build_
00091 void LimitedGaussianSmoother::build()
00092 {
00093     inherited::build();
00094     build_();
00095 }
00096 
00097 
00098 void LimitedGaussianSmoother::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00099 {
00100     inherited::makeDeepCopyFromShallowCopy(copies);
00101 
00102     // ### Call deepCopyField on all "pointer-like" fields 
00103     // ### that you wish to be deepCopied rather than 
00104     // ### shallow-copied.
00105     // ### ex:
00106     // deepCopyField(trainvec, copies);
00107 
00108     // ### Remove this line when you have fully implemented this method.
00109     PLERROR("LimitedGaussianSmoother::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!");
00110 }
00111 
00112 
00113 real LimitedGaussianSmoother::smooth(const Vec& source_function, Vec& smoothed_function, 
00114                                      Vec bin_positions, Vec dest_bin_positions) const
00115 {
00116     //parzen regressor?? kernel smoothing??
00117 // smoothed_function[k] = sum_{j=max(0,k-window_size)}^{min(l-1,k+window_size)} w_{k,j} source_function[j]
00118 //                        / sum_{j=max(0,k-window_size)}^{min(l-1,k+window_size)} w_{k,j} 
00119 // with w_{k,j} = phi(bin_positions[j+1];mu_k,sigma_k)-phi(bin_positions[j];mu_k,sigma_k)
00120 // where mu_k = 0.5*(bin_positions[k+1]+bin_positions[k]),
00121 //       sigma_k = bin_positions[k+window_size]-bin_positions[k]
00122 // where phi(x;mu,sigma) = cdf of normal(mu,sigma) at x,
00123 // window_size = window_size_wrt_sigma * sigma_bin
00124 
00125 // for dest_bin_positions != bin_positions: 2 methods:
00126 // 1- trouver sigma_bin en fonction du voisinage
00127 //    d'une position dest.  Somme ponderee avec 
00128 //    gaussienne centree sur le pt. dest.
00129 // 2- Un sigma_bin pour chaque bin_position (src).
00130 //    Une gaussienne centree sur sur ch. pos src.
00131 //    
00132 
00133 
00134 
00135     smoothed_function.resize(source_function.length());
00136     smoothed_function.fill(0.0);
00137     real window_size= window_size_wrt_sigma * sigma_bin;
00138     for(int i= 0; i < smoothed_function.length()-1; ++i)
00139     {
00140         int min_j= i-static_cast<int>(window_size), max_j= i+static_cast<int>(window_size);
00141         if(min_j < 0) min_j= 0;
00142         if(max_j > smoothed_function.length()) max_j= smoothed_function.length();
00143         real sum_weights= 0.0;
00144         real mu= 0.5*(bin_positions[i+1]+bin_positions[i]),
00145             sigma= bin_positions[max_j-1]-bin_positions[i];
00146         for(int j= min_j; j < max_j-1; ++j)
00147         {
00148             sum_weights+= gauss_cum(bin_positions[j+1], mu, sigma) -
00149                 gauss_cum(bin_positions[j], mu, sigma);
00150         }
00151         for(int j= min_j; j < max_j-1; ++j)
00152             smoothed_function[i]+= ( gauss_cum(bin_positions[j+1], mu, sigma) 
00153                                      - gauss_cum(bin_positions[j], mu, sigma)
00154                 ) 
00155                 * source_function[j] / sum_weights;
00156     }
00157 
00158     return 0.0; //dummy - FIXME - xsm
00159 
00160 
00161 
00162 /*
00163   if(bin_positions.length() != 0 && source_function.length() != bin_positions.length()-1)
00164   PLERROR("in LimitedGaussianSmoother::smooth  There must be one more bin_positions than the "
00165   "number of source_function points.");
00166   //if no bin_positions given, assume positions are 0, 1, 2, ..., n
00167   if(bin_positions.length() == 0)
00168   {
00169   int n= source_function.length()+1;
00170   bin_positions.resize(n);
00171   for(int i= 0; i < n; ++i)
00172   bin_positions[i]= i;
00173   }
00174   //if no dest_bin_positions given, assume same as bin_positions
00175   if(dest_bin_positions.length() == 0)
00176   dest_bin_positions= bin_positions;
00177 
00178   smoothed_function.resize(dest_bin_positions.length()-1);
00179   smoothed_function.fill(0.0);
00180   real window_size, mu, sigma, sum_weights;
00181   int n= smoothed_function.length();
00182   for(int i= 0; i < n; ++i)
00183   {
00184   sum_weights= 0.0;
00185   int nj= source_function.length();
00186   for(int j= 0; j < nj; ++j)
00187   {
00188   mu= 0.5*(bin_positions[i+1]+bin_positions[i]);
00189   sigma= bin_positions[i+1]-bin_positions[i];
00190   window_size= window_size_wrt_sigma * sigma;
00191   real p1= mu - 0.5*window_size,
00192   p2= mu + 0.5*window_size;
00193 
00194   Vec::iterator it = find_if(options.begin(), options.end(),
00195   bind2nd(mem_fun(&OptionBase::isOptionNamed), optionname));
00196 
00197           
00198 
00199   }
00200       
00201 
00202 
00203   / *
00204   int min_j= i-static_cast<int>(window_size), max_j= i+static_cast<int>(window_size);
00205   if(min_j < 0) min_j= 0;
00206   if(max_j > smoothed_function.length()) max_j= smoothed_function.length();
00207   * /
00208   real sum_weights= 0.0;
00209   for(int j= min_j; j < max_j-1; ++j)
00210   {
00211   sum_weights+= gauss_cum(bin_positions[j+1], mu, sigma) -
00212   gauss_cum(bin_positions[j], mu, sigma);
00213   }
00214   for(int j= min_j; j < max_j-1; ++j)
00215   smoothed_function[i]+= ( gauss_cum(bin_positions[j+1], mu, sigma) 
00216   - gauss_cum(bin_positions[j], mu, sigma)
00217   ) 
00218   * source_function[j] / sum_weights;
00219   }
00220 */
00221 }
00222 
00223 } // end of namespace PLearn
00224 
00225 
00226 /*
00227   Local Variables:
00228   mode:c++
00229   c-basic-offset:4
00230   c-file-style:"stroustrup"
00231   c-file-offsets:((innamespace . 0)(inline-open . 0))
00232   indent-tabs-mode:nil
00233   fill-column:79
00234   End:
00235 */
00236 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines