PLearn 0.1
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Protected Member Functions | Static Protected Member Functions | Private Member Functions
PLearn::ConditionalStatsCollector Class Reference

#include <ConditionalStatsCollector.h>

Inheritance diagram for PLearn::ConditionalStatsCollector:
Inheritance graph
[legend]
Collaboration diagram for PLearn::ConditionalStatsCollector:
Collaboration graph
[legend]

List of all members.

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 OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual ConditionalStatsCollectordeepCopy (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 PPathdeclaringFile ()

Public Attributes

int condvar
 index of conditioning variable
TVec< RealMappingranges
 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.

Detailed Description

Definition at line 54 of file ConditionalStatsCollector.h.


Member Typedef Documentation

Reimplemented from PLearn::Object.

Definition at line 57 of file ConditionalStatsCollector.h.


Constructor & Destructor Documentation

PLearn::ConditionalStatsCollector::ConditionalStatsCollector ( )

Definition at line 49 of file ConditionalStatsCollector.cc.

    : inherited(),
      condvar(0) 
{}

Member Function Documentation

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.

bool PLearn::ConditionalStatsCollector::_isa_ ( const Object o) [static]

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_().

Here is the call graph for this function:

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().

{
    if(counts.size()==0)
        forget();
}

Here is the call graph for this function:

Here is the caller graph for this function:

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);
}

Here is the call graph for this function:

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.

int PLearn::ConditionalStatsCollector::findrange ( int  varindex,
real  val 
) const [protected]

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;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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);
}

Here is the call graph for this function:

Here is the caller graph for this function:

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]
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.

{ 
    ranges = the_ranges;
    condvar = the_condvar;
    forget();
}

Here is the call graph for this function:

void PLearn::ConditionalStatsCollector::update ( const Vec v,
real  weight = 1.0 
)

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;
        }
    }
}

Here is the call graph for this function:


Member Data Documentation

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().

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().

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().

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().

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().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines