00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
#include "MovingAverage.h"
00039
00040
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
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
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
00206
00207
00208
00209
00210
00211
00212
00213 }
00214