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

This SequentialLearner only takes the n previous target to predict the next one. More...

#include <MovingAverage.h>

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

List of all members.

Public Types

typedef SequentialLearner inherited
 Does the necessary operations to transform a shallow copy (this) into a deep copy by deep-copying all the members that need to be.

Public Member Functions

 MovingAverage ()
 Constructor.
virtual void build ()
 simply calls inherited::build() then build_()
virtual void train ()
virtual void test (VMat testset, PP< VecStatsCollector > test_stats, VMat testoutputs=0, VMat testcosts=0) const
virtual void computeCostsFromOutputs (const Vec &inputs, const Vec &outputs, const Vec &targets, Vec &costs) const
 *** SUBCLASS WRITING: ***
virtual TVec< string > getTrainCostNames () const
 *** SUBCLASS WRITING: ***
virtual TVec< string > getTestCostNames () const
 *** SUBCLASS WRITING: ***
virtual void forget ()
 *** SUBCLASS WRITING: ***
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual MovingAveragedeepCopy (CopiesMap &copies) const

Static Public Member Functions

static string _classname_ ()
 Does the necessary operations to transform a shallow copy (this) into a deep copy by deep-copying all the members that need to be.
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 window_length
Array< string > cost_funcs

Static Public Attributes

static StaticInitializer _static_initializer_

Static Protected Member Functions

static void declareOptions (OptionList &ol)
 Declare this class' options.

Private Member Functions

void build_ ()
 This does the actual building.

Detailed Description

This SequentialLearner only takes the n previous target to predict the next one.

This is a memoryless learner!

Definition at line 49 of file MovingAverage.h.


Member Typedef Documentation

Does the necessary operations to transform a shallow copy (this) into a deep copy by deep-copying all the members that need to be.

Reimplemented from PLearn::SequentialLearner.

Definition at line 87 of file MovingAverage.h.


Constructor & Destructor Documentation

PLearn::MovingAverage::MovingAverage ( )

Constructor.

Definition at line 48 of file MovingAverage.cc.

    : window_length(-1)
{}

Member Function Documentation

string PLearn::MovingAverage::_classname_ ( ) [static]

Does the necessary operations to transform a shallow copy (this) into a deep copy by deep-copying all the members that need to be.

Reimplemented from PLearn::SequentialLearner.

Definition at line 46 of file MovingAverage.cc.

OptionList & PLearn::MovingAverage::_getOptionList_ ( ) [static]

Reimplemented from PLearn::SequentialLearner.

Definition at line 46 of file MovingAverage.cc.

RemoteMethodMap & PLearn::MovingAverage::_getRemoteMethodMap_ ( ) [static]

Reimplemented from PLearn::SequentialLearner.

Definition at line 46 of file MovingAverage.cc.

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

Reimplemented from PLearn::SequentialLearner.

Definition at line 46 of file MovingAverage.cc.

Object * PLearn::MovingAverage::_new_instance_for_typemap_ ( ) [static]

Reimplemented from PLearn::Object.

Definition at line 46 of file MovingAverage.cc.

StaticInitializer MovingAverage::_static_initializer_ & PLearn::MovingAverage::_static_initialize_ ( ) [static]

Reimplemented from PLearn::SequentialLearner.

Definition at line 46 of file MovingAverage.cc.

void PLearn::MovingAverage::build ( ) [virtual]

simply calls inherited::build() then build_()

Reimplemented from PLearn::SequentialLearner.

Definition at line 64 of file MovingAverage.cc.

References PLearn::SequentialLearner::build(), and build_().

Here is the call graph for this function:

void PLearn::MovingAverage::build_ ( ) [private]

This does the actual building.

Reimplemented from PLearn::SequentialLearner.

Definition at line 52 of file MovingAverage.cc.

References cost_funcs, forget(), PLearn::SequentialLearner::max_train_len, PLERROR, PLearn::TVec< T >::size(), and window_length.

Referenced by build().

{
    if(cost_funcs.size() < 1)
        PLERROR("In MovingAverage::build_()  Empty cost_funcs : must at least specify one cost function!");
    if (window_length < 1)
        PLERROR("In MovingAverage::build_()  window_length has not been set!");

    max_train_len = window_length;

    forget();
}

Here is the call graph for this function:

Here is the caller graph for this function:

string PLearn::MovingAverage::classname ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 46 of file MovingAverage.cc.

void PLearn::MovingAverage::computeCostsFromOutputs ( const Vec input,
const Vec output,
const Vec target,
Vec costs 
) const [virtual]

*** SUBCLASS WRITING: ***

This should be defined in subclasses to compute the weighted costs from already computed output. The costs should correspond to the cost names returned by getTestCostNames().

NOTE: In exotic cases, the cost may also depend on some info in the input, that's why the method also gets so see it.

Reimplemented from PLearn::SequentialLearner.

Definition at line 177 of file MovingAverage.cc.

References cost_funcs, i, PLERROR, PLearn::TVec< T >::size(), and PLearn::square().

Referenced by test(), and train().

{
    for (int i=0; i<cost_funcs.size(); i++)
    {
        if (cost_funcs[i]=="mse" || cost_funcs[i]=="MSE")
            costs << square(outputs-targets);
        else
            PLERROR("This cost_funcs is not implemented.");
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::MovingAverage::declareOptions ( OptionList ol) [static, protected]

Declare this class' options.

Reimplemented from PLearn::SequentialLearner.

Definition at line 70 of file MovingAverage.cc.

References PLearn::OptionBase::buildoption, cost_funcs, PLearn::declareOption(), PLearn::SequentialLearner::declareOptions(), and window_length.

{
    declareOption(ol, "window_length", &MovingAverage::window_length,
                  OptionBase::buildoption, "the length of the moving average window \n");

    declareOption(ol, "cost_funcs", &MovingAverage::cost_funcs,
                  OptionBase::buildoption, "a list of cost functions to use \n");

    inherited::declareOptions(ol);
}

Here is the call graph for this function:

static const PPath& PLearn::MovingAverage::declaringFile ( ) [inline, static]

Reimplemented from PLearn::SequentialLearner.

Definition at line 88 of file MovingAverage.h.

MovingAverage * PLearn::MovingAverage::deepCopy ( CopiesMap copies) const [virtual]

Reimplemented from PLearn::SequentialLearner.

Definition at line 46 of file MovingAverage.cc.

void PLearn::MovingAverage::forget ( ) [virtual]

*** SUBCLASS WRITING: ***

(Re-)initializes the PLearner in its fresh state (that state may depend on the 'seed' option) and sets 'stage' back to 0 (this is the stage of a fresh learner!)

A typical forget() method should do the following:

  • initialize the learner's parameters, using this random generator
  • stage = 0;

This method is typically called by the build_() method, after it has finished setting up the parameters, and if it deemed useful to set or reset the learner in its fresh state. (remember build may be called after modifying options that do not necessarily require the learner to restart from a fresh state...) forget is also called by the setTrainingSet method, after calling build(), so it will generally be called TWICE during setTrainingSet!

Reimplemented from PLearn::SequentialLearner.

Definition at line 195 of file MovingAverage.cc.

References PLearn::SequentialLearner::forget().

Referenced by build_().

Here is the call graph for this function:

Here is the caller graph for this function:

OptionList & PLearn::MovingAverage::getOptionList ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 46 of file MovingAverage.cc.

OptionMap & PLearn::MovingAverage::getOptionMap ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 46 of file MovingAverage.cc.

RemoteMethodMap & PLearn::MovingAverage::getRemoteMethodMap ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 46 of file MovingAverage.cc.

TVec< string > PLearn::MovingAverage::getTestCostNames ( ) const [virtual]

*** SUBCLASS WRITING: ***

This should return the names of the costs computed by computeCostsFromOutputs.

Implements PLearn::PLearner.

Definition at line 192 of file MovingAverage.cc.

References getTrainCostNames().

{ return getTrainCostNames(); }

Here is the call graph for this function:

TVec< string > PLearn::MovingAverage::getTrainCostNames ( ) const [virtual]

*** SUBCLASS WRITING: ***

This should return the names of the objective costs that the train method computes and for which it updates the VecStatsCollector train_stats.

Implements PLearn::PLearner.

Definition at line 189 of file MovingAverage.cc.

References cost_funcs.

Referenced by getTestCostNames().

{ return cost_funcs; }

Here is the caller graph for this function:

void PLearn::MovingAverage::test ( VMat  testset,
PP< VecStatsCollector test_stats,
VMat  testoutputs = 0,
VMat  testcosts = 0 
) const [virtual]

*** SUBCLASS WRITING: *** The method should:

  • call computeOutputAndCosts on the test set
  • save the outputs and the costs in the predictions & errors matrices, beginning at position last_call_train_t

Implements PLearn::SequentialLearner.

Definition at line 128 of file MovingAverage.cc.

References PLearn::columnMean(), computeCostsFromOutputs(), PLearn::endl(), PLearn::SequentialLearner::errors, PLearn::VMat::getSubRow(), PLearn::TVec< T >::hasMissing(), PLearn::SequentialLearner::horizon, PLearn::PLearner::inputsize(), PLearn::SequentialLearner::last_test_t, PLearn::SequentialLearner::last_train_t, PLearn::VMat::length(), MAX, PLearn::SequentialLearner::outputsize(), PLearn::SequentialLearner::predictions, PLearn::PLearner::report_progress, PLearn::VMat::subMat(), PLearn::PLearner::targetsize(), PLearn::VMat::toMat(), and window_length.

{
    PP<ProgressBar> pb;

    static Vec input(0);
    static Vec target(targetsize());
    static Vec output(outputsize());
    static Vec cost(targetsize());
    static Mat all_targets;

    int start = MAX(window_length-1, last_test_t+1);
    start = MAX(last_train_t+1,start);
    int target_pos = inputsize();
    if (report_progress)
        pb = new ProgressBar("Testing MovingAverage learner", testset.length()-start);
    //test_stats->forget();
    for (int t=start; t<testset.length(); t++)
    {
#ifdef DEBUG
        cout << "MovingAverage::test -- t = " << t << endl;
#endif
        all_targets = testset.subMat(t-window_length+1, target_pos, window_length, targetsize()).toMat();
        columnMean(all_targets,output);
        predictions(t) << output;
        if (testoutputs) testoutputs->appendRow(output);
        if (t >= horizon)
        {
            Vec out = predictions(t-horizon);
            testset->getSubRow(t, target_pos, target);
            if (!target.hasMissing() && !out.hasMissing())
            {
                computeCostsFromOutputs(input, out, target, cost);
                errors(t) << cost;
                if (testcosts) testcosts->appendRow(cost);
                test_stats->update(cost);
#ifdef DEBUG
                cout << "MovingAverage::test update test_stats pour t = " << t << endl;
#endif
            }
        }
        if (pb) pb->update(t-start);
    }
    last_test_t = MAX(testset.length()-1, last_test_t);
#ifdef DEBUG
    cout << "MovingAverage.last_test_t = " << last_test_t << endl;
#endif
}

Here is the call graph for this function:

void PLearn::MovingAverage::train ( ) [virtual]

*** SUBCLASS WRITING: *** Does the actual training. Subclasses must implement this method. The method should upon entry, call setTrainingSet(training_set); The method should:

  • do nothing if we already called it with this value of train.length or a value between [last_train_t-train_step+1,last_train_t]
  • if not, train and update the value of last_train_t
  • in either case, update the value of last_call_train_t

Implements PLearn::SequentialLearner.

Definition at line 81 of file MovingAverage.cc.

References PLearn::columnMean(), computeCostsFromOutputs(), PLearn::endl(), PLearn::SequentialLearner::errors, PLearn::VMat::getSubRow(), PLearn::TVec< T >::hasMissing(), PLearn::SequentialLearner::horizon, PLearn::PLearner::inputsize(), PLearn::SequentialLearner::last_train_t, PLearn::VMat::length(), MAX, PLearn::SequentialLearner::outputsize(), PLearn::SequentialLearner::predictions, PLearn::PLearner::report_progress, PLearn::VMat::subMat(), PLearn::PLearner::targetsize(), PLearn::PLearner::train_set, PLearn::PLearner::train_stats, and window_length.

{
    PP<ProgressBar> pb;

    static Vec input(0);
    static Vec target(targetsize());
    static Vec output(outputsize());
    static Vec cost(targetsize());
    static Mat all_targets;

    int target_pos = inputsize();
    int start = MAX(window_length-1, last_train_t+1);
    if (report_progress)
        pb = new ProgressBar("Training MovingAverage learner", train_set.length()-start);
    //train_stats->forget();
    for (int t=start; t<train_set.length(); t++)
    {
#ifdef DEBUG
        cout << "MovingAverage::train -- t = " << t << endl;
#endif
        all_targets = train_set.subMat(t-window_length+1, target_pos, window_length, targetsize());
        columnMean(all_targets,output);
        predictions(t) << output;
        if (t >= horizon)
        {
            Vec out = predictions(t-horizon);
            train_set->getSubRow(t, target_pos, target);
            if (!target.hasMissing() && !out.hasMissing())
            {
                computeCostsFromOutputs(input, out, target, cost);
                errors(t) << cost;
                train_stats->update(cost);
#ifdef DEBUG
                cout << "MovingAverage::train update train_stats pour t = " << t << endl;
#endif
            }
        }
        if (pb) pb->update(t-start);
    }
    last_train_t = MAX(train_set.length()-1, last_train_t);
#ifdef DEBUG
    cout << "MovingAverage.last_train_t = " << last_train_t << endl;
#endif

    train_stats->finalize();
}

Here is the call graph for this function:


Member Data Documentation

Reimplemented from PLearn::SequentialLearner.

Definition at line 88 of file MovingAverage.h.

Definition at line 54 of file MovingAverage.h.

Referenced by build_(), computeCostsFromOutputs(), declareOptions(), and getTrainCostNames().

Definition at line 53 of file MovingAverage.h.

Referenced by build_(), declareOptions(), test(), and train().


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