PLearn 0.1
|
#include <ConditionalStatsCollector.h>
Public Types | |
typedef Object | inherited |
Public Member Functions | |
ConditionalStatsCollector () | |
void | setBinMappingsAndCondvar (const TVec< RealMapping > &the_ranges, int the_condvar) |
Sets the ranges of interest for each variable, and the index of the conditioning variable. Ranges for a given variable should not overlap. | |
void | forget () |
clears all statistics, allowing to restart collecting them | |
void | update (const Vec &v, real weight=1.0) |
Updates the counts for an observation v. | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual ConditionalStatsCollector * | deepCopy (CopiesMap &copies) const |
virtual void | build () |
Post-constructor. | |
virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
Transforms a shallow copy into a deep copy. | |
Static Public Member Functions | |
static string | _classname_ () |
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 () |
Public Attributes | |
int | condvar |
index of conditioning variable | |
TVec< RealMapping > | ranges |
ranges[k] must contain bin-mappings for variable k, which maps it to an integer ( 0 to mappings[k].size()-1 ) | |
TVec< TMat< double > > | counts |
counts[k](i,j) is the number of times the variable k fell in range i while variable condvar was in range j counts[k] has one more row and column than there are mapping ranges: the last ones counting "MISSING_VALUE" occurences. | |
TVec< TMat< double > > | sums |
sums[k](i,j) contains the (possibly weighted) sum of variable k's values that fell in range i while condvar was in range j counts[k] has one more row and column than there are mapping ranges: the last ones counting "MISSING_VALUE" occurences. | |
TVec< TMat< double > > | sums_condvar |
sums_condvar[k](i,j) contains the (possibly weighted) sum of variable condvar's values that fell in range i while variable k was in range j counts[k] has one more row and column than there are mapping ranges: the last ones counting "MISSING_VALUE" occurences. | |
TVec< TMat< double > > | sumsquares |
sumsquares[k](i,j) contains the (possibly weighted) sum of squares of variable k's values that fell in range i while condvar was in range j counts[k] has one more row and column than there are mapping ranges: the last ones counting "MISSING_VALUE" occurences. | |
TVec< TMat< double > > | sumsquares_condvar |
sumsquares_condvar[k](i,j) contains the (possibly weighted) sum of squares of condvar's values that fell in range i while variable k was in range j counts[k] has one more row and column than there are mapping ranges: the last ones counting "MISSING_VALUE" occurences. | |
TVec< TMat< double > > | minima |
minima[k](i,j) contains the min of variable k's values that fell in range i while condvar was in range j counts[k] has one more row and column than there are mapping ranges: the last ones counting "MISSING_VALUE" occurences. | |
TVec< TMat< double > > | minima_condvar |
minima_condvar[k](i,j) contains the min of variable condvar's values that fell in range i while variable k was in range j counts[k] has one more row and column than there are mapping ranges: the last ones counting "MISSING_VALUE" occurences. | |
TVec< TMat< double > > | maxima |
maxima[k](i,j) contains the max of variable k's values that fell in range i while condvar was in range j counts[k] has one more row and column than there are mapping ranges: the last ones counting "MISSING_VALUE" occurences. | |
TVec< TMat< double > > | maxima_condvar |
maxima_condvar[k](i,j) contains the max of variable condvar's values that fell in range i while variable k was in range j counts[k] has one more row and column than there are mapping ranges: the last ones counting "MISSING_VALUE" occurences. | |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Protected Member Functions | |
int | findrange (int varindex, real val) const |
Returns the first index of the range containing the given value for that variable Returns ranges[varindex].length() if val==missing Returns -1 if no range containing val was found. | |
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 54 of file ConditionalStatsCollector.h.
Reimplemented from PLearn::Object.
Definition at line 57 of file ConditionalStatsCollector.h.
PLearn::ConditionalStatsCollector::ConditionalStatsCollector | ( | ) |
Definition at line 49 of file ConditionalStatsCollector.cc.
string PLearn::ConditionalStatsCollector::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 54 of file ConditionalStatsCollector.cc.
OptionList & PLearn::ConditionalStatsCollector::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 54 of file ConditionalStatsCollector.cc.
RemoteMethodMap & PLearn::ConditionalStatsCollector::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 54 of file ConditionalStatsCollector.cc.
Reimplemented from PLearn::Object.
Definition at line 54 of file ConditionalStatsCollector.cc.
Object * PLearn::ConditionalStatsCollector::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 54 of file ConditionalStatsCollector.cc.
StaticInitializer ConditionalStatsCollector::_static_initializer_ & PLearn::ConditionalStatsCollector::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 54 of file ConditionalStatsCollector.cc.
void PLearn::ConditionalStatsCollector::build | ( | ) | [virtual] |
Post-constructor.
The normal implementation should call simply inherited::build(), then this class's build_(). This method should be callable again at later times, after modifying some option fields to change the "architecture" of the object.
Reimplemented from PLearn::Object.
Definition at line 130 of file ConditionalStatsCollector.cc.
References PLearn::Object::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::ConditionalStatsCollector::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::Object.
Definition at line 123 of file ConditionalStatsCollector.cc.
References counts, forget(), and PLearn::TVec< T >::size().
Referenced by build().
string PLearn::ConditionalStatsCollector::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 54 of file ConditionalStatsCollector.cc.
void PLearn::ConditionalStatsCollector::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares this class' options.
Reimplemented from PLearn::Object.
Definition at line 56 of file ConditionalStatsCollector.cc.
References PLearn::OptionBase::buildoption, condvar, counts, PLearn::declareOption(), PLearn::Object::declareOptions(), PLearn::OptionBase::learntoption, maxima, maxima_condvar, minima, minima_condvar, ranges, sums, sums_condvar, sumsquares, and sumsquares_condvar.
{ // ### Declare all of this object's options here // ### For the "flags" of each option, you should typically specify // ### one of OptionBase::buildoption, OptionBase::learntoption or // ### OptionBase::tuningoption. Another possible flag to be combined with // ### is OptionBase::nosave // ### ex: // declareOption(ol, "myoption", &ConditionalStatsCollector::myoption, OptionBase::buildoption, // "Help text describing this option"); // ... declareOption(ol, "condvar", &ConditionalStatsCollector::condvar, OptionBase::buildoption, "index of conditioning variable \n"); declareOption(ol, "ranges", &ConditionalStatsCollector::ranges, OptionBase::buildoption, "ranges[k] must contain bin-mappings for variable k, \n" "which maps it to an integer ( 0 to mappings[k].size()-1 ) \n"); declareOption(ol, "counts", &ConditionalStatsCollector::counts, OptionBase::learntoption, "counts[k](i,j) is the number of times the variable k fell in \n" "range i while variable condvar was in range j \n" "counts[k] has one more row and column than there are mapping ranges:\n" "the last ones counting MISSING_VALUE occurences.\n" "Actually counts is the 'number of times' only when update is called \n" "without a weight. Otherwise it's really the sum of the sample weights."); declareOption(ol, "sums", &ConditionalStatsCollector::sums, OptionBase::learntoption, "sums[k](i,j) contains the sum of variable k's values that fell in range i while condvar was in range j \n" "(unlike counts, these do not have an extra row and column for misisng value"); declareOption(ol, "sums_condvar", &ConditionalStatsCollector::sums_condvar, OptionBase::learntoption, "sums_condvar[k](i,j) contains the (possibly weighted) sum of variable condvar's values that fell in range i while variable k was in range j \n" "(unlike counts, these do not have an extra row and column for misisng value)"); declareOption(ol, "sumsquares", &ConditionalStatsCollector::sumsquares, OptionBase::learntoption, "sumsquares[k](i,j) contains the (possibly weighted) sum of squares of variable k's values that fell in range i while condvar was in range j \n" "(unlike counts, these do not have an extra row and column for misisng value)"); declareOption(ol, "sumsquares_condvar", &ConditionalStatsCollector::sumsquares_condvar, OptionBase::learntoption, "sumsquares_condvar[k](i,j) contains the (possibly weighted) sum of squares of condvar's values that fell in range i while variable k was in range j \n" "(unlike counts, these do not have an extra row and column for misisng value)"); declareOption(ol, "minima", &ConditionalStatsCollector::minima, OptionBase::learntoption, "minima[k](i,j) contains the min of variable k's values that fell in range i while condvar was in range j \n" "(unlike counts, these do not have an extra row and column for misisng value)"); declareOption(ol, "minima_condvar", &ConditionalStatsCollector::minima_condvar, OptionBase::learntoption, "minima_condvar[k](i,j) contains the min of variable condvar's values that fell in range i while variable k was in range j \n" "(unlike counts, these do not have an extra row and column for misisng value)"); declareOption(ol, "maxima", &ConditionalStatsCollector::maxima, OptionBase::learntoption, "maxima[k](i,j) contains the max of variable k's values that fell in range i while condvar was in range j \n" "(unlike counts, these do not have an extra row and column for misisng value)"); declareOption(ol, "maxima_condvar", &ConditionalStatsCollector::maxima_condvar, OptionBase::learntoption, "maxima_condvar[k](i,j) contains the max of variable condvar's values that fell in range i while variable k was in range j \n" "(unlike counts, these do not have an extra row and column for misisng value)"); // Now call the parent class' declareOptions inherited::declareOptions(ol); }
static const PPath& PLearn::ConditionalStatsCollector::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::Object.
Definition at line 160 of file ConditionalStatsCollector.h.
ConditionalStatsCollector * PLearn::ConditionalStatsCollector::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 54 of file ConditionalStatsCollector.cc.
Returns the first index of the range containing the given value for that variable Returns ranges[varindex].length() if val==missing Returns -1 if no range containing val was found.
Definition at line 156 of file ConditionalStatsCollector.cc.
References PLearn::is_missing(), PLearn::RealMapping::length(), PLearn::RealMapping::map(), and ranges.
Referenced by update().
{ RealMapping& r = ranges[varindex]; int pos = -1; if(is_missing(val)) pos = r.length(); else { pos = (int) r.map(val); /* if(pos==-1) { real minimum = r.begin()->first.low; real maximum = (--r.end())->first.high; PLWARNING("In ConditionalStatsCollector::findrange(%d, %.18g) value of variable not in mapping (min=%.18g, max=%.18g)",varindex,val,minimum,maximum); cerr << r << endl; if(val>maximum && val-maximum<1e-6) pos = r.length()-1; else if(val<minimum && minimum-val<1e-6) pos = 0; } */ } return pos; }
void PLearn::ConditionalStatsCollector::forget | ( | ) |
clears all statistics, allowing to restart collecting them
Definition at line 136 of file ConditionalStatsCollector.cc.
References counts, maxima, maxima_condvar, minima, minima_condvar, PLearn::TVec< T >::resize(), sums, sums_condvar, sumsquares, and sumsquares_condvar.
Referenced by build_(), and setBinMappingsAndCondvar().
{ counts.resize(0); sums.resize(0); sumsquares.resize(0); minima.resize(0); maxima.resize(0); sums_condvar.resize(0); sumsquares_condvar.resize(0); minima_condvar.resize(0); maxima_condvar.resize(0); }
OptionList & PLearn::ConditionalStatsCollector::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 54 of file ConditionalStatsCollector.cc.
OptionMap & PLearn::ConditionalStatsCollector::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 54 of file ConditionalStatsCollector.cc.
RemoteMethodMap & PLearn::ConditionalStatsCollector::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 54 of file ConditionalStatsCollector.cc.
void PLearn::ConditionalStatsCollector::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::Object.
Definition at line 262 of file ConditionalStatsCollector.cc.
References counts, PLearn::deepCopyField(), PLearn::Object::makeDeepCopyFromShallowCopy(), maxima, maxima_condvar, minima, minima_condvar, ranges, sums, sums_condvar, sumsquares, and sumsquares_condvar.
{ inherited::makeDeepCopyFromShallowCopy(copies); deepCopyField(ranges, copies); deepCopyField(counts, copies); deepCopyField(sums, copies); deepCopyField(sumsquares, copies); deepCopyField(minima, copies); deepCopyField(maxima, copies); deepCopyField(sums_condvar, copies); deepCopyField(sumsquares_condvar, copies); deepCopyField(minima_condvar, copies); deepCopyField(maxima_condvar, copies); }
void PLearn::ConditionalStatsCollector::setBinMappingsAndCondvar | ( | const TVec< RealMapping > & | the_ranges, |
int | the_condvar | ||
) |
Sets the ranges of interest for each variable, and the index of the conditioning variable. Ranges for a given variable should not overlap.
Definition at line 149 of file ConditionalStatsCollector.cc.
References condvar, forget(), and ranges.
Updates the counts for an observation v.
Definition at line 184 of file ConditionalStatsCollector.cc.
References condvar, counts, PLearn::TVec< T >::fill(), findrange(), i, PLearn::is_missing(), j, PLearn::TVec< T >::length(), maxima, maxima_condvar, minima, minima_condvar, PLERROR, PLWARNING, ranges, PLearn::TVec< T >::resize(), PLearn::square(), sums, sums_condvar, sumsquares, and sumsquares_condvar.
{ int nvars = ranges.length(); if(v.length()!=nvars) PLERROR("IN ConditionalStatsCollectos::update length of update vector and nvars differ!"); if(counts.length()!=nvars) { counts.resize(nvars); sums.resize(nvars); sums_condvar.resize(nvars); sumsquares.resize(nvars); sumsquares_condvar.resize(nvars); minima.resize(nvars); minima_condvar.resize(nvars); maxima.resize(nvars); maxima_condvar.resize(nvars); int nranges_condvar = ranges[condvar].length()+1; // +1 for missing values for(int k=0; k<nvars; k++) { int nranges_k = ranges[k].length()+1; // +1 for missing values counts[k].resize(nranges_k, nranges_condvar); counts[k].fill(0); sums[k].resize(nranges_k, nranges_condvar); sums[k].fill(0); sums_condvar[k].resize(nranges_condvar, nranges_k); sums_condvar[k].fill(0); sumsquares[k].resize(nranges_k, nranges_condvar); sumsquares[k].fill(0); sumsquares_condvar[k].resize(nranges_condvar, nranges_k); sumsquares_condvar[k].fill(0); minima[k].resize(nranges_k, nranges_condvar); minima[k].fill(FLT_MAX); minima_condvar[k].resize(nranges_condvar, nranges_k); minima_condvar[k].fill(FLT_MAX); maxima[k].resize(nranges_k, nranges_condvar); maxima[k].fill(-FLT_MAX); maxima_condvar[k].resize(nranges_condvar, nranges_k); maxima_condvar[k].fill(-FLT_MAX); } } real condvar_val = v[condvar]; int j = findrange(condvar, condvar_val); if(j==-1) PLWARNING("In ConditionalStatsCollector::update value of conditioning var in none of the ranges"); for(int k=0; k<nvars; k++) { real val = v[k]; int i = findrange(k, val); if(i==-1) { PLWARNING("In ConditionalStatsCollector::update value of variable #%d in none of the ranges",k); } counts[k](i,j)+=weight; if(!is_missing(val)) { sums[k](i,j) += weight*val; sumsquares[k](i,j) += weight*square(val); if(val<minima[k](i,j)) minima[k](i,j) = val; if(val>maxima[k](i,j)) maxima[k](i,j) = val; } if(!is_missing(condvar_val)) { sums_condvar[k](j,i) += weight*condvar_val; sumsquares_condvar[k](j,i) += weight*square(condvar_val); if(condvar_val<minima_condvar[k](j,i)) minima_condvar[k](j,i) = condvar_val; if(condvar_val>maxima_condvar[k](j,i)) maxima_condvar[k](j,i) = condvar_val; } } }
Reimplemented from PLearn::Object.
Definition at line 160 of file ConditionalStatsCollector.h.
index of conditioning variable
Definition at line 74 of file ConditionalStatsCollector.h.
Referenced by declareOptions(), setBinMappingsAndCondvar(), and update().
TVec< TMat<double> > PLearn::ConditionalStatsCollector::counts |
counts[k](i,j) is the number of times the variable k fell in range i while variable condvar was in range j counts[k] has one more row and column than there are mapping ranges: the last ones counting "MISSING_VALUE" occurences.
Actually counts is the "number of times" only when update is called without a weight. Otherwise it's really the sum of the sample weights.
Definition at line 83 of file ConditionalStatsCollector.h.
Referenced by build_(), declareOptions(), forget(), makeDeepCopyFromShallowCopy(), and update().
TVec< TMat<double> > PLearn::ConditionalStatsCollector::maxima |
maxima[k](i,j) contains the max of variable k's values that fell in range i while condvar was in range j counts[k] has one more row and column than there are mapping ranges: the last ones counting "MISSING_VALUE" occurences.
Definition at line 111 of file ConditionalStatsCollector.h.
Referenced by declareOptions(), forget(), makeDeepCopyFromShallowCopy(), and update().
maxima_condvar[k](i,j) contains the max of variable condvar's values that fell in range i while variable k was in range j counts[k] has one more row and column than there are mapping ranges: the last ones counting "MISSING_VALUE" occurences.
Definition at line 115 of file ConditionalStatsCollector.h.
Referenced by declareOptions(), forget(), makeDeepCopyFromShallowCopy(), and update().
TVec< TMat<double> > PLearn::ConditionalStatsCollector::minima |
minima[k](i,j) contains the min of variable k's values that fell in range i while condvar was in range j counts[k] has one more row and column than there are mapping ranges: the last ones counting "MISSING_VALUE" occurences.
Definition at line 103 of file ConditionalStatsCollector.h.
Referenced by declareOptions(), forget(), makeDeepCopyFromShallowCopy(), and update().
minima_condvar[k](i,j) contains the min of variable condvar's values that fell in range i while variable k was in range j counts[k] has one more row and column than there are mapping ranges: the last ones counting "MISSING_VALUE" occurences.
Definition at line 107 of file ConditionalStatsCollector.h.
Referenced by declareOptions(), forget(), makeDeepCopyFromShallowCopy(), and update().
ranges[k] must contain bin-mappings for variable k, which maps it to an integer ( 0 to mappings[k].size()-1 )
Definition at line 77 of file ConditionalStatsCollector.h.
Referenced by declareOptions(), findrange(), makeDeepCopyFromShallowCopy(), setBinMappingsAndCondvar(), and update().
TVec< TMat<double> > PLearn::ConditionalStatsCollector::sums |
sums[k](i,j) contains the (possibly weighted) sum of variable k's values that fell in range i while condvar was in range j counts[k] has one more row and column than there are mapping ranges: the last ones counting "MISSING_VALUE" occurences.
Definition at line 87 of file ConditionalStatsCollector.h.
Referenced by declareOptions(), forget(), makeDeepCopyFromShallowCopy(), and update().
sums_condvar[k](i,j) contains the (possibly weighted) sum of variable condvar's values that fell in range i while variable k was in range j counts[k] has one more row and column than there are mapping ranges: the last ones counting "MISSING_VALUE" occurences.
Definition at line 91 of file ConditionalStatsCollector.h.
Referenced by declareOptions(), forget(), makeDeepCopyFromShallowCopy(), and update().
sumsquares[k](i,j) contains the (possibly weighted) sum of squares of variable k's values that fell in range i while condvar was in range j counts[k] has one more row and column than there are mapping ranges: the last ones counting "MISSING_VALUE" occurences.
Definition at line 95 of file ConditionalStatsCollector.h.
Referenced by declareOptions(), forget(), makeDeepCopyFromShallowCopy(), and update().
sumsquares_condvar[k](i,j) contains the (possibly weighted) sum of squares of condvar's values that fell in range i while variable k was in range j counts[k] has one more row and column than there are mapping ranges: the last ones counting "MISSING_VALUE" occurences.
Definition at line 99 of file ConditionalStatsCollector.h.
Referenced by declareOptions(), forget(), makeDeepCopyFromShallowCopy(), and update().