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

SpiralDistribution.cc

Go to the documentation of this file.
00001 00002 // -*- C++ -*- 00003 00004 // SpiralDistribution.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: SpiralDistribution.cc,v 1.10 2004/07/21 16:30:56 chrish42 Exp $ 00038 ******************************************************* */ 00039 00041 #include "SpiralDistribution.h" 00042 #include <plearn/math/random.h> 00043 00044 namespace PLearn { 00045 using namespace std; 00046 00047 SpiralDistribution::SpiralDistribution() 00048 : lambda(0.04), 00049 alpha(1), 00050 tmin(3), 00051 tmax(15), 00052 sigma(0.01), 00053 uniformity(1), 00054 include_t(false) 00055 { 00056 // build_(); 00057 } 00058 00059 PLEARN_IMPLEMENT_OBJECT(SpiralDistribution, "Generates samples drawn from a 2D spiral", 00060 "SpiralDistribution is a generative model that generates 2D (x,y) samples in the following manner:\n" 00061 " t ~ uniform([tmin, tmax])^uniformity \n" 00062 " x = lambda*t*sin(alpha*t) + N(0,sigma) \n" 00063 " y = lambda*t*cos(alpha*t) + N(0,sigma) \n"); 00064 00065 void SpiralDistribution::declareOptions(OptionList& ol) 00066 { 00067 declareOption(ol, "lambda", &SpiralDistribution::lambda, OptionBase::buildoption,""); 00068 declareOption(ol, "alpha", &SpiralDistribution::alpha, OptionBase::buildoption,""); 00069 declareOption(ol, "tmin", &SpiralDistribution::tmin, OptionBase::buildoption,""); 00070 declareOption(ol, "tmax", &SpiralDistribution::tmax, OptionBase::buildoption,""); 00071 declareOption(ol, "sigma", &SpiralDistribution::sigma, OptionBase::buildoption,""); 00072 declareOption(ol, "uniformity", &SpiralDistribution::uniformity, OptionBase::buildoption,""); 00073 declareOption(ol, "include_t", &SpiralDistribution::include_t, OptionBase::buildoption, 00074 "If true, then t will be appended to the generated sample, along with x and y."); 00075 00076 inherited::declareOptions(ol); 00077 } 00078 00079 void SpiralDistribution::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 SpiralDistribution::build() 00092 { 00093 inherited::build(); 00094 build_(); 00095 } 00096 00097 void SpiralDistribution::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00098 { 00099 inherited::makeDeepCopyFromShallowCopy(copies); 00100 00101 // ### Call deepCopyField on all "pointer-like" fields 00102 // ### that you wish to be deepCopied rather than 00103 // ### shallow-copied. 00104 // ### ex: 00105 // deepCopyField(trainvec, copies); 00106 00107 // ### Remove this line when you have fully implemented this method. 00108 PLERROR("SpiralDistribution::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); 00109 } 00110 00111 real SpiralDistribution::log_density(const Vec& x) const 00112 { PLERROR("density not implemented for SpiralDistribution"); return 0; } 00113 00114 real SpiralDistribution::survival_fn(const Vec& x) const 00115 { PLERROR("survival_fn not implemented for SpiralDistribution"); return 0; } 00116 00117 real SpiralDistribution::cdf(const Vec& x) const 00118 { PLERROR("cdf not implemented for SpiralDistribution"); return 0; } 00119 00120 void SpiralDistribution::expectation(Vec& mu) const 00121 { PLERROR("expectation not implemented for SpiralDistribution"); } 00122 00123 void SpiralDistribution::variance(Mat& covar) const 00124 { PLERROR("variance not implemented for SpiralDistribution"); } 00125 00126 void SpiralDistribution::curve(real t, real& x, real& y) const 00127 { 00128 x = lambda*t*sin(alpha*t); 00129 y = lambda*t*cos(alpha*t); 00130 } 00131 00132 void SpiralDistribution::generate(Vec& v) const 00133 { 00134 v.resize(inputsize()); 00135 00136 real x, y; 00137 real u = bounded_uniform(0,1); 00138 real t = (uniformity==1)?u:pow(u,uniformity); 00139 t = tmin+(tmax-tmin)*t; 00140 curve(t,x,y); 00141 x += gaussian_mu_sigma(0, sigma); 00142 y += gaussian_mu_sigma(0, sigma); 00143 00144 v[0] = x; 00145 v[1] = y; 00146 if(inputsize()==3) 00147 v[2] = t; 00148 } 00149 00150 00151 // Default version of inputsize returns learner->inputsize() 00152 // If this is not appropriate, you should uncomment this and define 00153 // it properly in the .cc 00154 int SpiralDistribution::inputsize() const 00155 { return include_t ?3 :2; } 00156 00157 void SpiralDistribution::resetGenerator(long g_seed) const 00158 { 00159 manual_seed(g_seed); 00160 } 00161 00162 00163 } // end of namespace PLearn

Generated on Tue Aug 17 16:06:38 2004 for PLearn by doxygen 1.3.7