source: branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/TimeSeriesPrognosisSolutionBase.cs @ 8742

Last change on this file since 8742 was 8742, checked in by mkommend, 10 years ago

#1081: Merged trunk changes and fixed compilation errors due to the merge.

File size: 44.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.DataAnalysis {
31  [StorableClass]
32  public abstract class TimeSeriesPrognosisSolutionBase : RegressionSolutionBase, ITimeSeriesPrognosisSolution {
33    #region result names
34    protected const string TrainingDirectionalSymmetryResultName = "Average directional symmetry (training)";
35    protected const string TestDirectionalSymmetryResultName = "Average directional symmetry (test)";
36    protected const string TrainingWeightedDirectionalSymmetryResultName = "Average weighted directional symmetry (training)";
37    protected const string TestWeightedDirectionalSymmetryResultName = "Average weighted directional symmetry (test)";
38    protected const string TrainingTheilsUStatisticAR1ResultName = "Theil's U2 (AR1) (training)";
39    protected const string TestTheilsUStatisticLastResultName = "Theil's U2 (AR1) (test)";
40    protected const string TrainingTheilsUStatisticMeanResultName = "Theil's U2 (mean) (training)";
41    protected const string TestTheilsUStatisticMeanResultName = "Theil's U2 (mean) (test)";
42    protected const string TrainingTheilsUStatisticMovingAverageResultName = "Theil's U2 (moving average) (training)";
43    protected const string TestTheilsUStatisticMovingAverageResultName = "Theil's U2 (moving average) (test)";
44
45    protected const string PrognosisTrainingMeanSquaredErrorResultName = "Prognosis " + TrainingMeanSquaredErrorResultName;
46    protected const string PrognosisTestMeanSquaredErrorResultName = "Prognosis " + TestMeanSquaredErrorResultName;
47    protected const string PrognosisTrainingMeanAbsoluteErrorResultName = "Prognosis " + TrainingMeanAbsoluteErrorResultName;
48    protected const string PrognosisTestMeanAbsoluteErrorResultName = "Prognosis " + TestMeanAbsoluteErrorResultName;
49    protected const string PrognosisTrainingSquaredCorrelationResultName = "Prognosis " + TrainingSquaredCorrelationResultName;
50    protected const string PrognosisTestSquaredCorrelationResultName = "Prognosis " + TestSquaredCorrelationResultName;
51    protected const string PrognosisTrainingRelativeErrorResultName = "Prognosis " + TrainingRelativeErrorResultName;
52    protected const string PrognosisTestRelativeErrorResultName = "Prognosis " + TestRelativeErrorResultName;
53    protected const string PrognosisTrainingNormalizedMeanSquaredErrorResultName = "Prognosis " + TrainingNormalizedMeanSquaredErrorResultName;
54    protected const string PrognosisTestNormalizedMeanSquaredErrorResultName = "Prognosis " + TestNormalizedMeanSquaredErrorResultName;
55    protected const string PrognosisTrainingMeanErrorResultName = "Prognosis " + TrainingMeanErrorResultName;
56    protected const string PrognosisTestMeanErrorResultName = "Prognosis " + TestMeanErrorResultName;
57
58    protected const string PrognosisTrainingDirectionalSymmetryResultName = "Prognosis " + TrainingDirectionalSymmetryResultName;
59    protected const string PrognosisTestDirectionalSymmetryResultName = "Prognosis " + TestDirectionalSymmetryResultName;
60    protected const string PrognosisTrainingWeightedDirectionalSymmetryResultName = "Prognosis " + TrainingWeightedDirectionalSymmetryResultName;
61    protected const string PrognosisTestWeightedDirectionalSymmetryResultName = "Prognosis " + TestWeightedDirectionalSymmetryResultName;
62    protected const string PrognosisTrainingTheilsUStatisticAR1ResultName = "Prognosis " + TrainingTheilsUStatisticAR1ResultName;
63    protected const string PrognosisTestTheilsUStatisticAR1ResultName = "Prognosis " + TestTheilsUStatisticLastResultName;
64    protected const string PrognosisTrainingTheilsUStatisticMeanResultName = "Prognosis " + TrainingTheilsUStatisticMeanResultName;
65    protected const string PrognosisTestTheilsUStatisticMeanResultName = "Prognosis " + TestTheilsUStatisticMeanResultName;
66    protected const string PrognosisTrainingTheilsUStatisticMovingAverageResultName = "Prognosis " + TrainingTheilsUStatisticMovingAverageResultName;
67    protected const string PrognosisTestTheilsUStatisticMovingAverageResultName = "Prognosis " + TestTheilsUStatisticMovingAverageResultName;
68    #endregion
69
70    #region result descriptions
71    protected const string TrainingDirectionalSymmetryResultDescription = "The average directional symmetry of the forecasts of the model on the training partition";
72    protected const string TestDirectionalSymmetryResultDescription = "The average directional symmetry of the forecasts of the model on the test partition";
73    protected const string TrainingWeightedDirectionalSymmetryResultDescription = "The average weighted directional symmetry of the forecasts of the model on the training partition";
74    protected const string TestWeightedDirectionalSymmetryResultDescription = "The average weighted directional symmetry of the forecasts of the model on the test partition";
75    protected const string TrainingTheilsUStatisticAR1ResultDescription = "The Theil's U statistic (reference: AR1 model) of the forecasts of the model on the training partition";
76    protected const string TestTheilsUStatisticAR1ResultDescription = "The Theil's U statistic (reference: AR1 model) of the forecasts of the model on the test partition";
77    protected const string TrainingTheilsUStatisticMeanResultDescription = "The Theil's U statistic (reference: mean model) of the forecasts of the model on the training partition";
78    protected const string TestTheilsUStatisticMeanResultDescription = "The Theil's U statistic (reference: mean value) of the forecasts of the model on the test partition";
79    protected const string TrainingTheilsUStatisticMovingAverageResultDescription = "The Theil's U statistic (reference: moving average model) of the forecasts of the model on the training partition";
80    protected const string TestTheilsUStatisticMovingAverageResultDescription = "The Theil's U statistic (reference: moving average model) of the forecasts of the model on the test partition";
81
82    protected const string PrognosisTrainingMeanSquaredErrorResultDescription = TrainingMeanSquaredErrorResultDescription;
83    protected const string PrognosisTestMeanSquaredErrorResultDescription = TestMeanSquaredErrorResultDescription;
84    protected const string PrognosisTrainingMeanAbsoluteErrorResultDescription = TrainingMeanAbsoluteErrorResultDescription;
85    protected const string PrognosisTestMeanAbsoluteErrorResultDescription = TestMeanAbsoluteErrorResultDescription;
86    protected const string PrognosisTrainingSquaredCorrelationResultDescription = TrainingSquaredCorrelationResultDescription;
87    protected const string PrognosisTestSquaredCorrelationResultDescription = TestSquaredCorrelationResultDescription;
88    protected const string PrognosisTrainingRelativeErrorResultDescription = TrainingRelativeErrorResultDescription;
89    protected const string PrognosisTestRelativeErrorResultDescription = TestRelativeErrorResultDescription;
90    protected const string PrognosisTrainingNormalizedMeanSquaredErrorResultDescription = TrainingNormalizedMeanSquaredErrorResultDescription;
91    protected const string PrognosisTestNormalizedMeanSquaredErrorResultDescription = TestNormalizedMeanSquaredErrorResultDescription;
92    protected const string PrognosisTrainingMeanErrorResultDescription = TrainingMeanErrorResultDescription;
93    protected const string PrognosisTestMeanErrorResultDescription = TestMeanErrorResultDescription;
94
95    protected const string PrognosisTrainingDirectionalSymmetryResultDescription = TrainingDirectionalSymmetryResultDescription;
96    protected const string PrognosisTestDirectionalSymmetryResultDescription = TestDirectionalSymmetryResultDescription;
97    protected const string PrognosisTrainingWeightedDirectionalSymmetryResultDescription = TrainingWeightedDirectionalSymmetryResultDescription;
98    protected const string PrognosisTestWeightedDirectionalSymmetryResultDescription = TestWeightedDirectionalSymmetryResultDescription;
99    protected const string PrognosisTrainingTheilsUStatisticAR1ResultDescription = TrainingTheilsUStatisticAR1ResultDescription;
100    protected const string PrognosisTestTheilsUStatisticAR1ResultDescription = TestTheilsUStatisticAR1ResultDescription;
101    protected const string PrognosisTrainingTheilsUStatisticMeanResultDescription = TrainingTheilsUStatisticMeanResultDescription;
102    protected const string PrognosisTestTheilsUStatisticMeanResultDescription = TestTheilsUStatisticMeanResultDescription;
103    protected const string PrognosisTrainingTheilsUStatisticMovingAverageResultDescription = TrainingTheilsUStatisticMovingAverageResultDescription;
104    protected const string PrognosisTestTheilsUStatisticMovingAverageResultDescription = TestTheilsUStatisticMovingAverageResultDescription;
105    #endregion
106
107    public new ITimeSeriesPrognosisModel Model {
108      get { return (ITimeSeriesPrognosisModel)base.Model; }
109      protected set { base.Model = value; }
110    }
111
112    public new ITimeSeriesPrognosisProblemData ProblemData {
113      get { return (ITimeSeriesPrognosisProblemData)base.ProblemData; }
114      set { base.ProblemData = value; }
115    }
116
117    public abstract IEnumerable<IEnumerable<double>> GetPrognosedValues(IEnumerable<int> rows, IEnumerable<int> horizon);
118
119    #region Results
120    public double TrainingDirectionalSymmetry {
121      get { return ((DoubleValue)this[TrainingDirectionalSymmetryResultName].Value).Value; }
122      private set { ((DoubleValue)this[TrainingDirectionalSymmetryResultName].Value).Value = value; }
123    }
124    public double TestDirectionalSymmetry {
125      get { return ((DoubleValue)this[TestDirectionalSymmetryResultName].Value).Value; }
126      private set { ((DoubleValue)this[TestDirectionalSymmetryResultName].Value).Value = value; }
127    }
128    public double TrainingWeightedDirectionalSymmetry {
129      get { return ((DoubleValue)this[TrainingWeightedDirectionalSymmetryResultName].Value).Value; }
130      private set { ((DoubleValue)this[TrainingWeightedDirectionalSymmetryResultName].Value).Value = value; }
131    }
132    public double TestWeightedDirectionalSymmetry {
133      get { return ((DoubleValue)this[TestWeightedDirectionalSymmetryResultName].Value).Value; }
134      private set { ((DoubleValue)this[TestWeightedDirectionalSymmetryResultName].Value).Value = value; }
135    }
136    public double TrainingTheilsUStatisticAR1 {
137      get { return ((DoubleValue)this[TrainingTheilsUStatisticAR1ResultName].Value).Value; }
138      private set { ((DoubleValue)this[TrainingTheilsUStatisticAR1ResultName].Value).Value = value; }
139    }
140    public double TestTheilsUStatisticAR1 {
141      get { return ((DoubleValue)this[TestTheilsUStatisticLastResultName].Value).Value; }
142      private set { ((DoubleValue)this[TestTheilsUStatisticLastResultName].Value).Value = value; }
143    }
144    public double TrainingTheilsUStatisticMean {
145      get { return ((DoubleValue)this[TrainingTheilsUStatisticMeanResultName].Value).Value; }
146      private set { ((DoubleValue)this[TrainingTheilsUStatisticMeanResultName].Value).Value = value; }
147    }
148    public double TestTheilsUStatisticMean {
149      get { return ((DoubleValue)this[TestTheilsUStatisticMeanResultName].Value).Value; }
150      private set { ((DoubleValue)this[TestTheilsUStatisticMeanResultName].Value).Value = value; }
151    }
152    public double TrainingTheilsUStatisticMovingAverage {
153      get { return ((DoubleValue)this[TrainingTheilsUStatisticMovingAverageResultName].Value).Value; }
154      private set { ((DoubleValue)this[TrainingTheilsUStatisticMovingAverageResultName].Value).Value = value; }
155    }
156    public double TestTheilsUStatisticMovingAverage {
157      get { return ((DoubleValue)this[TestTheilsUStatisticMovingAverageResultName].Value).Value; }
158      private set { ((DoubleValue)this[TestTheilsUStatisticMovingAverageResultName].Value).Value = value; }
159    }
160
161    //prognosis results for different horizons
162    public double PrognosisTrainingMeanSquaredError {
163      get {
164        if (!ContainsKey(PrognosisTrainingMeanSquaredErrorResultName)) return double.NaN;
165        return ((DoubleValue)this[PrognosisTrainingMeanSquaredErrorResultName].Value).Value;
166      }
167      private set {
168        if (!ContainsKey(PrognosisTrainingMeanSquaredErrorResultName)) Add(new Result(PrognosisTrainingMeanSquaredErrorResultName, PrognosisTrainingMeanSquaredErrorResultDescription, new DoubleValue()));
169        ((DoubleValue)this[PrognosisTrainingMeanSquaredErrorResultName].Value).Value = value;
170      }
171    }
172
173    public double PrognosisTestMeanSquaredError {
174      get {
175        if (!ContainsKey(PrognosisTestMeanSquaredErrorResultName)) return double.NaN;
176        return ((DoubleValue)this[PrognosisTestMeanSquaredErrorResultName].Value).Value;
177      }
178      private set {
179        if (!ContainsKey(PrognosisTestMeanSquaredErrorResultName)) Add(new Result(PrognosisTestMeanSquaredErrorResultName, PrognosisTestMeanSquaredErrorResultDescription, new DoubleValue()));
180        ((DoubleValue)this[PrognosisTestMeanSquaredErrorResultName].Value).Value = value;
181      }
182    }
183
184    public double PrognosisTrainingMeanAbsoluteError {
185      get {
186        if (!ContainsKey(PrognosisTrainingMeanAbsoluteErrorResultName)) return double.NaN;
187        return ((DoubleValue)this[PrognosisTrainingMeanAbsoluteErrorResultName].Value).Value;
188      }
189      private set {
190        if (!ContainsKey(PrognosisTrainingMeanAbsoluteErrorResultName)) Add(new Result(PrognosisTrainingMeanAbsoluteErrorResultName, PrognosisTrainingMeanAbsoluteErrorResultDescription, new DoubleValue()));
191        ((DoubleValue)this[PrognosisTrainingMeanAbsoluteErrorResultName].Value).Value = value;
192      }
193    }
194
195    public double PrognosisTestMeanAbsoluteError {
196      get {
197        if (!ContainsKey(PrognosisTestMeanAbsoluteErrorResultName)) return double.NaN;
198        return ((DoubleValue)this[PrognosisTestMeanAbsoluteErrorResultName].Value).Value;
199      }
200      private set {
201        if (!ContainsKey(PrognosisTestMeanAbsoluteErrorResultName)) Add(new Result(PrognosisTestMeanAbsoluteErrorResultName, PrognosisTestMeanAbsoluteErrorResultDescription, new DoubleValue()));
202        ((DoubleValue)this[PrognosisTestMeanAbsoluteErrorResultName].Value).Value = value;
203      }
204    }
205
206    public double PrognosisTrainingRSquared {
207      get {
208        if (!ContainsKey(PrognosisTrainingSquaredCorrelationResultName)) return double.NaN;
209        return ((DoubleValue)this[PrognosisTrainingSquaredCorrelationResultName].Value).Value;
210      }
211      private set {
212        if (!ContainsKey(PrognosisTrainingSquaredCorrelationResultName)) Add(new Result(PrognosisTrainingSquaredCorrelationResultName, PrognosisTrainingSquaredCorrelationResultDescription, new DoubleValue()));
213        ((DoubleValue)this[PrognosisTrainingSquaredCorrelationResultName].Value).Value = value;
214      }
215    }
216
217    public double PrognosisTestRSquared {
218      get {
219        if (!ContainsKey(PrognosisTestSquaredCorrelationResultName)) return double.NaN;
220        return ((DoubleValue)this[PrognosisTestSquaredCorrelationResultName].Value).Value;
221      }
222      private set {
223        if (!ContainsKey(PrognosisTestSquaredCorrelationResultName)) Add(new Result(PrognosisTestSquaredCorrelationResultName, PrognosisTestSquaredCorrelationResultDescription, new DoubleValue()));
224        ((DoubleValue)this[PrognosisTestSquaredCorrelationResultName].Value).Value = value;
225      }
226    }
227
228    public double PrognosisTrainingRelativeError {
229      get {
230        if (!ContainsKey(PrognosisTrainingRelativeErrorResultName)) return double.NaN;
231        return ((DoubleValue)this[PrognosisTrainingRelativeErrorResultName].Value).Value;
232      }
233      private set {
234        if (!ContainsKey(PrognosisTrainingRelativeErrorResultName)) Add(new Result(PrognosisTrainingRelativeErrorResultName, PrognosisTrainingRelativeErrorResultDescription, new DoubleValue()));
235        ((DoubleValue)this[PrognosisTrainingRelativeErrorResultName].Value).Value = value;
236      }
237    }
238
239    public double PrognosisTestRelativeError {
240      get {
241        if (!ContainsKey(PrognosisTestRelativeErrorResultName)) return double.NaN;
242        return ((DoubleValue)this[PrognosisTestRelativeErrorResultName].Value).Value;
243      }
244      private set {
245        if (!ContainsKey(PrognosisTestRelativeErrorResultName)) Add(new Result(PrognosisTestRelativeErrorResultName, PrognosisTestRelativeErrorResultDescription, new DoubleValue()));
246        ((DoubleValue)this[PrognosisTestRelativeErrorResultName].Value).Value = value;
247      }
248    }
249
250    public double PrognosisTrainingNormalizedMeanSquaredError {
251      get {
252        if (!ContainsKey(PrognosisTrainingNormalizedMeanSquaredErrorResultName)) return double.NaN;
253        return ((DoubleValue)this[PrognosisTrainingNormalizedMeanSquaredErrorResultName].Value).Value;
254      }
255      private set {
256        if (!ContainsKey(PrognosisTrainingNormalizedMeanSquaredErrorResultName)) Add(new Result(PrognosisTrainingNormalizedMeanSquaredErrorResultName, PrognosisTrainingNormalizedMeanSquaredErrorResultDescription, new DoubleValue()));
257        ((DoubleValue)this[PrognosisTrainingNormalizedMeanSquaredErrorResultName].Value).Value = value;
258      }
259    }
260
261    public double PrognosisTestNormalizedMeanSquaredError {
262      get {
263        if (!ContainsKey(PrognosisTestNormalizedMeanSquaredErrorResultName)) return double.NaN;
264        return ((DoubleValue)this[PrognosisTestNormalizedMeanSquaredErrorResultName].Value).Value;
265      }
266      private set {
267        if (!ContainsKey(PrognosisTestNormalizedMeanSquaredErrorResultName)) Add(new Result(PrognosisTestNormalizedMeanSquaredErrorResultName, PrognosisTestNormalizedMeanSquaredErrorResultDescription, new DoubleValue()));
268        ((DoubleValue)this[PrognosisTestNormalizedMeanSquaredErrorResultName].Value).Value = value;
269      }
270    }
271
272    public double PrognosisTrainingMeanError {
273      get {
274        if (!ContainsKey(PrognosisTrainingMeanErrorResultName)) return double.NaN;
275        return ((DoubleValue)this[PrognosisTrainingMeanErrorResultName].Value).Value;
276      }
277      private set {
278        if (!ContainsKey(PrognosisTrainingMeanErrorResultName)) Add(new Result(PrognosisTrainingMeanErrorResultName, PrognosisTrainingMeanErrorResultDescription, new DoubleValue()));
279        ((DoubleValue)this[PrognosisTrainingMeanErrorResultName].Value).Value = value;
280      }
281    }
282
283    public double PrognosisTestMeanError {
284      get {
285        if (!ContainsKey(PrognosisTestMeanErrorResultName)) return double.NaN;
286        return ((DoubleValue)this[PrognosisTestMeanErrorResultName].Value).Value;
287      }
288      private set {
289        if (!ContainsKey(PrognosisTestMeanErrorResultName)) Add(new Result(PrognosisTestMeanErrorResultName, PrognosisTestMeanErrorResultDescription, new DoubleValue()));
290        ((DoubleValue)this[PrognosisTestMeanErrorResultName].Value).Value = value;
291      }
292    }
293
294
295    public double PrognosisTrainingDirectionalSymmetry {
296      get {
297        if (!ContainsKey(PrognosisTrainingDirectionalSymmetryResultName)) return double.NaN;
298        return ((DoubleValue)this[PrognosisTrainingDirectionalSymmetryResultName].Value).Value;
299      }
300      private set {
301        if (!ContainsKey(PrognosisTrainingDirectionalSymmetryResultName)) Add(new Result(PrognosisTrainingDirectionalSymmetryResultName, PrognosisTrainingDirectionalSymmetryResultDescription, new DoubleValue()));
302        ((DoubleValue)this[PrognosisTrainingDirectionalSymmetryResultName].Value).Value = value;
303      }
304    }
305    public double PrognosisTestDirectionalSymmetry {
306      get {
307        if (!ContainsKey(PrognosisTestDirectionalSymmetryResultName)) return double.NaN;
308        return ((DoubleValue)this[PrognosisTestDirectionalSymmetryResultName].Value).Value;
309      }
310      private set {
311        if (!ContainsKey(PrognosisTestDirectionalSymmetryResultName)) Add(new Result(PrognosisTestDirectionalSymmetryResultName, PrognosisTestDirectionalSymmetryResultDescription, new DoubleValue()));
312        ((DoubleValue)this[PrognosisTestDirectionalSymmetryResultName].Value).Value = value;
313      }
314    }
315    public double PrognosisTrainingWeightedDirectionalSymmetry {
316      get {
317        if (!ContainsKey(PrognosisTrainingWeightedDirectionalSymmetryResultName)) return double.NaN;
318        return ((DoubleValue)this[PrognosisTrainingWeightedDirectionalSymmetryResultName].Value).Value;
319      }
320      private set {
321        if (!ContainsKey(PrognosisTrainingWeightedDirectionalSymmetryResultName)) Add(new Result(PrognosisTrainingWeightedDirectionalSymmetryResultName, PrognosisTrainingWeightedDirectionalSymmetryResultDescription, new DoubleValue()));
322        ((DoubleValue)this[PrognosisTrainingWeightedDirectionalSymmetryResultName].Value).Value = value;
323      }
324    }
325    public double PrognosisTestWeightedDirectionalSymmetry {
326      get {
327        if (!ContainsKey(PrognosisTestWeightedDirectionalSymmetryResultName)) return double.NaN;
328        return ((DoubleValue)this[PrognosisTestWeightedDirectionalSymmetryResultName].Value).Value;
329      }
330      private set {
331        if (!ContainsKey(PrognosisTestWeightedDirectionalSymmetryResultName)) Add(new Result(PrognosisTestWeightedDirectionalSymmetryResultName, PrognosisTestWeightedDirectionalSymmetryResultDescription, new DoubleValue()));
332        ((DoubleValue)this[PrognosisTestWeightedDirectionalSymmetryResultName].Value).Value = value;
333      }
334    }
335    public double PrognosisTrainingTheilsUStatisticAR1 {
336      get {
337        if (!ContainsKey(PrognosisTrainingTheilsUStatisticAR1ResultName)) return double.NaN;
338        return ((DoubleValue)this[PrognosisTrainingTheilsUStatisticAR1ResultName].Value).Value;
339      }
340      private set {
341        if (!ContainsKey(PrognosisTrainingTheilsUStatisticAR1ResultName)) Add(new Result(PrognosisTrainingTheilsUStatisticAR1ResultName, PrognosisTrainingTheilsUStatisticAR1ResultDescription, new DoubleValue()));
342        ((DoubleValue)this[PrognosisTrainingTheilsUStatisticAR1ResultName].Value).Value = value;
343      }
344    }
345    public double PrognosisTestTheilsUStatisticAR1 {
346      get {
347        if (!ContainsKey(PrognosisTestTheilsUStatisticAR1ResultName)) return double.NaN;
348        return ((DoubleValue)this[PrognosisTestTheilsUStatisticAR1ResultName].Value).Value;
349      }
350      private set {
351        if (!ContainsKey(PrognosisTestTheilsUStatisticAR1ResultName)) Add(new Result(PrognosisTestTheilsUStatisticAR1ResultName, PrognosisTestTheilsUStatisticAR1ResultDescription, new DoubleValue()));
352        ((DoubleValue)this[PrognosisTestTheilsUStatisticAR1ResultName].Value).Value = value;
353      }
354    }
355    public double PrognosisTrainingTheilsUStatisticMean {
356      get {
357        if (!ContainsKey(PrognosisTrainingTheilsUStatisticMeanResultName)) return double.NaN;
358        return ((DoubleValue)this[PrognosisTrainingTheilsUStatisticMeanResultName].Value).Value;
359      }
360      private set {
361        if (!ContainsKey(PrognosisTrainingTheilsUStatisticMeanResultName)) Add(new Result(PrognosisTrainingTheilsUStatisticMeanResultName, PrognosisTrainingTheilsUStatisticMeanResultDescription, new DoubleValue()));
362        ((DoubleValue)this[PrognosisTrainingTheilsUStatisticMeanResultName].Value).Value = value;
363      }
364    }
365    public double PrognosisTestTheilsUStatisticMean {
366      get {
367        if (!ContainsKey(PrognosisTestTheilsUStatisticMeanResultName)) return double.NaN;
368        return ((DoubleValue)this[PrognosisTestTheilsUStatisticMeanResultName].Value).Value;
369      }
370      private set {
371        if (!ContainsKey(PrognosisTestTheilsUStatisticMeanResultName)) Add(new Result(PrognosisTestTheilsUStatisticMeanResultName, PrognosisTestTheilsUStatisticMeanResultDescription, new DoubleValue()));
372        ((DoubleValue)this[PrognosisTestTheilsUStatisticMeanResultName].Value).Value = value;
373      }
374    }
375    public double PrognosisTrainingTheilsUStatisticMovingAverage {
376      get {
377        if (!ContainsKey(PrognosisTrainingTheilsUStatisticMovingAverageResultName)) return double.NaN;
378        return ((DoubleValue)this[PrognosisTrainingTheilsUStatisticMovingAverageResultName].Value).Value;
379      }
380      private set {
381        if (!ContainsKey(PrognosisTrainingTheilsUStatisticMovingAverageResultName)) Add(new Result(PrognosisTrainingTheilsUStatisticMovingAverageResultName, PrognosisTrainingTheilsUStatisticMovingAverageResultDescription, new DoubleValue()));
382        ((DoubleValue)this[PrognosisTrainingTheilsUStatisticMovingAverageResultName].Value).Value = value;
383      }
384    }
385    public double PrognosisTestTheilsUStatisticMovingAverage {
386      get {
387        if (!ContainsKey(PrognosisTestTheilsUStatisticMovingAverageResultName)) return double.NaN;
388        return ((DoubleValue)this[PrognosisTestTheilsUStatisticMovingAverageResultName].Value).Value;
389      }
390      private set {
391        if (!ContainsKey(PrognosisTestTheilsUStatisticMovingAverageResultName)) Add(new Result(PrognosisTestTheilsUStatisticMovingAverageResultName, PrognosisTestTheilsUStatisticMovingAverageResultDescription, new DoubleValue()));
392        ((DoubleValue)this[PrognosisTestTheilsUStatisticMovingAverageResultName].Value).Value = value;
393      }
394    }
395    #endregion
396
397    public override IEnumerable<double> EstimatedValues {
398      get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
399    }
400    public override IEnumerable<double> EstimatedTrainingValues {
401      get { return GetEstimatedValues(ProblemData.TrainingIndices); }
402    }
403    public override IEnumerable<double> EstimatedTestValues {
404      get { return GetEstimatedValues(ProblemData.TestIndices); }
405    }
406    public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
407      return Model.GetEstimatedValues(ProblemData.Dataset, rows);
408    }
409
410    [StorableConstructor]
411    protected TimeSeriesPrognosisSolutionBase(bool deserializing) : base(deserializing) { }
412    protected TimeSeriesPrognosisSolutionBase(TimeSeriesPrognosisSolutionBase original, Cloner cloner) : base(original, cloner) { }
413    protected TimeSeriesPrognosisSolutionBase(ITimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData)
414      : base(model, problemData) {
415      Add(new Result(TrainingDirectionalSymmetryResultName, TrainingDirectionalSymmetryResultDescription, new DoubleValue()));
416      Add(new Result(TestDirectionalSymmetryResultName, TestDirectionalSymmetryResultDescription, new DoubleValue()));
417      Add(new Result(TrainingWeightedDirectionalSymmetryResultName, TrainingWeightedDirectionalSymmetryResultDescription, new DoubleValue()));
418      Add(new Result(TestWeightedDirectionalSymmetryResultName, TestWeightedDirectionalSymmetryResultDescription, new DoubleValue()));
419      Add(new Result(TrainingTheilsUStatisticAR1ResultName, TrainingTheilsUStatisticAR1ResultDescription, new DoubleValue()));
420      Add(new Result(TestTheilsUStatisticLastResultName, TestTheilsUStatisticAR1ResultDescription, new DoubleValue()));
421      Add(new Result(TrainingTheilsUStatisticMeanResultName, TrainingTheilsUStatisticMeanResultDescription, new DoubleValue()));
422      Add(new Result(TestTheilsUStatisticMeanResultName, TestTheilsUStatisticMeanResultDescription, new DoubleValue()));
423      Add(new Result(TrainingTheilsUStatisticMovingAverageResultName, TrainingTheilsUStatisticMovingAverageResultDescription, new DoubleValue()));
424      Add(new Result(TestTheilsUStatisticMovingAverageResultName, TestTheilsUStatisticMovingAverageResultDescription, new DoubleValue()));
425    }
426
427    protected override void RecalculateResults() {
428      base.RecalculateResults();
429      CalculateTimeSeriesResults();
430      CalculateTimeSeriesResults(ProblemData.TrainingHorizon, ProblemData.TestHorizon);
431    }
432
433    protected void CalculateTimeSeriesResults() {
434      OnlineCalculatorError errorState;
435      double trainingMean = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).Average();
436      var meanModel = new ConstantTimeSeriesPrognosisModel(trainingMean);
437
438      double alpha, beta;
439      IEnumerable<double> trainingStartValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices.Select(r => r - 1).Where(r => r > 0)).ToList();
440      OnlineLinearScalingParameterCalculator.Calculate(ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices.Where(x => x > 0)), trainingStartValues, out alpha, out beta, out errorState);
441      var AR1model = new TimeSeriesPrognosisAutoRegressiveModel(ProblemData.TargetVariable, new double[] { beta }, alpha);
442
443      //MA model
444      const int movingAverageWindowSize = 10;
445      var movingAverageModel = new TimeSeriesPrognosisMovingAverageModel(movingAverageWindowSize, ProblemData.TargetVariable);
446
447      #region Calculate training quality measures
448      IEnumerable<double> trainingTargetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
449      IEnumerable<double> trainingEstimatedValues = EstimatedTrainingValues.ToList();
450      IEnumerable<double> trainingMeanModelPredictions = meanModel.GetEstimatedValues(ProblemData.Dataset, ProblemData.TrainingIndices).ToList();
451      IEnumerable<double> trainingAR1ModelPredictions = AR1model.GetEstimatedValues(ProblemData.Dataset, ProblemData.TrainingIndices).ToList();
452      IEnumerable<double> trainingMovingAverageModelPredictions = movingAverageModel.GetEstimatedValues(ProblemData.Dataset, ProblemData.TrainingIndices).ToList();
453
454      TrainingDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingEstimatedValues, out errorState);
455      TrainingDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TrainingDirectionalSymmetry : 0.0;
456      TrainingWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingEstimatedValues, out errorState);
457      TrainingWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TrainingWeightedDirectionalSymmetry : 0.0;
458      TrainingTheilsUStatisticAR1 = OnlineTheilsUStatisticCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingAR1ModelPredictions, trainingEstimatedValues, out errorState);
459      TrainingTheilsUStatisticAR1 = errorState == OnlineCalculatorError.None ? TrainingTheilsUStatisticAR1 : double.PositiveInfinity;
460      TrainingTheilsUStatisticMean = OnlineTheilsUStatisticCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingMeanModelPredictions, trainingEstimatedValues, out errorState);
461      TrainingTheilsUStatisticMean = errorState == OnlineCalculatorError.None ? TrainingTheilsUStatisticMean : double.PositiveInfinity;
462      TrainingTheilsUStatisticMovingAverage = OnlineTheilsUStatisticCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingMovingAverageModelPredictions, trainingEstimatedValues, out errorState);
463      TrainingTheilsUStatisticMovingAverage = errorState == OnlineCalculatorError.None ? TrainingTheilsUStatisticMovingAverage : double.PositiveInfinity;
464      #endregion
465
466      #region Calculate test quality measures
467      IEnumerable<double> testTargetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices).ToList();
468      IEnumerable<double> testEstimatedValues = EstimatedTestValues.ToList();
469      IEnumerable<double> testMeanModelPredictions = meanModel.GetEstimatedValues(ProblemData.Dataset, ProblemData.TestIndices).ToList();
470      IEnumerable<double> testAR1ModelPredictions = AR1model.GetEstimatedValues(ProblemData.Dataset, ProblemData.TestIndices).ToList();
471      IEnumerable<double> testMovingAverageModelPredictions = movingAverageModel.GetEstimatedValues(ProblemData.Dataset, ProblemData.TestIndices).ToList();
472
473      TestDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(testTargetValues.First(), testTargetValues, testEstimatedValues, out errorState);
474      TestDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TestDirectionalSymmetry : 0.0;
475      TestWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(testTargetValues.First(), testTargetValues, testEstimatedValues, out errorState);
476      TestWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TestWeightedDirectionalSymmetry : 0.0;
477      TestTheilsUStatisticAR1 = OnlineTheilsUStatisticCalculator.Calculate(testTargetValues.First(), testTargetValues, testAR1ModelPredictions, testEstimatedValues, out errorState);
478      TestTheilsUStatisticAR1 = errorState == OnlineCalculatorError.None ? TestTheilsUStatisticAR1 : double.PositiveInfinity;
479      TestTheilsUStatisticMean = OnlineTheilsUStatisticCalculator.Calculate(testTargetValues.First(), testTargetValues, testMeanModelPredictions, testEstimatedValues, out errorState);
480      TestTheilsUStatisticMean = errorState == OnlineCalculatorError.None ? TestTheilsUStatisticMean : double.PositiveInfinity;
481      TestTheilsUStatisticMovingAverage = OnlineTheilsUStatisticCalculator.Calculate(testTargetValues.First(), testTargetValues, testMovingAverageModelPredictions, testEstimatedValues, out errorState);
482      TestTheilsUStatisticMovingAverage = errorState == OnlineCalculatorError.None ? TestTheilsUStatisticMovingAverage : double.PositiveInfinity;
483      #endregion
484    }
485
486    protected void CalculateTimeSeriesResults(int trainingHorizon, int testHorizon) {
487      OnlineCalculatorError errorState;
488      //mean model
489      double trainingMean = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).Average();
490      var meanModel = new ConstantTimeSeriesPrognosisModel(trainingMean);
491
492      //AR1 model
493      double alpha, beta;
494      IEnumerable<double> trainingStartValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices.Select(r => r - 1).Where(r => r > 0)).ToList();
495      OnlineLinearScalingParameterCalculator.Calculate(ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices.Where(x => x > 0)), trainingStartValues, out alpha, out beta, out errorState);
496      var AR1model = new TimeSeriesPrognosisAutoRegressiveModel(ProblemData.TargetVariable, new double[] { beta }, alpha);
497
498      //MA model
499      const int movingAverageWindowSize = 10;
500      var MovingAverageModel = new TimeSeriesPrognosisMovingAverageModel(movingAverageWindowSize, ProblemData.TargetVariable);
501
502      #region Calculate training quality measures
503      if (trainingHorizon != 1) {
504        var trainingHorizions = ProblemData.TrainingIndices.Select(r => Math.Min(trainingHorizon, ProblemData.TrainingPartition.End - r)).ToList();
505        IEnumerable<IEnumerable<double>> trainingTargetValues = ProblemData.TrainingIndices.Zip(trainingHorizions, Enumerable.Range).Select(r => ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, r)).ToList();
506        IEnumerable<IEnumerable<double>> trainingEstimatedValues = Model.GetPrognosedValues(ProblemData.Dataset, ProblemData.TrainingIndices, trainingHorizions).ToList();
507        IEnumerable<IEnumerable<double>> trainingMeanModelPredictions = meanModel.GetPrognosedValues(ProblemData.Dataset, ProblemData.TrainingIndices, trainingHorizions).ToList();
508        IEnumerable<IEnumerable<double>> trainingAR1ModelPredictions = AR1model.GetPrognosedValues(ProblemData.Dataset, ProblemData.TrainingIndices, trainingHorizions).ToList();
509        IEnumerable<IEnumerable<double>> trainingMovingAverageModelPredictions = MovingAverageModel.GetPrognosedValues(ProblemData.Dataset, ProblemData.TrainingIndices, trainingHorizions).ToList();
510
511        IEnumerable<double> originalTrainingValues = trainingTargetValues.SelectMany(x => x).ToList();
512        IEnumerable<double> estimatedTrainingValues = trainingEstimatedValues.SelectMany(x => x).ToList();
513
514        double trainingMSE = OnlineMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
515        PrognosisTrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMSE : double.NaN;
516        double trainingMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
517        PrognosisTrainingMeanAbsoluteError = errorState == OnlineCalculatorError.None ? trainingMAE : double.NaN;
518        double trainingR2 = OnlinePearsonsRSquaredCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
519        PrognosisTrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR2 : double.NaN;
520        double trainingRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
521        PrognosisTrainingRelativeError = errorState == OnlineCalculatorError.None ? trainingRelError : double.NaN;
522        double trainingNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
523        PrognosisTrainingNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingNMSE : double.NaN;
524        double trainingME = OnlineMeanErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
525        PrognosisTrainingMeanError = errorState == OnlineCalculatorError.None ? trainingME : double.NaN;
526
527        PrognosisTrainingDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(trainingStartValues, trainingTargetValues, trainingEstimatedValues, out errorState);
528        PrognosisTrainingDirectionalSymmetry = errorState == OnlineCalculatorError.None ? PrognosisTrainingDirectionalSymmetry : 0.0;
529        PrognosisTrainingWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(trainingStartValues, trainingTargetValues, trainingEstimatedValues, out errorState);
530        PrognosisTrainingWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? PrognosisTrainingWeightedDirectionalSymmetry : 0.0;
531        PrognosisTrainingTheilsUStatisticAR1 = OnlineTheilsUStatisticCalculator.Calculate(trainingStartValues, trainingTargetValues, trainingAR1ModelPredictions, trainingEstimatedValues, out errorState);
532        PrognosisTrainingTheilsUStatisticAR1 = errorState == OnlineCalculatorError.None ? PrognosisTrainingTheilsUStatisticAR1 : double.PositiveInfinity;
533        PrognosisTrainingTheilsUStatisticMean = OnlineTheilsUStatisticCalculator.Calculate(trainingStartValues, trainingTargetValues, trainingMeanModelPredictions, trainingEstimatedValues, out errorState);
534        PrognosisTrainingTheilsUStatisticMean = errorState == OnlineCalculatorError.None ? PrognosisTrainingTheilsUStatisticMean : double.PositiveInfinity;
535        PrognosisTrainingTheilsUStatisticMovingAverage = OnlineTheilsUStatisticCalculator.Calculate(trainingStartValues, trainingTargetValues, trainingMovingAverageModelPredictions, trainingEstimatedValues, out errorState);
536        PrognosisTrainingTheilsUStatisticMovingAverage = errorState == OnlineCalculatorError.None ? PrognosisTrainingTheilsUStatisticMovingAverage : double.PositiveInfinity;
537      }
538
539      #endregion
540
541      #region  Calculate test quality measures
542      if (testHorizon != 1) {
543        var testHorizions = ProblemData.TestIndices.Select(r => Math.Min(testHorizon, ProblemData.TestPartition.End - r)).ToList();
544        IEnumerable<IEnumerable<double>> testTargetValues = ProblemData.TestIndices.Zip(testHorizions, Enumerable.Range).Select(r => ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, r)).ToList();
545        IEnumerable<IEnumerable<double>> testEstimatedValues = Model.GetPrognosedValues(ProblemData.Dataset, ProblemData.TestIndices, testHorizions).ToList();
546        IEnumerable<double> testStartValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices.Select(r => r - 1).Where(r => r > 0)).ToList();
547        IEnumerable<IEnumerable<double>> testMeanModelPredictions = meanModel.GetPrognosedValues(ProblemData.Dataset, ProblemData.TestIndices, testHorizions).ToList();
548        IEnumerable<IEnumerable<double>> testAR1ModelPredictions = AR1model.GetPrognosedValues(ProblemData.Dataset, ProblemData.TestIndices, testHorizions).ToList();
549        IEnumerable<IEnumerable<double>> testMovingAverageModelPredictions = MovingAverageModel.GetPrognosedValues(ProblemData.Dataset, ProblemData.TestIndices, testHorizions).ToList();
550
551        IEnumerable<double> originalTestValues = testTargetValues.SelectMany(x => x).ToList();
552        IEnumerable<double> estimatedTestValues = testEstimatedValues.SelectMany(x => x).ToList();
553
554        double testMSE = OnlineMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
555        PrognosisTestMeanSquaredError = errorState == OnlineCalculatorError.None ? testMSE : double.NaN;
556        double testMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
557        PrognosisTestMeanAbsoluteError = errorState == OnlineCalculatorError.None ? testMAE : double.NaN;
558        double testR2 = OnlinePearsonsRSquaredCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
559        PrognosisTestRSquared = errorState == OnlineCalculatorError.None ? testR2 : double.NaN;
560        double testRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
561        PrognosisTestRelativeError = errorState == OnlineCalculatorError.None ? testRelError : double.NaN;
562        double testNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
563        PrognosisTestNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? testNMSE : double.NaN;
564        double testME = OnlineMeanErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
565        PrognosisTestMeanError = errorState == OnlineCalculatorError.None ? testME : double.NaN;
566
567        PrognosisTestDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(testStartValues, testTargetValues, testEstimatedValues, out errorState);
568        PrognosisTestDirectionalSymmetry = errorState == OnlineCalculatorError.None ? PrognosisTestDirectionalSymmetry : 0.0;
569        PrognosisTestWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(testStartValues, testTargetValues, testEstimatedValues, out errorState);
570        PrognosisTestWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? PrognosisTestWeightedDirectionalSymmetry : 0.0;
571        PrognosisTestTheilsUStatisticAR1 = OnlineTheilsUStatisticCalculator.Calculate(testStartValues, testTargetValues, testAR1ModelPredictions, testEstimatedValues, out errorState);
572        PrognosisTestTheilsUStatisticAR1 = errorState == OnlineCalculatorError.None ? PrognosisTestTheilsUStatisticAR1 : double.PositiveInfinity;
573        PrognosisTestTheilsUStatisticMean = OnlineTheilsUStatisticCalculator.Calculate(testStartValues, testTargetValues, testMeanModelPredictions, testEstimatedValues, out errorState);
574        PrognosisTestTheilsUStatisticMean = errorState == OnlineCalculatorError.None ? PrognosisTestTheilsUStatisticMean : double.PositiveInfinity;
575        PrognosisTestTheilsUStatisticMovingAverage = OnlineTheilsUStatisticCalculator.Calculate(testStartValues, testTargetValues, testMovingAverageModelPredictions, testEstimatedValues, out errorState);
576        PrognosisTestTheilsUStatisticMovingAverage = errorState == OnlineCalculatorError.None ? PrognosisTestTheilsUStatisticMovingAverage : double.PositiveInfinity;
577      }
578
579      #endregion
580    }
581  }
582}
Note: See TracBrowser for help on using the repository browser.