source: branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/TimeSeriesPrognosisResults.cs @ 8750

Last change on this file since 8750 was 8750, checked in by mkommend, 7 years ago

#1081: Extracted prognosis results into separate class and added a view for them.

File size: 33.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.DataAnalysis {
32  [StorableClass]
33  [Item("Prognosis Results", "Represents a collection of time series prognosis results.")]
34  public class TimeSeriesPrognosisResults : ResultCollection {
35    #region result names
36    protected const string PrognosisTrainingMeanSquaredErrorResultName = "Mean squared error (training)";
37    protected const string PrognosisTestMeanSquaredErrorResultName = "Mean squared error (test)";
38    protected const string PrognosisTrainingMeanAbsoluteErrorResultName = "Mean absolute error (training)";
39    protected const string PrognosisTestMeanAbsoluteErrorResultName = "Mean absolute error (test)";
40    protected const string PrognosisTrainingSquaredCorrelationResultName = "Pearson's R² (training)";
41    protected const string PrognosisTestSquaredCorrelationResultName = "Pearson's R² (test)";
42    protected const string PrognosisTrainingRelativeErrorResultName = "Average relative error (training)";
43    protected const string PrognosisTestRelativeErrorResultName = "Average relative error (test)";
44    protected const string PrognosisTrainingNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (training)";
45    protected const string PrognosisTestNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (test)";
46    protected const string PrognosisTrainingMeanErrorResultName = "Mean error (training)";
47    protected const string PrognosisTestMeanErrorResultName = "Mean error (test)";
48
49    protected const string PrognosisTrainingDirectionalSymmetryResultName = "Average directional symmetry (training)";
50    protected const string PrognosisTestDirectionalSymmetryResultName = "Average directional symmetry (test)";
51    protected const string PrognosisTrainingWeightedDirectionalSymmetryResultName = "Average weighted directional symmetry (training)";
52    protected const string PrognosisTestWeightedDirectionalSymmetryResultName = "Average weighted directional symmetry (test)";
53    protected const string PrognosisTrainingTheilsUStatisticAR1ResultName = "Theil's U2 (AR1) (training)";
54    protected const string PrognosisTestTheilsUStatisticAR1ResultName = "Theil's U2 (AR1) (test)";
55    protected const string PrognosisTrainingTheilsUStatisticMeanResultName = "Theil's U2 (mean) (training)";
56    protected const string PrognosisTestTheilsUStatisticMeanResultName = "Theil's U2 (mean) (test)";
57    protected const string PrognosisTrainingTheilsUStatisticMovingAverageResultName = "Theil's U2 (moving average) (training)";
58    protected const string PrognosisTestTheilsUStatisticMovingAverageResultName = "Theil's U2 (moving average) (test)";
59    #endregion
60
61    #region result descriptions
62    protected const string PrognosisTrainingMeanSquaredErrorResultDescription = "Mean of squared errors of the model on the training partition";
63    protected const string PrognosisTestMeanSquaredErrorResultDescription = "Mean of squared errors of the model on the test partition";
64    protected const string PrognosisTrainingMeanAbsoluteErrorResultDescription = "Mean of absolute errors of the model on the training partition";
65    protected const string PrognosisTestMeanAbsoluteErrorResultDescription = "Mean of absolute errors of the model on the test partition";
66    protected const string PrognosisTrainingSquaredCorrelationResultDescription = "Squared Pearson's correlation coefficient of the model output and the actual values on the training partition";
67    protected const string PrognosisTestSquaredCorrelationResultDescription = "Squared Pearson's correlation coefficient of the model output and the actual values on the test partition";
68    protected const string PrognosisTrainingRelativeErrorResultDescription = "Average of the relative errors of the model output and the actual values on the training partition";
69    protected const string PrognosisTestRelativeErrorResultDescription = "Average of the relative errors of the model output and the actual values on the test partition";
70    protected const string PrognosisTrainingNormalizedMeanSquaredErrorResultDescription = "Normalized mean of squared errors of the model on the training partition";
71    protected const string PrognosisTestNormalizedMeanSquaredErrorResultDescription = "Normalized mean of squared errors of the model on the test partition";
72    protected const string PrognosisTrainingMeanErrorResultDescription = "Mean of errors of the model on the training partition";
73    protected const string PrognosisTestMeanErrorResultDescription = "Mean of errors of the model on the test partition";
74
75    protected const string PrognosisTrainingDirectionalSymmetryResultDescription = "The average directional symmetry of the forecasts of the model on the training partition";
76    protected const string PrognosisTestDirectionalSymmetryResultDescription = "The average directional symmetry of the forecasts of the model on the test partition";
77    protected const string PrognosisTrainingWeightedDirectionalSymmetryResultDescription = "The average weighted directional symmetry of the forecasts of the model on the training partition";
78    protected const string PrognosisTestWeightedDirectionalSymmetryResultDescription = "The average weighted directional symmetry of the forecasts of the model on the test partition";
79    protected const string PrognosisTrainingTheilsUStatisticAR1ResultDescription = "The Theil's U statistic (reference: AR1 model) of the forecasts of the model on the training partition";
80    protected const string PrognosisTestTheilsUStatisticAR1ResultDescription = "The Theil's U statistic (reference: AR1 model) of the forecasts of the model on the test partition";
81    protected const string PrognosisTrainingTheilsUStatisticMeanResultDescription = "The Theil's U statistic (reference: mean model) of the forecasts of the model on the training partition";
82    protected const string PrognosisTestTheilsUStatisticMeanResultDescription = "The Theil's U statistic (reference: mean value) of the forecasts of the model on the test partition";
83    protected const string PrognosisTrainingTheilsUStatisticMovingAverageResultDescription = "The Theil's U statistic (reference: moving average model) of the forecasts of the model on the training partition";
84    protected const string PrognosisTestTheilsUStatisticMovingAverageResultDescription = "The Theil's U statistic (reference: moving average model) of the forecasts of the model on the test partition";
85    #endregion
86
87    #region result properties
88    //prognosis results for different horizons
89    public double PrognosisTrainingMeanSquaredError {
90      get {
91        if (!ContainsKey(PrognosisTrainingMeanSquaredErrorResultName)) return double.NaN;
92        return ((DoubleValue)this[PrognosisTrainingMeanSquaredErrorResultName].Value).Value;
93      }
94      private set {
95        if (!ContainsKey(PrognosisTrainingMeanSquaredErrorResultName)) Add(new Result(PrognosisTrainingMeanSquaredErrorResultName, PrognosisTrainingMeanSquaredErrorResultDescription, new DoubleValue()));
96        ((DoubleValue)this[PrognosisTrainingMeanSquaredErrorResultName].Value).Value = value;
97      }
98    }
99
100    public double PrognosisTestMeanSquaredError {
101      get {
102        if (!ContainsKey(PrognosisTestMeanSquaredErrorResultName)) return double.NaN;
103        return ((DoubleValue)this[PrognosisTestMeanSquaredErrorResultName].Value).Value;
104      }
105      private set {
106        if (!ContainsKey(PrognosisTestMeanSquaredErrorResultName)) Add(new Result(PrognosisTestMeanSquaredErrorResultName, PrognosisTestMeanSquaredErrorResultDescription, new DoubleValue()));
107        ((DoubleValue)this[PrognosisTestMeanSquaredErrorResultName].Value).Value = value;
108      }
109    }
110
111    public double PrognosisTrainingMeanAbsoluteError {
112      get {
113        if (!ContainsKey(PrognosisTrainingMeanAbsoluteErrorResultName)) return double.NaN;
114        return ((DoubleValue)this[PrognosisTrainingMeanAbsoluteErrorResultName].Value).Value;
115      }
116      private set {
117        if (!ContainsKey(PrognosisTrainingMeanAbsoluteErrorResultName)) Add(new Result(PrognosisTrainingMeanAbsoluteErrorResultName, PrognosisTrainingMeanAbsoluteErrorResultDescription, new DoubleValue()));
118        ((DoubleValue)this[PrognosisTrainingMeanAbsoluteErrorResultName].Value).Value = value;
119      }
120    }
121
122    public double PrognosisTestMeanAbsoluteError {
123      get {
124        if (!ContainsKey(PrognosisTestMeanAbsoluteErrorResultName)) return double.NaN;
125        return ((DoubleValue)this[PrognosisTestMeanAbsoluteErrorResultName].Value).Value;
126      }
127      private set {
128        if (!ContainsKey(PrognosisTestMeanAbsoluteErrorResultName)) Add(new Result(PrognosisTestMeanAbsoluteErrorResultName, PrognosisTestMeanAbsoluteErrorResultDescription, new DoubleValue()));
129        ((DoubleValue)this[PrognosisTestMeanAbsoluteErrorResultName].Value).Value = value;
130      }
131    }
132
133    public double PrognosisTrainingRSquared {
134      get {
135        if (!ContainsKey(PrognosisTrainingSquaredCorrelationResultName)) return double.NaN;
136        return ((DoubleValue)this[PrognosisTrainingSquaredCorrelationResultName].Value).Value;
137      }
138      private set {
139        if (!ContainsKey(PrognosisTrainingSquaredCorrelationResultName)) Add(new Result(PrognosisTrainingSquaredCorrelationResultName, PrognosisTrainingSquaredCorrelationResultDescription, new DoubleValue()));
140        ((DoubleValue)this[PrognosisTrainingSquaredCorrelationResultName].Value).Value = value;
141      }
142    }
143
144    public double PrognosisTestRSquared {
145      get {
146        if (!ContainsKey(PrognosisTestSquaredCorrelationResultName)) return double.NaN;
147        return ((DoubleValue)this[PrognosisTestSquaredCorrelationResultName].Value).Value;
148      }
149      private set {
150        if (!ContainsKey(PrognosisTestSquaredCorrelationResultName)) Add(new Result(PrognosisTestSquaredCorrelationResultName, PrognosisTestSquaredCorrelationResultDescription, new DoubleValue()));
151        ((DoubleValue)this[PrognosisTestSquaredCorrelationResultName].Value).Value = value;
152      }
153    }
154
155    public double PrognosisTrainingRelativeError {
156      get {
157        if (!ContainsKey(PrognosisTrainingRelativeErrorResultName)) return double.NaN;
158        return ((DoubleValue)this[PrognosisTrainingRelativeErrorResultName].Value).Value;
159      }
160      private set {
161        if (!ContainsKey(PrognosisTrainingRelativeErrorResultName)) Add(new Result(PrognosisTrainingRelativeErrorResultName, PrognosisTrainingRelativeErrorResultDescription, new DoubleValue()));
162        ((DoubleValue)this[PrognosisTrainingRelativeErrorResultName].Value).Value = value;
163      }
164    }
165
166    public double PrognosisTestRelativeError {
167      get {
168        if (!ContainsKey(PrognosisTestRelativeErrorResultName)) return double.NaN;
169        return ((DoubleValue)this[PrognosisTestRelativeErrorResultName].Value).Value;
170      }
171      private set {
172        if (!ContainsKey(PrognosisTestRelativeErrorResultName)) Add(new Result(PrognosisTestRelativeErrorResultName, PrognosisTestRelativeErrorResultDescription, new DoubleValue()));
173        ((DoubleValue)this[PrognosisTestRelativeErrorResultName].Value).Value = value;
174      }
175    }
176
177    public double PrognosisTrainingNormalizedMeanSquaredError {
178      get {
179        if (!ContainsKey(PrognosisTrainingNormalizedMeanSquaredErrorResultName)) return double.NaN;
180        return ((DoubleValue)this[PrognosisTrainingNormalizedMeanSquaredErrorResultName].Value).Value;
181      }
182      private set {
183        if (!ContainsKey(PrognosisTrainingNormalizedMeanSquaredErrorResultName)) Add(new Result(PrognosisTrainingNormalizedMeanSquaredErrorResultName, PrognosisTrainingNormalizedMeanSquaredErrorResultDescription, new DoubleValue()));
184        ((DoubleValue)this[PrognosisTrainingNormalizedMeanSquaredErrorResultName].Value).Value = value;
185      }
186    }
187
188    public double PrognosisTestNormalizedMeanSquaredError {
189      get {
190        if (!ContainsKey(PrognosisTestNormalizedMeanSquaredErrorResultName)) return double.NaN;
191        return ((DoubleValue)this[PrognosisTestNormalizedMeanSquaredErrorResultName].Value).Value;
192      }
193      private set {
194        if (!ContainsKey(PrognosisTestNormalizedMeanSquaredErrorResultName)) Add(new Result(PrognosisTestNormalizedMeanSquaredErrorResultName, PrognosisTestNormalizedMeanSquaredErrorResultDescription, new DoubleValue()));
195        ((DoubleValue)this[PrognosisTestNormalizedMeanSquaredErrorResultName].Value).Value = value;
196      }
197    }
198
199    public double PrognosisTrainingMeanError {
200      get {
201        if (!ContainsKey(PrognosisTrainingMeanErrorResultName)) return double.NaN;
202        return ((DoubleValue)this[PrognosisTrainingMeanErrorResultName].Value).Value;
203      }
204      private set {
205        if (!ContainsKey(PrognosisTrainingMeanErrorResultName)) Add(new Result(PrognosisTrainingMeanErrorResultName, PrognosisTrainingMeanErrorResultDescription, new DoubleValue()));
206        ((DoubleValue)this[PrognosisTrainingMeanErrorResultName].Value).Value = value;
207      }
208    }
209
210    public double PrognosisTestMeanError {
211      get {
212        if (!ContainsKey(PrognosisTestMeanErrorResultName)) return double.NaN;
213        return ((DoubleValue)this[PrognosisTestMeanErrorResultName].Value).Value;
214      }
215      private set {
216        if (!ContainsKey(PrognosisTestMeanErrorResultName)) Add(new Result(PrognosisTestMeanErrorResultName, PrognosisTestMeanErrorResultDescription, new DoubleValue()));
217        ((DoubleValue)this[PrognosisTestMeanErrorResultName].Value).Value = value;
218      }
219    }
220
221
222    public double PrognosisTrainingDirectionalSymmetry {
223      get {
224        if (!ContainsKey(PrognosisTrainingDirectionalSymmetryResultName)) return double.NaN;
225        return ((DoubleValue)this[PrognosisTrainingDirectionalSymmetryResultName].Value).Value;
226      }
227      private set {
228        if (!ContainsKey(PrognosisTrainingDirectionalSymmetryResultName)) Add(new Result(PrognosisTrainingDirectionalSymmetryResultName, PrognosisTrainingDirectionalSymmetryResultDescription, new DoubleValue()));
229        ((DoubleValue)this[PrognosisTrainingDirectionalSymmetryResultName].Value).Value = value;
230      }
231    }
232    public double PrognosisTestDirectionalSymmetry {
233      get {
234        if (!ContainsKey(PrognosisTestDirectionalSymmetryResultName)) return double.NaN;
235        return ((DoubleValue)this[PrognosisTestDirectionalSymmetryResultName].Value).Value;
236      }
237      private set {
238        if (!ContainsKey(PrognosisTestDirectionalSymmetryResultName)) Add(new Result(PrognosisTestDirectionalSymmetryResultName, PrognosisTestDirectionalSymmetryResultDescription, new DoubleValue()));
239        ((DoubleValue)this[PrognosisTestDirectionalSymmetryResultName].Value).Value = value;
240      }
241    }
242    public double PrognosisTrainingWeightedDirectionalSymmetry {
243      get {
244        if (!ContainsKey(PrognosisTrainingWeightedDirectionalSymmetryResultName)) return double.NaN;
245        return ((DoubleValue)this[PrognosisTrainingWeightedDirectionalSymmetryResultName].Value).Value;
246      }
247      private set {
248        if (!ContainsKey(PrognosisTrainingWeightedDirectionalSymmetryResultName)) Add(new Result(PrognosisTrainingWeightedDirectionalSymmetryResultName, PrognosisTrainingWeightedDirectionalSymmetryResultDescription, new DoubleValue()));
249        ((DoubleValue)this[PrognosisTrainingWeightedDirectionalSymmetryResultName].Value).Value = value;
250      }
251    }
252    public double PrognosisTestWeightedDirectionalSymmetry {
253      get {
254        if (!ContainsKey(PrognosisTestWeightedDirectionalSymmetryResultName)) return double.NaN;
255        return ((DoubleValue)this[PrognosisTestWeightedDirectionalSymmetryResultName].Value).Value;
256      }
257      private set {
258        if (!ContainsKey(PrognosisTestWeightedDirectionalSymmetryResultName)) Add(new Result(PrognosisTestWeightedDirectionalSymmetryResultName, PrognosisTestWeightedDirectionalSymmetryResultDescription, new DoubleValue()));
259        ((DoubleValue)this[PrognosisTestWeightedDirectionalSymmetryResultName].Value).Value = value;
260      }
261    }
262    public double PrognosisTrainingTheilsUStatisticAR1 {
263      get {
264        if (!ContainsKey(PrognosisTrainingTheilsUStatisticAR1ResultName)) return double.NaN;
265        return ((DoubleValue)this[PrognosisTrainingTheilsUStatisticAR1ResultName].Value).Value;
266      }
267      private set {
268        if (!ContainsKey(PrognosisTrainingTheilsUStatisticAR1ResultName)) Add(new Result(PrognosisTrainingTheilsUStatisticAR1ResultName, PrognosisTrainingTheilsUStatisticAR1ResultDescription, new DoubleValue()));
269        ((DoubleValue)this[PrognosisTrainingTheilsUStatisticAR1ResultName].Value).Value = value;
270      }
271    }
272    public double PrognosisTestTheilsUStatisticAR1 {
273      get {
274        if (!ContainsKey(PrognosisTestTheilsUStatisticAR1ResultName)) return double.NaN;
275        return ((DoubleValue)this[PrognosisTestTheilsUStatisticAR1ResultName].Value).Value;
276      }
277      private set {
278        if (!ContainsKey(PrognosisTestTheilsUStatisticAR1ResultName)) Add(new Result(PrognosisTestTheilsUStatisticAR1ResultName, PrognosisTestTheilsUStatisticAR1ResultDescription, new DoubleValue()));
279        ((DoubleValue)this[PrognosisTestTheilsUStatisticAR1ResultName].Value).Value = value;
280      }
281    }
282    public double PrognosisTrainingTheilsUStatisticMean {
283      get {
284        if (!ContainsKey(PrognosisTrainingTheilsUStatisticMeanResultName)) return double.NaN;
285        return ((DoubleValue)this[PrognosisTrainingTheilsUStatisticMeanResultName].Value).Value;
286      }
287      private set {
288        if (!ContainsKey(PrognosisTrainingTheilsUStatisticMeanResultName)) Add(new Result(PrognosisTrainingTheilsUStatisticMeanResultName, PrognosisTrainingTheilsUStatisticMeanResultDescription, new DoubleValue()));
289        ((DoubleValue)this[PrognosisTrainingTheilsUStatisticMeanResultName].Value).Value = value;
290      }
291    }
292    public double PrognosisTestTheilsUStatisticMean {
293      get {
294        if (!ContainsKey(PrognosisTestTheilsUStatisticMeanResultName)) return double.NaN;
295        return ((DoubleValue)this[PrognosisTestTheilsUStatisticMeanResultName].Value).Value;
296      }
297      private set {
298        if (!ContainsKey(PrognosisTestTheilsUStatisticMeanResultName)) Add(new Result(PrognosisTestTheilsUStatisticMeanResultName, PrognosisTestTheilsUStatisticMeanResultDescription, new DoubleValue()));
299        ((DoubleValue)this[PrognosisTestTheilsUStatisticMeanResultName].Value).Value = value;
300      }
301    }
302    public double PrognosisTrainingTheilsUStatisticMovingAverage {
303      get {
304        if (!ContainsKey(PrognosisTrainingTheilsUStatisticMovingAverageResultName)) return double.NaN;
305        return ((DoubleValue)this[PrognosisTrainingTheilsUStatisticMovingAverageResultName].Value).Value;
306      }
307      private set {
308        if (!ContainsKey(PrognosisTrainingTheilsUStatisticMovingAverageResultName)) Add(new Result(PrognosisTrainingTheilsUStatisticMovingAverageResultName, PrognosisTrainingTheilsUStatisticMovingAverageResultDescription, new DoubleValue()));
309        ((DoubleValue)this[PrognosisTrainingTheilsUStatisticMovingAverageResultName].Value).Value = value;
310      }
311    }
312    public double PrognosisTestTheilsUStatisticMovingAverage {
313      get {
314        if (!ContainsKey(PrognosisTestTheilsUStatisticMovingAverageResultName)) return double.NaN;
315        return ((DoubleValue)this[PrognosisTestTheilsUStatisticMovingAverageResultName].Value).Value;
316      }
317      private set {
318        if (!ContainsKey(PrognosisTestTheilsUStatisticMovingAverageResultName)) Add(new Result(PrognosisTestTheilsUStatisticMovingAverageResultName, PrognosisTestTheilsUStatisticMovingAverageResultDescription, new DoubleValue()));
319        ((DoubleValue)this[PrognosisTestTheilsUStatisticMovingAverageResultName].Value).Value = value;
320      }
321    }
322    #endregion
323
324
325    private int trainingHorizon;
326    [Storable]
327    public int TrainingHorizon {
328      get { return trainingHorizon; }
329      set {
330        if (trainingHorizon != value) {
331          trainingHorizon = value;
332          OnTrainingHorizonChanged();
333        }
334      }
335    }
336
337    private int testHorizon;
338    [Storable]
339    public int TestHorizon {
340      get { return testHorizon; }
341      set {
342        if (testHorizon != value) {
343          testHorizon = value;
344          OnTestHorizonChanged();
345        }
346      }
347    }
348
349    private ITimeSeriesPrognosisSolution solution;
350    [Storable]
351    public ITimeSeriesPrognosisSolution Solution {
352      get { return solution; }
353    }
354
355    [StorableConstructor]
356    public TimeSeriesPrognosisResults(bool deserializing) : base(deserializing) { }
357    protected TimeSeriesPrognosisResults(TimeSeriesPrognosisResults original, Cloner cloner) : base(original, cloner) { }
358    public override IDeepCloneable Clone(Cloner cloner) {
359      return new TimeSeriesPrognosisResults(this, cloner);
360    }
361
362    public TimeSeriesPrognosisResults(int trainingHorizon, int testHorizon, ITimeSeriesPrognosisSolution solution)
363      : base() {
364      this.trainingHorizon = trainingHorizon;
365      this.testHorizon = testHorizon;
366      this.solution = solution;
367      CalculateTrainingPrognosisResults();
368      CalculateTestPrognosisResults();
369    }
370
371    #region events
372    public event EventHandler TrainingHorizonChanged;
373    protected virtual void OnTrainingHorizonChanged() {
374      CalculateTrainingPrognosisResults();
375      var handler = TrainingHorizonChanged;
376      if (handler != null) handler(this, EventArgs.Empty);
377    }
378
379    public event EventHandler TestHorizonChanged;
380    protected virtual void OnTestHorizonChanged() {
381      CalculateTestPrognosisResults();
382      var handler = TestHorizonChanged;
383      if (handler != null) handler(this, EventArgs.Empty);
384    }
385    #endregion
386
387    private void CalculateTrainingPrognosisResults() {
388      OnlineCalculatorError errorState;
389      var problemData = Solution.ProblemData;
390      var model = Solution.Model;
391      //mean model
392      double trainingMean = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices).Average();
393      var meanModel = new ConstantTimeSeriesPrognosisModel(trainingMean);
394
395      //AR1 model
396      double alpha, beta;
397      IEnumerable<double> trainingStartValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices.Select(r => r - 1).Where(r => r > 0)).ToList();
398      OnlineLinearScalingParameterCalculator.Calculate(problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices.Where(x => x > 0)), trainingStartValues, out alpha, out beta, out errorState);
399      var AR1model = new TimeSeriesPrognosisAutoRegressiveModel(problemData.TargetVariable, new double[] { beta }, alpha);
400
401      //MA model
402      const int movingAverageWindowSize = 10;
403      var MovingAverageModel = new TimeSeriesPrognosisMovingAverageModel(movingAverageWindowSize, problemData.TargetVariable);
404
405      var trainingHorizions = problemData.TrainingIndices.Select(r => Math.Min(trainingHorizon, problemData.TrainingPartition.End - r)).ToList();
406      IEnumerable<IEnumerable<double>> trainingTargetValues = problemData.TrainingIndices.Zip(trainingHorizions, Enumerable.Range).Select(r => problemData.Dataset.GetDoubleValues(problemData.TargetVariable, r)).ToList();
407      IEnumerable<IEnumerable<double>> trainingEstimatedValues = model.GetPrognosedValues(problemData.Dataset, problemData.TrainingIndices, trainingHorizions).ToList();
408      IEnumerable<IEnumerable<double>> trainingMeanModelPredictions = meanModel.GetPrognosedValues(problemData.Dataset, problemData.TrainingIndices, trainingHorizions).ToList();
409      IEnumerable<IEnumerable<double>> trainingAR1ModelPredictions = AR1model.GetPrognosedValues(problemData.Dataset, problemData.TrainingIndices, trainingHorizions).ToList();
410      IEnumerable<IEnumerable<double>> trainingMovingAverageModelPredictions = MovingAverageModel.GetPrognosedValues(problemData.Dataset, problemData.TrainingIndices, trainingHorizions).ToList();
411
412      IEnumerable<double> originalTrainingValues = trainingTargetValues.SelectMany(x => x).ToList();
413      IEnumerable<double> estimatedTrainingValues = trainingEstimatedValues.SelectMany(x => x).ToList();
414
415      double trainingMSE = OnlineMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
416      PrognosisTrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMSE : double.NaN;
417      double trainingMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
418      PrognosisTrainingMeanAbsoluteError = errorState == OnlineCalculatorError.None ? trainingMAE : double.NaN;
419      double trainingR2 = OnlinePearsonsRSquaredCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
420      PrognosisTrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR2 : double.NaN;
421      double trainingRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
422      PrognosisTrainingRelativeError = errorState == OnlineCalculatorError.None ? trainingRelError : double.NaN;
423      double trainingNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
424      PrognosisTrainingNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingNMSE : double.NaN;
425      double trainingME = OnlineMeanErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
426      PrognosisTrainingMeanError = errorState == OnlineCalculatorError.None ? trainingME : double.NaN;
427
428      PrognosisTrainingDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(trainingStartValues, trainingTargetValues, trainingEstimatedValues, out errorState);
429      PrognosisTrainingDirectionalSymmetry = errorState == OnlineCalculatorError.None ? PrognosisTrainingDirectionalSymmetry : 0.0;
430      PrognosisTrainingWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(trainingStartValues, trainingTargetValues, trainingEstimatedValues, out errorState);
431      PrognosisTrainingWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? PrognosisTrainingWeightedDirectionalSymmetry : 0.0;
432      PrognosisTrainingTheilsUStatisticAR1 = OnlineTheilsUStatisticCalculator.Calculate(trainingStartValues, trainingTargetValues, trainingAR1ModelPredictions, trainingEstimatedValues, out errorState);
433      PrognosisTrainingTheilsUStatisticAR1 = errorState == OnlineCalculatorError.None ? PrognosisTrainingTheilsUStatisticAR1 : double.PositiveInfinity;
434      PrognosisTrainingTheilsUStatisticMean = OnlineTheilsUStatisticCalculator.Calculate(trainingStartValues, trainingTargetValues, trainingMeanModelPredictions, trainingEstimatedValues, out errorState);
435      PrognosisTrainingTheilsUStatisticMean = errorState == OnlineCalculatorError.None ? PrognosisTrainingTheilsUStatisticMean : double.PositiveInfinity;
436      PrognosisTrainingTheilsUStatisticMovingAverage = OnlineTheilsUStatisticCalculator.Calculate(trainingStartValues, trainingTargetValues, trainingMovingAverageModelPredictions, trainingEstimatedValues, out errorState);
437      PrognosisTrainingTheilsUStatisticMovingAverage = errorState == OnlineCalculatorError.None ? PrognosisTrainingTheilsUStatisticMovingAverage : double.PositiveInfinity;
438    }
439
440    private void CalculateTestPrognosisResults() {
441      OnlineCalculatorError errorState;
442      var problemData = Solution.ProblemData;
443      var model = Solution.Model;
444      //mean model
445      double trainingMean = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices).Average();
446      var meanModel = new ConstantTimeSeriesPrognosisModel(trainingMean);
447
448      //AR1 model
449      double alpha, beta;
450      IEnumerable<double> trainingStartValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices.Select(r => r - 1).Where(r => r > 0)).ToList();
451      OnlineLinearScalingParameterCalculator.Calculate(problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices.Where(x => x > 0)), trainingStartValues, out alpha, out beta, out errorState);
452      var AR1model = new TimeSeriesPrognosisAutoRegressiveModel(problemData.TargetVariable, new double[] { beta }, alpha);
453
454      //MA model
455      const int movingAverageWindowSize = 10;
456      var MovingAverageModel = new TimeSeriesPrognosisMovingAverageModel(movingAverageWindowSize, problemData.TargetVariable);
457
458      var testHorizions = problemData.TestIndices.Select(r => Math.Min(testHorizon, problemData.TestPartition.End - r)).ToList();
459      IEnumerable<IEnumerable<double>> testTargetValues = problemData.TestIndices.Zip(testHorizions, Enumerable.Range).Select(r => problemData.Dataset.GetDoubleValues(problemData.TargetVariable, r)).ToList();
460      IEnumerable<IEnumerable<double>> testEstimatedValues = model.GetPrognosedValues(problemData.Dataset, problemData.TestIndices, testHorizions).ToList();
461      IEnumerable<double> testStartValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TestIndices.Select(r => r - 1).Where(r => r > 0)).ToList();
462      IEnumerable<IEnumerable<double>> testMeanModelPredictions = meanModel.GetPrognosedValues(problemData.Dataset, problemData.TestIndices, testHorizions).ToList();
463      IEnumerable<IEnumerable<double>> testAR1ModelPredictions = AR1model.GetPrognosedValues(problemData.Dataset, problemData.TestIndices, testHorizions).ToList();
464      IEnumerable<IEnumerable<double>> testMovingAverageModelPredictions = MovingAverageModel.GetPrognosedValues(problemData.Dataset, problemData.TestIndices, testHorizions).ToList();
465
466      IEnumerable<double> originalTestValues = testTargetValues.SelectMany(x => x).ToList();
467      IEnumerable<double> estimatedTestValues = testEstimatedValues.SelectMany(x => x).ToList();
468
469      double testMSE = OnlineMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
470      PrognosisTestMeanSquaredError = errorState == OnlineCalculatorError.None ? testMSE : double.NaN;
471      double testMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
472      PrognosisTestMeanAbsoluteError = errorState == OnlineCalculatorError.None ? testMAE : double.NaN;
473      double testR2 = OnlinePearsonsRSquaredCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
474      PrognosisTestRSquared = errorState == OnlineCalculatorError.None ? testR2 : double.NaN;
475      double testRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
476      PrognosisTestRelativeError = errorState == OnlineCalculatorError.None ? testRelError : double.NaN;
477      double testNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
478      PrognosisTestNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? testNMSE : double.NaN;
479      double testME = OnlineMeanErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
480      PrognosisTestMeanError = errorState == OnlineCalculatorError.None ? testME : double.NaN;
481
482      PrognosisTestDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(testStartValues, testTargetValues, testEstimatedValues, out errorState);
483      PrognosisTestDirectionalSymmetry = errorState == OnlineCalculatorError.None ? PrognosisTestDirectionalSymmetry : 0.0;
484      PrognosisTestWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(testStartValues, testTargetValues, testEstimatedValues, out errorState);
485      PrognosisTestWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? PrognosisTestWeightedDirectionalSymmetry : 0.0;
486      PrognosisTestTheilsUStatisticAR1 = OnlineTheilsUStatisticCalculator.Calculate(testStartValues, testTargetValues, testAR1ModelPredictions, testEstimatedValues, out errorState);
487      PrognosisTestTheilsUStatisticAR1 = errorState == OnlineCalculatorError.None ? PrognosisTestTheilsUStatisticAR1 : double.PositiveInfinity;
488      PrognosisTestTheilsUStatisticMean = OnlineTheilsUStatisticCalculator.Calculate(testStartValues, testTargetValues, testMeanModelPredictions, testEstimatedValues, out errorState);
489      PrognosisTestTheilsUStatisticMean = errorState == OnlineCalculatorError.None ? PrognosisTestTheilsUStatisticMean : double.PositiveInfinity;
490      PrognosisTestTheilsUStatisticMovingAverage = OnlineTheilsUStatisticCalculator.Calculate(testStartValues, testTargetValues, testMovingAverageModelPredictions, testEstimatedValues, out errorState);
491      PrognosisTestTheilsUStatisticMovingAverage = errorState == OnlineCalculatorError.None ? PrognosisTestTheilsUStatisticMovingAverage : double.PositiveInfinity;
492    }
493  }
494}
Note: See TracBrowser for help on using the repository browser.