PLearn 0.1
PConditionalDistribution.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PConditionalDistribution.cc
00004 // 
00005 // Copyright (C) 2004 Université de Montréal
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 #include "PConditionalDistribution.h"
00036 
00037 namespace PLearn {
00038 using namespace std;
00039 
00040 PConditionalDistribution::PConditionalDistribution() 
00041     :PDistribution(), input_part_size(-1)
00042 {
00043    
00044 }
00045 
00046 PLEARN_IMPLEMENT_OBJECT(PConditionalDistribution, 
00047                         "(THIS CLASS IS DEPRECATED, use PDistribution instead). Conditional distribution or conditional density model P(Y|X)",
00048                         "Abstract superclass for conditional distribution classes.\n"
00049                         "It is a subclass of PDistribution, with the added method\n"
00050                         "   setInput(Vec& input)\n"
00051                         "to set X, that must be called before PDistribution methods such as\n"
00052                         "log_density,cdf,survival_fn,expectation,variance,generate.\n"
00053                         "The PDistribution option output_defs must be set to specify\n"
00054                         "what the PLearner method computeOutput will produce. If it is\n"
00055                         "set to 'l' (log_density), 'd' (density), 'c' (cdf), or 's' (survival_fn)\n"
00056                         "then the input part of the data should contain both the input X and\n"
00057                         "the 'target' Y values (targetsize()==0). Instead, if output_defs is set to\n"
00058                         " 'e' (expectation) or 'v' (variance), then the input part of the data should\n"
00059                         "contain only X, while the target part should contain Y\n");
00060 
00061 void PConditionalDistribution::declareOptions(OptionList& ol)
00062 {
00063     declareOption(ol, "input_part_size", &PConditionalDistribution::input_part_size, OptionBase::buildoption,
00064                   "This option should be used only if outputs_def is 'l','d','c' or 's' (or upper case),\n"
00065                   "which is when computeOutput takes as input both the X and Y parts to compute P(Y|X).\n"
00066                   "This option gives the size of X, that is the length of the part of the data input which\n"
00067                   "contains the conditioning part of the distribution. The rest of the data input vector should\n"
00068                   "contain the Y value. If outputs_def is 'e' or 'v' or upper case then this option is ignored.\n");
00069     inherited::declareOptions(ol);
00070 }
00071 
00072 void PConditionalDistribution::build_()
00073 {
00074     if (train_set)
00075     {
00076         if (outputs_def=="L" || outputs_def=="D" || outputs_def=="C" || outputs_def=="S" || outputs_def=="e" || outputs_def=="v")
00077             input_part_size = train_set->inputsize();
00078     }
00079 }
00080 
00081 // ### Nothing to add here, simply calls build_
00082 void PConditionalDistribution::build()
00083 {
00084     inherited::build();
00085     build_();
00086 }
00087 
00088 void PConditionalDistribution::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00089 {
00090     inherited::makeDeepCopyFromShallowCopy(copies);
00091 }
00092 
00093 void PConditionalDistribution::setInput(const Vec& input) const
00094 { PLERROR("setInput must be implemented for this PConditionalDistribution"); }
00095 
00096 
00097 void PConditionalDistribution::computeOutput(const Vec& input, Vec& output) const
00098 {
00099     Vec x = input.subVec(0,input_part_size);
00100     int d=input.length()-input_part_size;
00101     Vec y = input.subVec(input_part_size,d);
00102     setInput(x);
00103     if (outputs_def=="l")
00104         output[0]=log_density(y);
00105     else if (outputs_def=="d")
00106         output[0]=density(y);
00107     else if (outputs_def=="c")
00108         output[0]=cdf(y);
00109     else if (outputs_def=="s")
00110         output[0]=survival_fn(y);
00111     else if (outputs_def=="e")
00112         expectation(output);
00113     else if (outputs_def=="v")
00114     {
00115         Mat covmat = output.toMat(d,d);
00116         variance(covmat);
00117     }
00118     else if (outputs_def=="L")
00119     {
00120         real lower = lower_bound;
00121         real upper = upper_bound;
00122         real delta = (upper - lower)/n_curve_points;
00123         Vec y(1); y[0]=lower;
00124         for (int i=0;i<n_curve_points;i++)
00125         {
00126             output[i] = log_density(y);
00127             y[0]+=delta;
00128         }
00129     }
00130     else if (outputs_def=="D")
00131     {
00132         real lower = lower_bound;
00133         real upper = upper_bound;
00134         real delta = (upper - lower)/n_curve_points;
00135         Vec y(1); y[0]=lower;
00136         for (int i=0;i<n_curve_points;i++)
00137         {
00138             output[i] = density(y);
00139             y[0]+=delta;
00140         }
00141     }
00142     else if (outputs_def=="C")
00143     {
00144         real lower = lower_bound;
00145         real upper = upper_bound;
00146         real delta = (upper - lower)/n_curve_points;
00147         Vec y(1); y[0]=lower;
00148         for (int i=0;i<n_curve_points;i++)
00149         {
00150             output[i] = cdf(y);
00151             y[0]+=delta;
00152         }
00153     }
00154     else if (outputs_def=="S")
00155     {
00156         real lower = lower_bound;
00157         real upper = upper_bound;
00158         real delta = (upper - lower)/n_curve_points;
00159         Vec y(1); y[0]=lower;
00160         for (int i=0;i<n_curve_points;i++)
00161         {
00162             output[i] = survival_fn(y);
00163             y[0]+=delta;
00164         }
00165     }
00166     else PLERROR("PConditionalDistribution: unknown setting of outputs_def = %s",outputs_def.c_str());
00167 }
00168 
00169 } // end of namespace PLearn
00170 
00171 
00172 /*
00173   Local Variables:
00174   mode:c++
00175   c-basic-offset:4
00176   c-file-style:"stroustrup"
00177   c-file-offsets:((innamespace . 0)(inline-open . 0))
00178   indent-tabs-mode:nil
00179   fill-column:79
00180   End:
00181 */
00182 // 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