PLearn 0.1
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 5687 2006-05-26 17:27:54Z plearner $
00038  ******************************************************* */
00039 
00041 #include "SpiralDistribution.h"
00042 
00043 namespace PLearn {
00044 using namespace std;
00045 
00046 SpiralDistribution::SpiralDistribution()
00047     : lambda(0.04),
00048       alpha(1),
00049       tmin(3),
00050       tmax(15),
00051       sigma(0.01),
00052       uniformity(1),
00053       include_t(false)
00054 {
00055     // build_();
00056 }
00057 
00058 PLEARN_IMPLEMENT_OBJECT(SpiralDistribution, "Generates samples drawn from a 2D spiral",
00059                         "SpiralDistribution is a generative model that generates 2D (x,y) samples in the following manner:\n"
00060                         " t ~ uniform([tmin, tmax])^uniformity \n"
00061                         " x = lambda*t*sin(alpha*t) + N(0,sigma) \n"
00062                         " y = lambda*t*cos(alpha*t) + N(0,sigma) \n");
00063 
00064 void SpiralDistribution::declareOptions(OptionList& ol)
00065 {
00066     declareOption(ol, "lambda", &SpiralDistribution::lambda, OptionBase::buildoption,"");
00067     declareOption(ol, "alpha", &SpiralDistribution::alpha, OptionBase::buildoption,"");
00068     declareOption(ol, "tmin", &SpiralDistribution::tmin, OptionBase::buildoption,"");
00069     declareOption(ol, "tmax", &SpiralDistribution::tmax, OptionBase::buildoption,"");
00070     declareOption(ol, "sigma", &SpiralDistribution::sigma, OptionBase::buildoption,"");
00071     declareOption(ol, "uniformity", &SpiralDistribution::uniformity, OptionBase::buildoption,"");
00072     declareOption(ol, "include_t", &SpiralDistribution::include_t, OptionBase::buildoption,
00073                   "If true, then t will be appended to the generated sample, along with x and y.");
00074 
00075     inherited::declareOptions(ol);
00076 }
00077 
00078 void SpiralDistribution::build_()
00079 {
00080     // ### This method should do the real building of the object,
00081     // ### according to set 'options', in *any* situation.
00082     // ### Typical situations include:
00083     // ###  - Initial building of an object from a few user-specified options
00084     // ###  - Building of a "reloaded" object: i.e. from the complete set of all serialised options.
00085     // ###  - Updating or "re-building" of an object after a few "tuning" options have been modified.
00086     // ### You should assume that the parent class' build_() has already been called.
00087     
00088     // annoying stuff due to conditional distribution (silly, fix this some day)
00089     predicted_size = inputsize();
00090     inherited::build();
00091 }
00092 
00093 // ### Nothing to add here, simply calls build_
00094 void SpiralDistribution::build()
00095 {
00096     inherited::build();
00097     build_();
00098 }
00099 
00100 void SpiralDistribution::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00101 {
00102     inherited::makeDeepCopyFromShallowCopy(copies);
00103 
00104     // ### Call deepCopyField on all "pointer-like" fields
00105     // ### that you wish to be deepCopied rather than
00106     // ### shallow-copied.
00107     // ### ex:
00108     // deepCopyField(trainvec, copies);
00109 
00110     // ### Remove this line when you have fully implemented this method.
00111     PLERROR("SpiralDistribution::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!");
00112 }
00113 
00114 real SpiralDistribution::log_density(const Vec& x) const
00115 { PLERROR("density not implemented for SpiralDistribution"); return 0; }
00116 
00117 real SpiralDistribution::survival_fn(const Vec& x) const
00118 { PLERROR("survival_fn not implemented for SpiralDistribution"); return 0; }
00119 
00120 real SpiralDistribution::cdf(const Vec& x) const
00121 { PLERROR("cdf not implemented for SpiralDistribution"); return 0; }
00122 
00123 void SpiralDistribution::expectation(Vec& mu) const
00124 { PLERROR("expectation not implemented for SpiralDistribution"); }
00125 
00126 void SpiralDistribution::variance(Mat& covar) const
00127 { PLERROR("variance not implemented for SpiralDistribution"); }
00128 
00129 void SpiralDistribution::curve(real t, real& x, real& y) const
00130 {
00131     x = lambda*t*sin(alpha*t);
00132     y = lambda*t*cos(alpha*t);
00133 }
00134 
00135 void SpiralDistribution::generate(Vec& v) const
00136 {
00137     v.resize(inputsize());
00138 
00139     real x, y;
00140     real u =  random_gen->bounded_uniform(0,1);
00141     real t = (fast_is_equal(uniformity, 1))?u:pow(u,uniformity);
00142     t = tmin+(tmax-tmin)*t;
00143     curve(t,x,y);
00144     x += random_gen->gaussian_mu_sigma(0, sigma);
00145     y += random_gen->gaussian_mu_sigma(0, sigma);
00146 
00147     v[0] = x;
00148     v[1] = y;
00149     if(inputsize()==3)
00150         v[2] = t;
00151 }
00152 
00153 
00154 // Default version of inputsize returns learner->inputsize()
00155 // If this is not appropriate, you should uncomment this and define
00156 // it properly in the .cc
00157 int SpiralDistribution::inputsize() const
00158 { return include_t ?3 :2; }
00159 
00160 } // end of namespace PLearn
00161 
00162 
00163 /*
00164   Local Variables:
00165   mode:c++
00166   c-basic-offset:4
00167   c-file-style:"stroustrup"
00168   c-file-offsets:((innamespace . 0)(inline-open . 0))
00169   indent-tabs-mode:nil
00170   fill-column:79
00171   End:
00172 */
00173 // 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