Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

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_impl.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 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 if (pb) delete pb; 00128 } 00129 00130 void MovingAverage::test(VMat testset, PP<VecStatsCollector> test_stats, 00131 VMat testoutputs, VMat testcosts) const 00132 { 00133 ProgressBar* pb; 00134 00135 static Vec input(0); 00136 static Vec target(targetsize()); 00137 static Vec output(outputsize()); 00138 static Vec cost(targetsize()); 00139 static Mat all_targets; 00140 00141 int start = MAX(window_length-1, last_test_t+1); 00142 start = MAX(last_train_t+1,start); 00143 int target_pos = inputsize(); 00144 if (report_progress) 00145 pb = new ProgressBar("Testing MovingAverage learner", testset.length()-start); 00146 //test_stats->forget(); 00147 for (int t=start; t<testset.length(); t++) 00148 { 00149 #ifdef DEBUG 00150 cout << "MovingAverage::test -- t = " << t << endl; 00151 #endif 00152 all_targets = testset.subMat(t-window_length+1, target_pos, window_length, targetsize()).toMat(); 00153 columnMean(all_targets,output); 00154 predictions(t) << output; 00155 if (testoutputs) testoutputs->appendRow(output); 00156 if (t >= horizon) 00157 { 00158 Vec out = predictions(t-horizon); 00159 testset->getSubRow(t, target_pos, target); 00160 if (!target.hasMissing() && !out.hasMissing()) 00161 { 00162 computeCostsFromOutputs(input, out, target, cost); 00163 errors(t) << cost; 00164 if (testcosts) testcosts->appendRow(cost); 00165 test_stats->update(cost); 00166 #ifdef DEBUG 00167 cout << "MovingAverage::test update test_stats pour t = " << t << endl; 00168 #endif 00169 } 00170 } 00171 if (pb) pb->update(t-start); 00172 } 00173 last_test_t = MAX(testset.length()-1, last_test_t); 00174 #ifdef DEBUG 00175 cout << "MovingAverage.last_test_t = " << last_test_t << endl; 00176 #endif 00177 00178 test_stats->finalize(); 00179 00180 if (pb) delete pb; 00181 } 00182 00183 void MovingAverage::computeCostsFromOutputs(const Vec& inputs, const Vec& outputs, 00184 const Vec& targets, Vec& costs) const 00185 { 00186 for (int i=0; i<cost_funcs.size(); i++) 00187 { 00188 if (cost_funcs[i]=="mse" || cost_funcs[i]=="MSE") 00189 costs << square(outputs-targets); 00190 else 00191 PLERROR("This cost_funcs is not implemented."); 00192 } 00193 } 00194 00195 TVec<string> MovingAverage::getTrainCostNames() const 00196 { return cost_funcs; } 00197 00198 TVec<string> MovingAverage::getTestCostNames() const 00199 { return getTrainCostNames(); } 00200 00201 void MovingAverage::forget() 00202 { inherited::forget(); } 00203 00204 /* 00205 void MovingAverage::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00206 { 00207 inherited::makeDeepCopyFromShallowCopy(copies); 00208 deepCopyField(cost_funcs, copies); 00209 } 00210 */ 00211 00212 00213 } // end of namespace PLearn 00214

Generated on Tue Aug 17 15:58:57 2004 for PLearn by doxygen 1.3.7