PLearn 0.1
MovingAverage.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // MovingAverage.cc
00004 //
00005 // Copyright (C) 2003 Rejean Ducharme, Yoshua Bengio
00006 // Copyright (C) 2003 Pascal Vincent
00007 //
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are met:
00010 //
00011 //  1. Redistributions of source code must retain the above copyright
00012 //     notice, this list of conditions and the following disclaimer.
00013 //
00014 //  2. Redistributions in binary form must reproduce the above copyright
00015 //     notice, this list of conditions and the following disclaimer in the
00016 //     documentation and/or other materials provided with the distribution.
00017 //
00018 //  3. The name of the authors may not be used to endorse or promote
00019 //     products derived from this software without specific prior written
00020 //     permission.
00021 //
00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 //
00033 // This file is part of the PLearn library. For more information on the PLearn
00034 // library, go to the PLearn Web site at www.plearn.org
00035 
00036 
00037 
00038 #include "MovingAverage.h"
00039 //#include "TMat_maths.h"
00040 //#include "TMat.h"
00041 
00042 namespace PLearn {
00043 using namespace std;
00044 
00045 
00046 PLEARN_IMPLEMENT_OBJECT(MovingAverage, "ONE LINE DESCR", "NO HELP");
00047 
00048 MovingAverage::MovingAverage()
00049     : window_length(-1)
00050 {}
00051 
00052 void MovingAverage::build_()
00053 {
00054     if(cost_funcs.size() < 1)
00055         PLERROR("In MovingAverage::build_()  Empty cost_funcs : must at least specify one cost function!");
00056     if (window_length < 1)
00057         PLERROR("In MovingAverage::build_()  window_length has not been set!");
00058 
00059     max_train_len = window_length;
00060 
00061     forget();
00062 }
00063 
00064 void MovingAverage::build()
00065 {
00066     inherited::build();
00067     build_();
00068 }
00069 
00070 void MovingAverage::declareOptions(OptionList& ol)
00071 {
00072     declareOption(ol, "window_length", &MovingAverage::window_length,
00073                   OptionBase::buildoption, "the length of the moving average window \n");
00074 
00075     declareOption(ol, "cost_funcs", &MovingAverage::cost_funcs,
00076                   OptionBase::buildoption, "a list of cost functions to use \n");
00077 
00078     inherited::declareOptions(ol);
00079 }
00080 
00081 void MovingAverage::train()
00082 {
00083     PP<ProgressBar> pb;
00084 
00085     static Vec input(0);
00086     static Vec target(targetsize());
00087     static Vec output(outputsize());
00088     static Vec cost(targetsize());
00089     static Mat all_targets;
00090 
00091     int target_pos = inputsize();
00092     int start = MAX(window_length-1, last_train_t+1);
00093     if (report_progress)
00094         pb = new ProgressBar("Training MovingAverage learner", train_set.length()-start);
00095     //train_stats->forget();
00096     for (int t=start; t<train_set.length(); t++)
00097     {
00098 #ifdef DEBUG
00099         cout << "MovingAverage::train -- t = " << t << endl;
00100 #endif
00101         all_targets = train_set.subMat(t-window_length+1, target_pos, window_length, targetsize());
00102         columnMean(all_targets,output);
00103         predictions(t) << output;
00104         if (t >= horizon)
00105         {
00106             Vec out = predictions(t-horizon);
00107             train_set->getSubRow(t, target_pos, target);
00108             if (!target.hasMissing() && !out.hasMissing())
00109             {
00110                 computeCostsFromOutputs(input, out, target, cost);
00111                 errors(t) << cost;
00112                 train_stats->update(cost);
00113 #ifdef DEBUG
00114                 cout << "MovingAverage::train update train_stats pour t = " << t << endl;
00115 #endif
00116             }
00117         }
00118         if (pb) pb->update(t-start);
00119     }
00120     last_train_t = MAX(train_set.length()-1, last_train_t);
00121 #ifdef DEBUG
00122     cout << "MovingAverage.last_train_t = " << last_train_t << endl;
00123 #endif
00124 
00125     train_stats->finalize();
00126 }
00127  
00128 void MovingAverage::test(VMat testset, PP<VecStatsCollector> test_stats,
00129                          VMat testoutputs, VMat testcosts) const
00130 {
00131     PP<ProgressBar> pb;
00132 
00133     static Vec input(0);
00134     static Vec target(targetsize());
00135     static Vec output(outputsize());
00136     static Vec cost(targetsize());
00137     static Mat all_targets;
00138 
00139     int start = MAX(window_length-1, last_test_t+1);
00140     start = MAX(last_train_t+1,start);
00141     int target_pos = inputsize();
00142     if (report_progress)
00143         pb = new ProgressBar("Testing MovingAverage learner", testset.length()-start);
00144     //test_stats->forget();
00145     for (int t=start; t<testset.length(); t++)
00146     {
00147 #ifdef DEBUG
00148         cout << "MovingAverage::test -- t = " << t << endl;
00149 #endif
00150         all_targets = testset.subMat(t-window_length+1, target_pos, window_length, targetsize()).toMat();
00151         columnMean(all_targets,output);
00152         predictions(t) << output;
00153         if (testoutputs) testoutputs->appendRow(output);
00154         if (t >= horizon)
00155         {
00156             Vec out = predictions(t-horizon);
00157             testset->getSubRow(t, target_pos, target);
00158             if (!target.hasMissing() && !out.hasMissing())
00159             {
00160                 computeCostsFromOutputs(input, out, target, cost);
00161                 errors(t) << cost;
00162                 if (testcosts) testcosts->appendRow(cost);
00163                 test_stats->update(cost);
00164 #ifdef DEBUG
00165                 cout << "MovingAverage::test update test_stats pour t = " << t << endl;
00166 #endif
00167             }
00168         }
00169         if (pb) pb->update(t-start);
00170     }
00171     last_test_t = MAX(testset.length()-1, last_test_t);
00172 #ifdef DEBUG
00173     cout << "MovingAverage.last_test_t = " << last_test_t << endl;
00174 #endif
00175 }
00176 
00177 void MovingAverage::computeCostsFromOutputs(const Vec& inputs, const Vec& outputs,
00178                                             const Vec& targets, Vec& costs) const
00179 {
00180     for (int i=0; i<cost_funcs.size(); i++)
00181     {
00182         if (cost_funcs[i]=="mse" || cost_funcs[i]=="MSE")
00183             costs << square(outputs-targets);
00184         else
00185             PLERROR("This cost_funcs is not implemented.");
00186     }
00187 }
00188 
00189 TVec<string> MovingAverage::getTrainCostNames() const
00190 { return cost_funcs; }
00191 
00192 TVec<string> MovingAverage::getTestCostNames() const
00193 { return getTrainCostNames(); }
00194 
00195 void MovingAverage::forget()
00196 { inherited::forget(); }
00197 
00198 /*
00199   void MovingAverage::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00200   {
00201   inherited::makeDeepCopyFromShallowCopy(copies);
00202   deepCopyField(cost_funcs, copies);
00203   }
00204 */
00205 
00206 
00207 } // end of namespace PLearn
00208 
00209 
00210 /*
00211   Local Variables:
00212   mode:c++
00213   c-basic-offset:4
00214   c-file-style:"stroustrup"
00215   c-file-offsets:((innamespace . 0)(inline-open . 0))
00216   indent-tabs-mode:nil
00217   fill-column:79
00218   End:
00219 */
00220 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines