PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // UniformDistribution.cc 00004 // 00005 // Copyright (C) 2004 Olivier Delalleau 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: UniformDistribution.cc 8496 2008-02-13 14:19:23Z tihocan $ 00037 ******************************************************* */ 00038 00039 // Authors: Olivier Delalleau 00040 00043 #include "UniformDistribution.h" 00044 00045 namespace PLearn { 00046 using namespace std; 00047 00049 // UniformDistribution // 00051 UniformDistribution::UniformDistribution(): 00052 counter(0), 00053 mesh_size(-1), 00054 n_dim(-1) 00055 { 00056 // Default = generate points uniformly in [0,1]. 00057 min.resize(1); 00058 max.resize(1); 00059 min[0] = 0; 00060 max[0] = 1; 00061 } 00062 00063 PLEARN_IMPLEMENT_OBJECT(UniformDistribution, 00064 "Implements uniform distribution over intervals.", 00065 "Currently, only very few methods are implemented.\n" 00066 "For example, to sample points in 2D in [a,b] x [c,d], use\n" 00067 " min = [a c]\n" 00068 " max = [b d]\n" 00069 ); 00070 00072 // declareOptions // 00074 void UniformDistribution::declareOptions(OptionList& ol) 00075 { 00076 declareOption(ol, "min", &UniformDistribution::min, OptionBase::buildoption, 00077 "The inferior bound for all intervals."); 00078 00079 declareOption(ol, "max", &UniformDistribution::max, OptionBase::buildoption, 00080 "The superior bound for all intervals."); 00081 00082 declareOption(ol, "mesh_size", &UniformDistribution::mesh_size, OptionBase::buildoption, 00083 "If set to a value > 0, this distribution will generate points deterministically\n" 00084 "so as to form a mesh of 'mesh_size'^d points equally spaced."); 00085 00086 declareOption(ol, "n_dim", &UniformDistribution::n_dim, 00087 OptionBase::buildoption, 00088 "Optionally, the number of dimensions. Provide this option only if\n" 00089 "you want to generate 'n_dim' dimensions all in [min,max] (in which\n" 00090 "case the length of 'min' and 'max' should be 1)."); 00091 00092 declareOption(ol, "counter", &UniformDistribution::counter, OptionBase::learntoption, 00093 "Counts the number of points generated (necessary when 'mesh_size' is used)."); 00094 00095 // Now call the parent class' declareOptions(). 00096 inherited::declareOptions(ol); 00097 } 00098 00100 // build // 00102 void UniformDistribution::build() 00103 { 00104 inherited::build(); 00105 build_(); 00106 } 00107 00109 // build_ // 00111 void UniformDistribution::build_() 00112 { 00113 // ### This method should do the real building of the object, 00114 // ### according to set 'options', in *any* situation. 00115 // ### Typical situations include: 00116 // ### - Initial building of an object from a few user-specified options 00117 // ### - Building of a "reloaded" object: i.e. from the complete set of all serialised options. 00118 // ### - Updating or "re-building" of an object after a few "tuning" options have been modified. 00119 // ### You should assume that the parent class' build_() has already been called. 00120 00121 // Check consistency of intervals. 00122 if (min.length() != max.length()) { 00123 PLERROR("In UniformDistribution::build_ - 'min' and 'max' should have the same size"); 00124 } 00125 00126 if (n_dim == -1) 00127 n_dim = min.length(); 00128 00129 if (n_dim != min.length()) { 00130 if (min.length() == 1) { 00131 real min_val = min[0]; 00132 real max_val = max[0]; 00133 for (int i = 0; i < n_dim - 1; i++) { 00134 min.append(min_val); 00135 max.append(max_val); 00136 } 00137 } else 00138 PLERROR("In UniformDistribution::build_ - The value of 'n_dim' " 00139 "does not match the length of the 'min' vector"); 00140 } 00141 PLASSERT( n_dim == min.length() ); 00142 for (int i = 0; i < n_dim; i++) { 00143 if (min[i] > max[i]) { 00144 PLERROR("In UniformDistribution::build_ - 'min' should be always <= 'max'"); 00145 } 00146 } 00147 inputsize_ = n_dim; 00148 // We need to re-build the parent classes, because the inputsize has 00149 // changed. 00150 inherited::build(); 00151 } 00152 00154 // cdf // 00156 real UniformDistribution::cdf(const Vec& x) const 00157 { 00158 PLERROR("cdf not implemented for UniformDistribution"); return 0; 00159 } 00160 00162 // expectation // 00164 void UniformDistribution::expectation(Vec& mu) const 00165 { 00166 PLERROR("expectation not implemented for UniformDistribution"); 00167 } 00168 00170 // generate // 00172 void UniformDistribution::generate(Vec& x) const 00173 { 00174 x.resize(n_dim); 00175 if (mesh_size > 0) { 00176 int val = counter; 00177 int coord; 00178 for (int i = 0; i < n_dim; i++) { 00179 coord = val % mesh_size; 00180 val /= mesh_size; 00181 x[i] = min[i] + (max[i] - min[i]) * coord / real(mesh_size) + (max[i] - min[i]) / (2 * real(mesh_size)); 00182 } 00183 counter++; 00184 } else { 00185 for (int i = 0; i < n_dim; i++) { 00186 x[i] = random_gen->bounded_uniform(min[i], max[i]); 00187 } 00188 } 00189 } 00190 00192 // log_density // 00194 real UniformDistribution::log_density(const Vec& x) const 00195 { 00196 real sum = 0; 00197 for (int i = 0; i < n_dim; i++) { 00198 if (x[i] > max[i] || x[i] < min[i]) 00199 return -INFINITY; 00200 sum += pl_log(max[i] - min[i]); 00201 } 00202 return -sum; 00203 } 00204 00206 // makeDeepCopyFromShallowCopy // 00208 void UniformDistribution::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00209 { 00210 inherited::makeDeepCopyFromShallowCopy(copies); 00211 00212 // ### Call deepCopyField on all "pointer-like" fields 00213 // ### that you wish to be deepCopied rather than 00214 // ### shallow-copied. 00215 // ### ex: 00216 // deepCopyField(trainvec, copies); 00217 00218 // ### Remove this line when you have fully implemented this method. 00219 PLERROR("UniformDistribution::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); 00220 } 00221 00223 // resetGenerator // 00225 void UniformDistribution::resetGenerator(long g_seed) 00226 { 00227 inherited::resetGenerator(g_seed); 00228 counter = 0; 00229 } 00230 00232 // survival_fn // 00234 real UniformDistribution::survival_fn(const Vec& x) const 00235 { 00236 PLERROR("survival_fn not implemented for UniformDistribution"); return 0; 00237 } 00238 00240 // variance // 00242 void UniformDistribution::variance(Mat& covar) const 00243 { 00244 PLERROR("variance not implemented for UniformDistribution"); 00245 } 00246 00247 } // end of namespace PLearn 00248 00249 00250 /* 00251 Local Variables: 00252 mode:c++ 00253 c-basic-offset:4 00254 c-file-style:"stroustrup" 00255 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00256 indent-tabs-mode:nil 00257 fill-column:79 00258 End: 00259 */ 00260 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :