Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

VMatrixFromDistribution.cc

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

Generated on Tue Aug 17 16:10:48 2004 for PLearn by doxygen 1.3.7