00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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
00082 void PConditionalDistribution::build()
00083 {
00084 inherited::build();
00085
build_();
00086 }
00087
00088 void PConditionalDistribution::makeDeepCopyFromShallowCopy(map<const void*, void*>& 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 }