PLearn 0.1
VMatrixFromDistribution.cc
Go to the documentation of this file.
00001 
00002 // -*- C++ -*-
00003 
00004 // VMatrixFromDistribution.cc
00005 //
00006 // Copyright (C) 2003  Pascal Vincent
00007 // Copyright (C) 2005  Olivier Delalleau
00008 //
00009 // Redistribution and use in source and binary forms, with or without
00010 // modification, are permitted provided that the following conditions are met:
00011 //
00012 //  1. Redistributions of source code must retain the above copyright
00013 //     notice, this list of conditions and the following disclaimer.
00014 //
00015 //  2. Redistributions in binary form must reproduce the above copyright
00016 //     notice, this list of conditions and the following disclaimer in the
00017 //     documentation and/or other materials provided with the distribution.
00018 //
00019 //  3. The name of the authors may not be used to endorse or promote
00020 //     products derived from this software without specific prior written
00021 //     permission.
00022 //
00023 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00024 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00025 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00026 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00028 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00029 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 //
00034 // This file is part of the PLearn library. For more information on the PLearn
00035 // library, go to the PLearn Web site at www.plearn.org
00036 
00037 /* *******************************************************
00038  * $Id: VMatrixFromDistribution.cc 5557 2006-05-10 20:36:58Z lamblin $
00039  ******************************************************* */
00040 
00042 #include "VMatrixFromDistribution.h"
00043 
00044 namespace PLearn {
00045 using namespace std;
00046 
00047 VMatrixFromDistribution::VMatrixFromDistribution()
00048     :mode("sample"), generator_seed(0), nsamples(0)
00049     /* ### Initialize all fields to their default value */
00050 {
00051     samples_per_dim = 10;
00052 }
00053 
00054 PLEARN_IMPLEMENT_OBJECT(VMatrixFromDistribution, "A VMatrix built from sampling a distribution",
00055                         "VMatrixFromDistribution implements a VMatrix whose data rows are drawn from a distribution\n"
00056                         "or that contains the density or log density sampled on a grid (depending on \"mode\").\n"
00057                         "The matrix is computed in memory at build time\n");
00058 
00059 void VMatrixFromDistribution::declareOptions(OptionList& ol)
00060 {
00061     declareOption(ol, "distr", &VMatrixFromDistribution::distr, OptionBase::buildoption,
00062                   "The distribution this matrix will be generated from\n");
00063 
00064     declareOption(ol, "mode", &VMatrixFromDistribution::mode, OptionBase::buildoption,
00065                   "mode can be one of:\n"
00066                   "   \"sample\" : will draw nsamples from the distribution initializing the generator with the generator_seed \n"
00067                   "   \"density\" : for 1d or 2d distributions, will report the density along a grid of samples_per_dim \n"
00068                   "                 gridpoints per dimension. The resulting matrix will contain rows of the form [ coordinates density ] \n"
00069                   "   \"log_density\" : same as density, but reports the log of the density instead. \n"
00070         );
00071 
00072     declareOption(ol, "generator_seed", &VMatrixFromDistribution::generator_seed, OptionBase::buildoption,
00073                   "The initial generator_seed to initialize the distribution's generator");
00074 
00075     declareOption(ol, "nsamples", &VMatrixFromDistribution::nsamples, OptionBase::buildoption,
00076                   "number of samples to draw");
00077 
00078     declareOption(ol, "samples_per_dim", &VMatrixFromDistribution::samples_per_dim, OptionBase::buildoption,
00079                   "number of samples on each dimensions of the grid");
00080 
00081     declareOption(ol, "mins", &VMatrixFromDistribution::mins, OptionBase::buildoption,
00082                   "the minimum of the grid on each dimensions");
00083 
00084     declareOption(ol, "maxs", &VMatrixFromDistribution::maxs, OptionBase::buildoption,
00085                   "the maximum of the grid on each dimensions");
00086 
00087     inherited::declareOptions(ol);
00088 }
00089 
00090 void VMatrixFromDistribution::build_()
00091 {
00092     if(distr)
00093     {
00094         if(mode=="sample")
00095         {
00096             length_ = nsamples;
00097             width_ = distr->getNPredicted();
00098             inputsize_ = width_;
00099             targetsize_ = 0;
00100             weightsize_ = 0;
00101             data.resize(length_, width_);
00102             distr->resetGenerator(generator_seed);
00103             distr->generateN(data);
00104         }
00105         else if(mode=="density" || mode=="log_density")
00106         {
00107             length_ = (int)pow(double(samples_per_dim),double(distr->inputsize()));
00108             width_ = distr->inputsize()+1;
00109             inputsize_ = distr->inputsize();
00110             targetsize_ = 0;
00111             weightsize_ = 1;
00112 
00113             data.resize(length_, width_);
00114             Vec v(data.width());
00115             int k=0;
00116             switch(distr->inputsize())
00117             {
00118             case 1:
00119                 for(int j=0;j<samples_per_dim;j++)
00120                 {
00121                     v[0] = mins[0] + ((real)j / (samples_per_dim-1)) * (maxs[0]-mins[0]);
00122                     if(mode=="density")
00123                         v[1] = distr->density(v.subVec(0,1));
00124                     else // log_density
00125                         v[1] = distr->log_density(v.subVec(0,1));
00126                     data(k++)<<v;
00127                 }
00128                 break;
00129             case 2:
00130                 for(int i=0;i<samples_per_dim;i++)
00131                 {
00132                     v[0] = mins[0] + ((real)i / (samples_per_dim-1)) * (maxs[0]-mins[0]);
00133                     for(int j=0;j<samples_per_dim;j++)
00134                     {
00135                         v[1] = mins[1] + ((real)j / (samples_per_dim-1)) * (maxs[1]-mins[1]);
00136                         if(mode=="density")
00137                             v[2] = distr->density(v.subVec(0,2));
00138                         else // log_density
00139                             v[2] = distr->log_density(v.subVec(0,2));
00140                         data(k++)<<v;
00141                     }
00142                 }
00143                 break;
00144             default:
00145                 PLERROR("density and log_density modes only supported for distribution of dimension 1 or 2");break;
00146             }
00147         }
00148         else
00149             PLERROR("In VMatrixFromDistribution: invalid mode: %s",mode.c_str());
00150     }
00151 }
00152 
00153 // ### Nothing to add here, simply calls build_
00154 void VMatrixFromDistribution::build()
00155 {
00156     inherited::build();
00157     build_();
00158 }
00159 
00160 void VMatrixFromDistribution::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00161 {
00162     inherited::makeDeepCopyFromShallowCopy(copies);
00163 }
00164 
00165 real VMatrixFromDistribution::get(int i, int j) const
00166 { return data(i,j); }
00167 
00168 void VMatrixFromDistribution::getColumn(int i, Vec v) const
00169 { v << data.column(i); }
00170 
00171 void VMatrixFromDistribution::getSubRow(int i, int j, Vec v) const
00172 { v << data(i).subVec(j,v.length()); }
00173 
00174 void VMatrixFromDistribution::getRow(int i, Vec v) const
00175 { v << data(i); }
00176 
00177 void VMatrixFromDistribution::getMat(int i, int j, Mat m) const
00178 { m << data.subMat(i,j,m.length(),m.width()); }
00179 
00180 Mat VMatrixFromDistribution::toMat() const
00181 { return data; }
00182 
00183 } // end of namespace PLearn
00184 
00185 
00186 /*
00187   Local Variables:
00188   mode:c++
00189   c-basic-offset:4
00190   c-file-style:"stroustrup"
00191   c-file-offsets:((innamespace . 0)(inline-open . 0))
00192   indent-tabs-mode:nil
00193   fill-column:79
00194   End:
00195 */
00196 // 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