PLearn 0.1
|
#include <HistogramDistribution.h>
Public Types | |
typedef PDistribution | inherited |
Public Member Functions | |
HistogramDistribution () | |
HistogramDistribution (VMat data, PP< Binner > binner_=0, PP< Smoother > smoother_=0) | |
This constructor calls train as part of the construction process. | |
virtual void | build () |
Simply calls inherited::build() then build_(). | |
virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
Transforms a shallow copy into a deep copy. | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual HistogramDistribution * | deepCopy (CopiesMap &copies) const |
virtual void | train () |
trains the model | |
virtual void | computeOutput (const Vec &input, Vec &output) |
computes the output of a trained model | |
virtual real | log_density (const Vec &x) const |
return log of probability density log(p(x)) | |
virtual real | density (const Vec &x) const |
return probability density p(x) [ default version returns exp(log_density(x)) ] | |
virtual real | survival_fn (const Vec &x) const |
return survival fn = P(X>x) | |
virtual real | cdf (const Vec &x) const |
return survival fn = P(X<x) | |
virtual void | expectation (Vec &mu) const |
return E[X] | |
virtual void | variance (Mat &cov) const |
return Var[X] | |
virtual real | prob_in_range (const Vec &x0, const Vec &x1) const |
return P(x0 < X < x1) | |
int | find_bin (real x) const |
Find the bin where x belongs; -1 if x is out of range. | |
void | calc_density_from_survival () |
calculate bin_density from survival_values | |
void | calc_survival_from_density () |
calculate survival_values from bin_density | |
Static Public Member Functions | |
static string | _classname_ () |
Declares name and deepCopy methods. | |
static OptionList & | _getOptionList_ () |
static RemoteMethodMap & | _getRemoteMethodMap_ () |
static Object * | _new_instance_for_typemap_ () |
static bool | _isa_ (const Object *o) |
static void | _static_initialize_ () |
static const PPath & | declaringFile () |
static void | calc_density_from_survival (const Vec &survival, Vec &density_, const Vec &positions) |
calculate density from survival - static, on 2 Vecs | |
static void | calc_survival_from_density (const Vec &density_, Vec &survival, const Vec &positions) |
calculate survival from density - static, on 2 Vecs | |
Public Attributes | |
Vec | bin_positions |
there is one more bin position than number of bins, all the bins are supposed adjacent | |
Vec | bin_density |
the density is supposed constant within each bin: p(x) = bin_density[i] if bin_positions[i] < x <= bin_positions[i+1] | |
Vec | survival_values |
redundant with density is the pre-computed (optional) survival fn | |
PP< Binner > | binner |
this Binner is used to do binning at training time. | |
PP< Smoother > | smoother |
this Smoother is used at training time. | |
bool | smooth_density_instead_of_survival_fn |
whether to smooth the density or the survival function | |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Static Protected Member Functions | |
static void | declareOptions (OptionList &ol) |
Declares this class' options. | |
Private Member Functions | |
void | build_ () |
This does the actual building. |
Definition at line 50 of file HistogramDistribution.h.
Reimplemented from PLearn::PDistribution.
Definition at line 77 of file HistogramDistribution.h.
PLearn::HistogramDistribution::HistogramDistribution | ( | ) |
Definition at line 47 of file HistogramDistribution.cc.
{}
PLearn::HistogramDistribution::HistogramDistribution | ( | VMat | data, |
PP< Binner > | binner_ = 0 , |
||
PP< Smoother > | smoother_ = 0 |
||
) |
This constructor calls train as part of the construction process.
The computeOutput function can then be used right away.
Definition at line 49 of file HistogramDistribution.cc.
References PLearn::PLearner::setTrainingSet(), and train().
:bin_positions(data.length()+1), bin_density(data.length()), survival_values(data.length()), binner(binner_), smoother(smoother_) { setTrainingSet(data); train(); }
string PLearn::HistogramDistribution::_classname_ | ( | ) | [static] |
Declares name and deepCopy methods.
Reimplemented from PLearn::PDistribution.
Definition at line 65 of file HistogramDistribution.cc.
OptionList & PLearn::HistogramDistribution::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::PDistribution.
Definition at line 65 of file HistogramDistribution.cc.
RemoteMethodMap & PLearn::HistogramDistribution::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::PDistribution.
Definition at line 65 of file HistogramDistribution.cc.
Reimplemented from PLearn::PDistribution.
Definition at line 65 of file HistogramDistribution.cc.
Object * PLearn::HistogramDistribution::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::PDistribution.
Definition at line 65 of file HistogramDistribution.cc.
StaticInitializer HistogramDistribution::_static_initializer_ & PLearn::HistogramDistribution::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::PDistribution.
Definition at line 65 of file HistogramDistribution.cc.
void PLearn::HistogramDistribution::build | ( | ) | [virtual] |
Simply calls inherited::build() then build_().
Reimplemented from PLearn::PDistribution.
Definition at line 102 of file HistogramDistribution.cc.
References PLearn::PDistribution::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::HistogramDistribution::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::PDistribution.
Definition at line 97 of file HistogramDistribution.cc.
Referenced by build().
{ }
void PLearn::HistogramDistribution::calc_density_from_survival | ( | ) |
calculate bin_density from survival_values
Definition at line 288 of file HistogramDistribution.cc.
References bin_density, bin_positions, and survival_values.
Referenced by train().
{ calc_density_from_survival(survival_values, bin_density, bin_positions); /* int n= bin_positions.length()-1; bin_density.resize(n); real sum= 0.0; for(int i= 0; i < n; ++i) if(bin_positions[i+1] != bin_positions[i]) if(i == n-1) sum+= (bin_density[i]= survival_values[i] / (bin_positions[i+1]-bin_positions[i])); else sum+= (bin_density[i]= (survival_values[i] - survival_values[i+1]) / (bin_positions[i+1]-bin_positions[i])); else bin_density[i]= 0.0; */ }
void PLearn::HistogramDistribution::calc_density_from_survival | ( | const Vec & | survival, |
Vec & | density_, | ||
const Vec & | positions | ||
) | [static] |
calculate density from survival - static, on 2 Vecs
Definition at line 321 of file HistogramDistribution.cc.
References i, PLearn::TVec< T >::length(), n, PLearn::TVec< T >::resize(), and PLearn::sum().
{ int n= positions.length()-1; density_.resize(n); real sum= 0.0; for(int i= 0; i < n; ++i) if(positions[i+1] != positions[i]) if(i == n-1) sum+= (density_[i]= survival[i] / (positions[i+1]-positions[i])); else sum+= (density_[i]= (survival[i] - survival[i+1]) / (positions[i+1]-positions[i])); else density_[i]= 0.0; }
void PLearn::HistogramDistribution::calc_survival_from_density | ( | ) |
calculate survival_values from bin_density
Definition at line 307 of file HistogramDistribution.cc.
References bin_density, bin_positions, and survival_values.
Referenced by train().
{ calc_survival_from_density(bin_density, survival_values, bin_positions); /* int n= bin_positions.length()-1; survival_values.resize(n); real prec= 0.0; for(int i= n-1; i >= 0; --i) prec= survival_values[i]= bin_density[i]*(bin_positions[i+1]-bin_positions[i]) + prec; for(int i= 0; i < n; ++i) survival_values[i]/= prec; */ }
void PLearn::HistogramDistribution::calc_survival_from_density | ( | const Vec & | density_, |
Vec & | survival, | ||
const Vec & | positions | ||
) | [static] |
calculate survival from density - static, on 2 Vecs
Definition at line 336 of file HistogramDistribution.cc.
References i, PLearn::TVec< T >::length(), n, and PLearn::TVec< T >::resize().
{ int n= positions.length()-1; survival.resize(n); real prec= 0.0; for(int i= n-1; i >= 0; --i) prec= survival[i]= density_[i]*(positions[i+1]-positions[i]) + prec; for(int i= 0; i < n; ++i) survival[i]/= prec; }
return survival fn = P(X<x)
Reimplemented from PLearn::PDistribution.
Definition at line 231 of file HistogramDistribution.cc.
References survival_fn().
Referenced by computeOutput().
{ return 1.0-survival_fn(x); }
string PLearn::HistogramDistribution::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::PDistribution.
Definition at line 65 of file HistogramDistribution.cc.
computes the output of a trained model
Definition at line 173 of file HistogramDistribution.cc.
References cdf(), density(), expectation(), log_density(), m, PLearn::PDistribution::outputs_def, PLERROR, PLearn::TVec< T >::size(), survival_fn(), and variance().
{ if(input.size() != 1 || output.size() != 1) PLERROR("In HistogramDistribution::use implemented only for reals; i.e. input.size()=output.size()=1. " "Got input.size()=%d and output.size()=%d", input.size(), output.size()); // outputs_def: 'l'->log_density, 'd' -> density, 'c' -> cdf, 's' -> survival_fn, 'e' -> expectation, 'v' -> variance if(outputs_def == "l") output[0]= log_density(input); else if(outputs_def == "d") output[0]= density(input); else if(outputs_def == "c") output[0]= cdf(input); else if(outputs_def == "s") output[0]= survival_fn(input); else if(outputs_def == "e") { Vec mu(1); expectation(mu); output[0]= mu[0]; } else if(outputs_def == "v") { Mat m(1,1); variance(m); output[0]= m(0,0); } else PLERROR("In HistogramDistribution::use unknown value for outputs_def= \"%s\"", outputs_def.c_str()); }
void PLearn::HistogramDistribution::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares this class' options.
Reimplemented from PLearn::PDistribution.
Definition at line 67 of file HistogramDistribution.cc.
References bin_density, bin_positions, binner, PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::PDistribution::declareOptions(), PLearn::OptionBase::learntoption, smooth_density_instead_of_survival_fn, smoother, and survival_values.
{ declareOption(ol, "bin_positions", &HistogramDistribution::bin_positions, OptionBase::learntoption, "The n+1 positions that define n bins. There is one more bin position " "than number of bins, all the bins are supposed adjacent."); declareOption(ol, "bin_density", &HistogramDistribution::bin_density, OptionBase::learntoption, "Density of the distribution for each bin. The density is supposed " "constant within each bin:\n" "\t p(x) = bin_density[i] if bin_positions[i] < x <= bin_positions[i+1]."); declareOption(ol, "survival_values", &HistogramDistribution::survival_values, OptionBase::learntoption, "Redundant with density is the pre-computed survival function."); declareOption(ol, "binner", &HistogramDistribution::binner, OptionBase::buildoption, "Used to do binning at training time (although a fixed binning scheme can be\n" "obtained by using a ManualBinner.B)"); declareOption(ol, "smoother", &HistogramDistribution::smoother, OptionBase::buildoption, "Used to smooth learned density (or survival) at train time, after the empirical\n" "frequencies of each bin have been collected\n"); declareOption(ol, "smooth_density_instead_of_survival_fn", &HistogramDistribution::smooth_density_instead_of_survival_fn, OptionBase::buildoption, "whether to smooth the density or the survival function, with the smoother\n"); // Now call the parent class' declareOptions inherited::declareOptions(ol); }
static const PPath& PLearn::HistogramDistribution::declaringFile | ( | ) | [inline, static] |
HistogramDistribution * PLearn::HistogramDistribution::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::PDistribution.
Definition at line 65 of file HistogramDistribution.cc.
return probability density p(x) [ default version returns exp(log_density(x)) ]
Reimplemented from PLearn::PDistribution.
Definition at line 205 of file HistogramDistribution.cc.
References bin_density, find_bin(), PLERROR, and PLearn::TVec< T >::size().
Referenced by computeOutput(), and log_density().
{ if(x.size() != 1) PLERROR("HistogramDistribution::density implemented only for univariate data (vec size == 1)."); return bin_density[find_bin(x[0])]; }
void PLearn::HistogramDistribution::expectation | ( | Vec & | mu | ) | const [virtual] |
return E[X]
Reimplemented from PLearn::PDistribution.
Definition at line 236 of file HistogramDistribution.cc.
References bin_density, bin_positions, i, PLERROR, PLearn::TVec< T >::size(), and PLearn::sum().
Referenced by computeOutput().
{ if(mu.size() != 1) PLERROR("HistogramDistribution::expectation implemented only for univariate data (vec size == 1)."); real sum= 0.0; for(int i= 0; i < bin_density.size(); ++i) sum+= bin_density[i] * (bin_positions[i+1]-bin_positions[i]) * (bin_positions[i]+bin_positions[i+1])/2; // sum+= bin_density[i] * bin_positions[i+1]; mu[0]=sum; }
Find the bin where x belongs; -1 if x is out of range.
Definition at line 268 of file HistogramDistribution.cc.
References b, bin_positions, and PLearn::TVec< T >::length().
Referenced by density(), and survival_fn().
{ int b= 0, e= bin_positions.length()-2, p= b+(e-b)/2; if(x < bin_positions[b] || x >= bin_positions[e+1]) return -1; while(b < e) { if(bin_positions[p] == x) return p; if(bin_positions[p] > x) e= p-1; else b= p+1; p= b+(e-b)/2; } return p; }
OptionList & PLearn::HistogramDistribution::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::PDistribution.
Definition at line 65 of file HistogramDistribution.cc.
OptionMap & PLearn::HistogramDistribution::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::PDistribution.
Definition at line 65 of file HistogramDistribution.cc.
RemoteMethodMap & PLearn::HistogramDistribution::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::PDistribution.
Definition at line 65 of file HistogramDistribution.cc.
return log of probability density log(p(x))
Reimplemented from PLearn::PDistribution.
Definition at line 199 of file HistogramDistribution.cc.
References density(), and pl_log.
Referenced by computeOutput().
void PLearn::HistogramDistribution::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::PDistribution.
Definition at line 188 of file HistogramDistribution.cc.
References bin_density, bin_positions, binner, PLearn::deepCopyField(), PLearn::PDistribution::makeDeepCopyFromShallowCopy(), smoother, and survival_values.
{ inherited::makeDeepCopyFromShallowCopy(copies); deepCopyField(bin_positions, copies); deepCopyField(bin_density, copies); deepCopyField(survival_values, copies); deepCopyField(binner, copies); deepCopyField(smoother, copies); }
real PLearn::HistogramDistribution::prob_in_range | ( | const Vec & | x0, |
const Vec & | x1 | ||
) | const [virtual] |
return P(x0 < X < x1)
Definition at line 262 of file HistogramDistribution.cc.
References survival_fn().
{ return survival_fn(x0) - survival_fn(x1); }
return survival fn = P(X>x)
Reimplemented from PLearn::PDistribution.
Definition at line 213 of file HistogramDistribution.cc.
References bin_positions, find_bin(), PLERROR, PLearn::TVec< T >::size(), and survival_values.
Referenced by cdf(), computeOutput(), and prob_in_range().
{ if(x.size() != 1) PLERROR("HistogramDistribution::survival_fn implemented only for univariate data (vec size == 1)."); int bin= find_bin(x[0]); if(bin < 0) if(x[0] < bin_positions[0]) return 1.0; else return 0.0; if(x[0] < bin_positions[bin] && bin >= 1) return survival_values[bin-1] + (x[0] - bin_positions[bin-1]) * (survival_values[bin] - survival_values[bin-1]) / (bin_positions[bin] - bin_positions[bin-1]); return survival_values[bin]; }
void PLearn::HistogramDistribution::train | ( | ) | [virtual] |
trains the model
Reimplemented from PLearn::PDistribution.
Definition at line 108 of file HistogramDistribution.cc.
References bin_density, bin_positions, binner, calc_density_from_survival(), calc_survival_from_density(), PLearn::VMat::getColumn(), i, PLearn::PLearner::inputsize(), PLearn::TVec< T >::length(), PLearn::VMat::length(), PLERROR, PLearn::TVec< T >::resize(), smooth_density_instead_of_survival_fn, smoother, survival_values, PLearn::PLearner::targetsize(), PLearn::PLearner::train_set, and PLearn::VMat::width().
Referenced by HistogramDistribution().
{ /* - prend la distri empirique | trie les points | merge les bins (possiblement sous contraintes) | - points de coupure predefinis (option include_cutpoints) ManualBinner | - largeur des bins > a une valeur minimale | - bins contenir un minimum de points Binner Smoother (recalcule la densite) calculer survival_values */ if(train_set->width() != inputsize()+targetsize()) PLERROR("In HistogramDistribution::train(VMat training_set) training_set->width() != inputsize()+targetsize()"); if(train_set->width() != 1) PLERROR("In HistogramDistribution::train() train_set->width() must be 1 (column vec.)"); if(binner == 0) PLERROR("In HistogramDistribution::train() Can't train without a Binner."); Vec data(train_set.length()); data << train_set.getColumn(train_set.width()-1); PP<RealMapping> binning= binner->getBinning(train_set); binning->setMappingForOther(0.0); binning->transform(data); bin_positions= binning->getCutPoints(); bin_density.resize(bin_positions.length()-1); survival_values.resize(bin_positions.length()-1); for(int i= 0; i < data.length(); ++i) ++survival_values[static_cast<int>(data[i])]; for(int i= survival_values.length()-2; i >= 0; --i) survival_values[i]+= survival_values[i+1]; for(int i= survival_values.length()-1; i >= 0; --i) survival_values[i]/= survival_values[0]; if(smoother) { if (smooth_density_instead_of_survival_fn) { calc_density_from_survival(); Vec df(bin_density.length()); df << bin_density; smoother->smooth(df, bin_density, bin_positions, bin_positions); calc_survival_from_density(); } else { Vec sv(survival_values.length()); sv << survival_values; smoother->smooth(sv, survival_values, bin_positions, bin_positions); calc_density_from_survival(); } } else calc_density_from_survival(); }
void PLearn::HistogramDistribution::variance | ( | Mat & | cov | ) | const [virtual] |
Reimplemented from PLearn::PDistribution.
Definition at line 247 of file HistogramDistribution.cc.
References PLearn::abs(), bin_density, bin_positions, i, n, PLERROR, PLearn::TVec< T >::size(), PLearn::TMat< T >::size(), and PLearn::sum().
Referenced by computeOutput().
{ if(cov.size() != 1) PLERROR("HistogramDistribution::variance implemented only for univariate data"); real sumsq= 0.0, sum= 0.0, s; int n= bin_density.size(); for(int i= 0; i < n; ++i) { s= bin_density[i] * (bin_positions[i+1]-bin_positions[i]) * (bin_positions[i]+bin_positions[i+1])/2; sum+= s; sumsq+= s*s; } cov(0,0) = abs(sumsq-(sum*sum)/n)/n; }
Reimplemented from PLearn::PDistribution.
Definition at line 120 of file HistogramDistribution.h.
the density is supposed constant within each bin: p(x) = bin_density[i] if bin_positions[i] < x <= bin_positions[i+1]
Definition at line 63 of file HistogramDistribution.h.
Referenced by calc_density_from_survival(), calc_survival_from_density(), declareOptions(), density(), expectation(), makeDeepCopyFromShallowCopy(), train(), and variance().
there is one more bin position than number of bins, all the bins are supposed adjacent
Definition at line 59 of file HistogramDistribution.h.
Referenced by calc_density_from_survival(), calc_survival_from_density(), declareOptions(), expectation(), find_bin(), makeDeepCopyFromShallowCopy(), survival_fn(), train(), and variance().
this Binner is used to do binning at training time.
Definition at line 69 of file HistogramDistribution.h.
Referenced by declareOptions(), makeDeepCopyFromShallowCopy(), and train().
whether to smooth the density or the survival function
Definition at line 75 of file HistogramDistribution.h.
Referenced by declareOptions(), and train().
this Smoother is used at training time.
Definition at line 72 of file HistogramDistribution.h.
Referenced by declareOptions(), makeDeepCopyFromShallowCopy(), and train().
redundant with density is the pre-computed (optional) survival fn
Definition at line 66 of file HistogramDistribution.h.
Referenced by calc_density_from_survival(), calc_survival_from_density(), declareOptions(), makeDeepCopyFromShallowCopy(), survival_fn(), and train().