Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7183 was 7183, checked in by gkronber, 12 years ago

#1081 implemented remaining metrics for time series prognosis solutions, added estimation limits fixed training and validation best solution analyzers, implemented overfitting analyzer.

File size: 31.8 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.Concurrent;
24using System.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.DataAnalysis {
32  [StorableClass]
33  public abstract class TimeSeriesPrognosisSolutionBase : DataAnalysisSolution, ITimeSeriesPrognosisSolution {
34    private const string TrainingMeanSquaredErrorResultName = "Mean squared error (training)";
35    private const string TestMeanSquaredErrorResultName = "Mean squared error (test)";
36    private const string TrainingMeanAbsoluteErrorResultName = "Mean absolute error (training)";
37    private const string TestMeanAbsoluteErrorResultName = "Mean absolute error (test)";
38    private const string TrainingSquaredCorrelationResultName = "Pearson's R² (training)";
39    private const string TestSquaredCorrelationResultName = "Pearson's R² (test)";
40    private const string TrainingRelativeErrorResultName = "Average relative error (training)";
41    private const string TestRelativeErrorResultName = "Average relative error (test)";
42    private const string TrainingNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (training)";
43    private const string TestNormalizedMeanSquaredErrorResultName = "Normalized mean squared error (test)";
44    private const string TrainingDirectionalSymmetryResultName = "Average directional symmetry (training)";
45    private const string TestDirectionalSymmetryResultName = "Average directional symmetry (test)";
46    private const string TrainingWeightedDirectionalSymmetryResultName = "Average weighted directional symmetry (training)";
47    private const string TestWeightedDirectionalSymmetryResultName = "Average weighted directional symmetry (test)";
48    private const string TrainingTheilsUStatisticLastResultName = "Average Theil's U (last) (training)";
49    private const string TestTheilsUStatisticLastResultName = "Average Theil's U (last) (test)";
50    private const string TrainingTheilsUStatisticMeanResultName = "Average Theil's U (mean) (training)";
51    private const string TestTheilsUStatisticMeanResultName = "Average Theil's U (mean) (test)";
52    private const string TrainingTheilsUStatisticMAResultName = "Average Theil's U (moving average) (training)";
53    private const string TestTheilsUStatisticMAResultName = "Average Theil's U (moving average) (test)";
54
55    public new ITimeSeriesPrognosisModel Model {
56      get { return (ITimeSeriesPrognosisModel)base.Model; }
57      protected set { base.Model = value; }
58    }
59
60    public new ITimeSeriesPrognosisProblemData ProblemData {
61      get { return (ITimeSeriesPrognosisProblemData)base.ProblemData; }
62      set { base.ProblemData = value; }
63    }
64
65    [Storable]
66    private int horizon;
67    public int Horizon {
68      get { return horizon; }
69      set {
70        if (horizon != value) {
71          horizon = value;
72          RecalculateResults();
73        }
74      }
75    }
76
77    public abstract IEnumerable<IEnumerable<double>> PrognosedTrainingValues { get; }
78    public abstract IEnumerable<IEnumerable<double>> PrognosedTestValues { get; }
79    public abstract IEnumerable<IEnumerable<IEnumerable<double>>> GetPrognosedValues(IEnumerable<int> rows, int horizon);
80
81    #region Results
82    public double[] TrainingMeanSquaredError {
83      get { return ((DoubleArray)this[TrainingMeanSquaredErrorResultName].Value).ToArray(); }
84      private set { this[TrainingMeanSquaredErrorResultName].Value = new DoubleArray(value); }
85    }
86    public double[] TestMeanSquaredError {
87      get { return ((DoubleArray)this[TestMeanSquaredErrorResultName].Value).ToArray(); }
88      private set { this[TestMeanSquaredErrorResultName].Value = new DoubleArray(value); }
89    }
90    public double[] TrainingMeanAbsoluteError {
91      get { return ((DoubleArray)this[TrainingMeanAbsoluteErrorResultName].Value).ToArray(); }
92      private set { this[TrainingMeanAbsoluteErrorResultName].Value = new DoubleArray(value); }
93    }
94    public double[] TestMeanAbsoluteError {
95      get { return ((DoubleArray)this[TestMeanAbsoluteErrorResultName].Value).ToArray(); }
96      private set { this[TestMeanAbsoluteErrorResultName].Value = new DoubleArray(value); }
97    }
98    public double[] TrainingRSquared {
99      get { return ((DoubleArray)this[TrainingSquaredCorrelationResultName].Value).ToArray(); }
100      private set { this[TrainingSquaredCorrelationResultName].Value = new DoubleArray(value); }
101    }
102    public double[] TestRSquared {
103      get { return ((DoubleArray)this[TestSquaredCorrelationResultName].Value).ToArray(); }
104      private set { this[TestSquaredCorrelationResultName].Value = new DoubleArray(value); }
105    }
106    public double[] TrainingRelativeError {
107      get { return ((DoubleArray)this[TrainingRelativeErrorResultName].Value).ToArray(); }
108      private set { this[TrainingRelativeErrorResultName].Value = new DoubleArray(value); }
109    }
110    public double[] TestRelativeError {
111      get { return ((DoubleArray)this[TestRelativeErrorResultName].Value).ToArray(); }
112      private set { this[TestRelativeErrorResultName].Value = new DoubleArray(value); }
113    }
114    public double[] TrainingNormalizedMeanSquaredError {
115      get { return ((DoubleArray)this[TrainingNormalizedMeanSquaredErrorResultName].Value).ToArray(); }
116      private set { this[TrainingNormalizedMeanSquaredErrorResultName].Value = new DoubleArray(value); }
117    }
118    public double[] TestNormalizedMeanSquaredError {
119      get { return ((DoubleArray)this[TestNormalizedMeanSquaredErrorResultName].Value).ToArray(); }
120      private set { this[TestNormalizedMeanSquaredErrorResultName].Value = new DoubleArray(value); }
121    }
122    public double[] TrainingDirectionalSymmetry {
123      get { return ((DoubleArray)this[TrainingDirectionalSymmetryResultName].Value).ToArray(); }
124      private set { this[TrainingDirectionalSymmetryResultName].Value = new DoubleArray(value); }
125    }
126    public double[] TestDirectionalSymmetry {
127      get { return ((DoubleArray)this[TestDirectionalSymmetryResultName].Value).ToArray(); }
128      private set { this[TestDirectionalSymmetryResultName].Value = new DoubleArray(value); }
129    }
130    public double[] TrainingWeightedDirectionalSymmetry {
131      get { return ((DoubleArray)this[TrainingWeightedDirectionalSymmetryResultName].Value).ToArray(); }
132      private set { this[TrainingWeightedDirectionalSymmetryResultName].Value = new DoubleArray(value); }
133    }
134    public double[] TestWeightedDirectionalSymmetry {
135      get { return ((DoubleArray)this[TestWeightedDirectionalSymmetryResultName].Value).ToArray(); }
136      private set { this[TestWeightedDirectionalSymmetryResultName].Value = new DoubleArray(value); }
137    }
138    public double[] TrainingTheilsUStatisticLast {
139      get { return ((DoubleArray)this[TrainingTheilsUStatisticLastResultName].Value).ToArray(); }
140      private set { this[TrainingTheilsUStatisticLastResultName].Value = new DoubleArray(value); }
141    }
142    public double[] TestTheilsUStatisticLast {
143      get { return ((DoubleArray)this[TestTheilsUStatisticLastResultName].Value).ToArray(); }
144      private set { this[TestTheilsUStatisticLastResultName].Value = new DoubleArray(value); }
145    }
146    public double[] TrainingTheilsUStatisticMean {
147      get { return ((DoubleArray)this[TrainingTheilsUStatisticMeanResultName].Value).ToArray(); }
148      private set { this[TrainingTheilsUStatisticMeanResultName].Value = new DoubleArray(value); }
149    }
150    public double[] TestTheilsUStatisticMean {
151      get { return ((DoubleArray)this[TestTheilsUStatisticMeanResultName].Value).ToArray(); }
152      private set { this[TestTheilsUStatisticMeanResultName].Value = new DoubleArray(value); }
153    }
154    public double[] TrainingTheilsUStatisticMovingAverage {
155      get { return ((DoubleArray)this[TrainingTheilsUStatisticMAResultName].Value).ToArray(); }
156      private set { this[TrainingTheilsUStatisticMAResultName].Value = new DoubleArray(value); }
157    }
158    public double[] TestTheilsUStatisticMovingAverage {
159      get { return ((DoubleArray)this[TestTheilsUStatisticMAResultName].Value).ToArray(); }
160      private set { this[TestTheilsUStatisticMAResultName].Value = new DoubleArray(value); }
161    }
162    #endregion
163
164    [StorableConstructor]
165    protected TimeSeriesPrognosisSolutionBase(bool deserializing) : base(deserializing) { }
166    protected TimeSeriesPrognosisSolutionBase(TimeSeriesPrognosisSolutionBase original, Cloner cloner)
167      : base(original, cloner) {
168      this.horizon = original.horizon;
169    }
170    protected TimeSeriesPrognosisSolutionBase(ITimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData)
171      : base(model, problemData) {
172      Add(new Result(TrainingMeanSquaredErrorResultName, "Mean of squared errors of the model on the training partition", new DoubleArray()));
173      Add(new Result(TestMeanSquaredErrorResultName, "Mean of squared errors of the model on the test partition", new DoubleArray()));
174      Add(new Result(TrainingMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the training partition", new DoubleArray()));
175      Add(new Result(TestMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the test partition", new DoubleArray()));
176      Add(new Result(TrainingSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the training partition", new DoubleArray()));
177      Add(new Result(TestSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the test partition", new DoubleArray()));
178      Add(new Result(TrainingRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the training partition", new DoubleArray()));
179      Add(new Result(TestRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the test partition", new DoubleArray()));
180      Add(new Result(TrainingNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the training partition", new DoubleArray()));
181      Add(new Result(TestNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the test partition", new DoubleArray()));
182      Add(new Result(TrainingDirectionalSymmetryResultName, "The average directional symmetry of the forecasts of the model on the training partition", new DoubleArray()));
183      Add(new Result(TestDirectionalSymmetryResultName, "The average directional symmetry of the forecasts of the model on the test partition", new DoubleArray()));
184      Add(new Result(TrainingWeightedDirectionalSymmetryResultName, "The average weighted directional symmetry of the forecasts of the model on the training partition", new DoubleArray()));
185      Add(new Result(TestWeightedDirectionalSymmetryResultName, "The average weighted directional symmetry of the forecasts of the model on the test partition", new DoubleArray()));
186      Add(new Result(TrainingTheilsUStatisticLastResultName, "The average Theil's U statistic (reference: previous value) of the forecasts of the model on the training partition", new DoubleArray()));
187      Add(new Result(TestTheilsUStatisticLastResultName, "The average Theil's U statistic (reference: previous value) of the forecasts of the model on the test partition", new DoubleArray()));
188      Add(new Result(TrainingTheilsUStatisticMeanResultName, "The average Theil's U statistic (reference: mean value) of the forecasts of the model on the training partition", new DoubleArray()));
189      Add(new Result(TestTheilsUStatisticMeanResultName, "The average Theil's U statistic (reference: mean value) of the forecasts of the model on the test partition", new DoubleArray()));
190      Add(new Result(TrainingTheilsUStatisticMAResultName, "The average Theil's U statistic (reference: moving average) of the forecasts of the model on the training partition", new DoubleArray()));
191      Add(new Result(TestTheilsUStatisticMAResultName, "The average Theil's U statistic (reference: moving average) of the forecasts of the model on the test partition", new DoubleArray()));
192      horizon = 1;
193    }
194
195    [StorableHook(HookType.AfterDeserialization)]
196    private void AfterDeserialization() {
197      if (horizon == 0) horizon = 1;
198      bool anyNewResult = false;
199      if (!ContainsKey(TrainingTheilsUStatisticLastResultName)) {
200        Add(new Result(TrainingTheilsUStatisticLastResultName, "The average Theil's U statistic (reference: previous value) of the forecasts of the model on the training partition", new DoubleArray()));
201        anyNewResult = true;
202      }
203      if (!ContainsKey(TestTheilsUStatisticLastResultName)) {
204        Add(new Result(TestTheilsUStatisticLastResultName, "The average Theil's U statistic (reference: previous value) of the forecasts of the model on the test partition", new DoubleArray()));
205        anyNewResult = true;
206      }
207      if (!ContainsKey(TrainingTheilsUStatisticMeanResultName)) {
208        Add(new Result(TrainingTheilsUStatisticMeanResultName, "The average Theil's U statistic (reference: mean value) of the forecasts of the model on the training partition", new DoubleArray()));
209        anyNewResult = true;
210      }
211      if (!ContainsKey(TestTheilsUStatisticMeanResultName)) {
212        Add(new Result(TestTheilsUStatisticMeanResultName, "The average Theil's U statistic (reference: mean value) of the forecasts of the model on the test partition", new DoubleArray()));
213        anyNewResult = true;
214      }
215      if (!ContainsKey(TrainingTheilsUStatisticMAResultName)) {
216        Add(new Result(TrainingTheilsUStatisticMAResultName, "The average Theil's U statistic (reference: moving average) of the forecasts of the model on the training partition", new DoubleArray()));
217        anyNewResult = true;
218      }
219      if (!ContainsKey(TestTheilsUStatisticMAResultName)) {
220        Add(new Result(TestTheilsUStatisticMAResultName, "The average Theil's U statistic (reference: moving average) of the forecasts of the model on the test partition", new DoubleArray()));
221        anyNewResult = true;
222      }
223      if (anyNewResult)
224        RecalculateResults();
225    }
226
227    protected void CalculateResults() {
228      OnlineCalculatorError errorState;
229      string[] targetVariables = ProblemData.TargetVariables.ToArray();
230      /*
231      double[] estimatedTrainingValues = PrognosedTrainingValues.ToArray(); // cache values
232      double[] originalTrainingValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes).ToArray();
233      double[] estimatedTestValues = PrognosedTestValues.ToArray(); // cache values
234      double[] originalTestValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndizes).ToArray();
235
236      double trainingMse = OnlineMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
237      TrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMse : double.NaN;
238      double testMse = OnlineMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
239      TestMeanSquaredError = errorState == OnlineCalculatorError.None ? testMse : double.NaN;
240
241      double trainingMae = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
242      TrainingMeanAbsoluteError = errorState == OnlineCalculatorError.None ? trainingMae : double.NaN;
243      double testMae = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
244      TestMeanAbsoluteError = errorState == OnlineCalculatorError.None ? testMae : double.NaN;
245
246      double trainingR2 = OnlinePearsonsRSquaredCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
247      TrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR2 : double.NaN;
248      double testR2 = OnlinePearsonsRSquaredCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
249      TestRSquared = errorState == OnlineCalculatorError.None ? testR2 : double.NaN;
250
251      double trainingRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
252      TrainingRelativeError = errorState == OnlineCalculatorError.None ? trainingRelError : double.NaN;
253      double testRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
254      TestRelativeError = errorState == OnlineCalculatorError.None ? testRelError : double.NaN;
255
256      double trainingNmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
257      TrainingNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingNmse : double.NaN;
258      double testNmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
259      TestNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? testNmse : double.NaN;
260              */
261
262      //double[] trainingDs = new double[targetVariables.Length];
263      //double[] testDs = new double[targetVariables.Length];
264
265      //double[] trainingWds = new double[targetVariables.Length];
266      //double[] testWds = new double[targetVariables.Length];
267
268      //double[] trainingTheilsU = new double[targetVariables.Length];
269      //double[] testTheilsU = new double[targetVariables.Length];
270
271      var trainingMseCalculators = new OnlineMeanSquaredErrorCalculator[targetVariables.Length];
272      var testMseCalculators = new OnlineMeanSquaredErrorCalculator[targetVariables.Length];
273      var trainingMaeCalculators = new OnlineMeanAbsoluteErrorCalculator[targetVariables.Length];
274      var testMaeCalculators = new OnlineMeanAbsoluteErrorCalculator[targetVariables.Length];
275      var trainingRSquaredCalculators = new OnlinePearsonsRSquaredCalculator[targetVariables.Length];
276      var testRSquaredCalculators = new OnlinePearsonsRSquaredCalculator[targetVariables.Length];
277      var trainingRelErrorCalculators = new OnlineMeanAbsolutePercentageErrorCalculator[targetVariables.Length];
278      var testRelErrorCalculators = new OnlineMeanAbsolutePercentageErrorCalculator[targetVariables.Length];
279      var trainingNmseCalculators = new OnlineNormalizedMeanSquaredErrorCalculator[targetVariables.Length];
280      var testNmseCalculators = new OnlineNormalizedMeanSquaredErrorCalculator[targetVariables.Length];
281
282      var trainingDsCalculators = new OnlineDirectionalSymmetryCalculator[targetVariables.Length];
283      var testDsCalculators = new OnlineDirectionalSymmetryCalculator[targetVariables.Length];
284      var trainingWdsCalculators = new OnlineWeightedDirectionalSymmetryCalculator[targetVariables.Length];
285      var testWdsCalculators = new OnlineWeightedDirectionalSymmetryCalculator[targetVariables.Length];
286      var trainingTheilsULastCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
287      var testTheilsULastCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
288      var trainingTheilsUMeanCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
289      var testTheilsUMeanCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
290      var trainingTheilsUMovingAverageCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
291      var testTheilsUMovingAverageCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length];
292      for (int i = 0; i < targetVariables.Length; i++) {
293        trainingMseCalculators[i] = new OnlineMeanSquaredErrorCalculator();
294        testMseCalculators[i] = new OnlineMeanSquaredErrorCalculator();
295        trainingMaeCalculators[i] = new OnlineMeanAbsoluteErrorCalculator();
296        testMaeCalculators[i] = new OnlineMeanAbsoluteErrorCalculator();
297        trainingRSquaredCalculators[i] = new OnlinePearsonsRSquaredCalculator();
298        testRSquaredCalculators[i] = new OnlinePearsonsRSquaredCalculator();
299        trainingRelErrorCalculators[i] = new OnlineMeanAbsolutePercentageErrorCalculator();
300        testRelErrorCalculators[i] = new OnlineMeanAbsolutePercentageErrorCalculator();
301        trainingNmseCalculators[i] = new OnlineNormalizedMeanSquaredErrorCalculator();
302        testNmseCalculators[i] = new OnlineNormalizedMeanSquaredErrorCalculator();
303
304        trainingDsCalculators[i] = new OnlineDirectionalSymmetryCalculator();
305        testDsCalculators[i] = new OnlineDirectionalSymmetryCalculator();
306        trainingWdsCalculators[i] = new OnlineWeightedDirectionalSymmetryCalculator();
307        testWdsCalculators[i] = new OnlineWeightedDirectionalSymmetryCalculator();
308        trainingTheilsULastCalculators[i] = new OnlineTheilsUStatisticCalculator();
309        testTheilsULastCalculators[i] = new OnlineTheilsUStatisticCalculator();
310        trainingTheilsUMeanCalculators[i] = new OnlineTheilsUStatisticCalculator();
311        testTheilsUMeanCalculators[i] = new OnlineTheilsUStatisticCalculator();
312        trainingTheilsUMovingAverageCalculators[i] = new OnlineTheilsUStatisticCalculator();
313        testTheilsUMovingAverageCalculators[i] = new OnlineTheilsUStatisticCalculator();
314      }
315
316      var allPrognosedTrainingValues = GetPrognosedValues(ProblemData.TrainingIndizes, horizon).GetEnumerator();
317      foreach (var row in ProblemData.TrainingIndizes) {
318        if (row + horizon < ProblemData.Dataset.Rows) {
319          allPrognosedTrainingValues.MoveNext();
320          var prognosedTrainingValues = allPrognosedTrainingValues.Current.SelectMany(x => x).ToArray();
321          for (int t = 0; t < targetVariables.Length; t++) {
322            var actualContinuation = ProblemData.Dataset.GetDoubleValues(targetVariables[t],
323                                                                         Enumerable.Range(row, horizon));
324            int maWindow = 10 * horizon;
325            var movingAverageContinuation = from h in Enumerable.Range(0, horizon)
326                                            select (from r in Enumerable.Range(row + h - maWindow, maWindow - h)
327                                                    where r > 0
328                                                    select ProblemData.Dataset.GetDoubleValue(targetVariables[t], r)
329                                                   ).Average();
330            var mean = ProblemData.Dataset.GetDoubleValues(targetVariables[t], ProblemData.TrainingIndizes).Median();
331            double startValue = ProblemData.Dataset.GetDoubleValue(targetVariables[t], row - 1);
332            var prognosedContinuation = prognosedTrainingValues.Skip(t).TakeEvery(targetVariables.Length);
333            trainingDsCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
334            trainingWdsCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
335            trainingTheilsULastCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
336            trainingTheilsUMeanCalculators[t].Add(startValue, actualContinuation.Select(x => mean), actualContinuation, prognosedContinuation);
337            trainingTheilsUMovingAverageCalculators[t].Add(startValue, movingAverageContinuation, actualContinuation, prognosedContinuation);
338
339            var actualContinuationEnumerator = actualContinuation.GetEnumerator();
340            var prognosedContinuationEnumerator = prognosedContinuation.GetEnumerator();
341            while (actualContinuationEnumerator.MoveNext() & prognosedContinuationEnumerator.MoveNext()) {
342              trainingMseCalculators[t].Add(actualContinuationEnumerator.Current, prognosedContinuationEnumerator.Current);
343              trainingMaeCalculators[t].Add(actualContinuationEnumerator.Current, prognosedContinuationEnumerator.Current);
344              trainingRelErrorCalculators[t].Add(actualContinuationEnumerator.Current, prognosedContinuationEnumerator.Current);
345              trainingRSquaredCalculators[t].Add(actualContinuationEnumerator.Current, prognosedContinuationEnumerator.Current);
346              trainingNmseCalculators[t].Add(actualContinuationEnumerator.Current, prognosedContinuationEnumerator.Current);
347            }
348            if (actualContinuationEnumerator.MoveNext() | prognosedContinuationEnumerator.MoveNext())
349              throw new ArgumentException(
350                "Different number of elements in Actual continuation and prognosed continuation.");
351          }
352        }
353      }
354      var allPrognosedTestValues = GetPrognosedValues(ProblemData.TestIndizes, horizon).GetEnumerator();
355      foreach (var row in ProblemData.TestIndizes) {
356        if (row + horizon < ProblemData.Dataset.Rows) {
357          allPrognosedTestValues.MoveNext();
358          var prognosedTestValues = allPrognosedTestValues.Current.SelectMany(x => x).ToArray();
359          for (int t = 0; t < targetVariables.Length; t++) {
360            var actualContinuation = ProblemData.Dataset.GetDoubleValues(targetVariables[t],
361                                                                         Enumerable.Range(row, horizon));
362            int maWindow = 10 * horizon;
363            var movingAverageContinuation = from h in Enumerable.Range(0, horizon)
364                                            select (from r in Enumerable.Range(row + h - maWindow, maWindow - h)
365                                                    where r > 0
366                                                    select ProblemData.Dataset.GetDoubleValue(targetVariables[t], r)
367                                                   ).Average();
368            // mean must be calculated on the training set!
369            var mean = ProblemData.Dataset.GetDoubleValues(targetVariables[t], ProblemData.TrainingIndizes).Median();
370            double startValue = ProblemData.Dataset.GetDoubleValue(targetVariables[t], row - 1);
371            var prognosedContinuation = prognosedTestValues.Skip(t).TakeEvery(targetVariables.Length);
372            testDsCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
373            testWdsCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
374            testTheilsULastCalculators[t].Add(startValue, actualContinuation, prognosedContinuation);
375            testTheilsUMeanCalculators[t].Add(startValue, actualContinuation.Select(x => mean), actualContinuation, prognosedContinuation);
376            testTheilsUMovingAverageCalculators[t].Add(startValue, movingAverageContinuation, actualContinuation, prognosedContinuation);
377
378            var actualContinuationEnumerator = actualContinuation.GetEnumerator();
379            var prognosedContinuationEnumerator = prognosedContinuation.GetEnumerator();
380            while (actualContinuationEnumerator.MoveNext() & prognosedContinuationEnumerator.MoveNext()) {
381              testMseCalculators[t].Add(actualContinuationEnumerator.Current, prognosedContinuationEnumerator.Current);
382              testMaeCalculators[t].Add(actualContinuationEnumerator.Current, prognosedContinuationEnumerator.Current);
383              testRelErrorCalculators[t].Add(actualContinuationEnumerator.Current, prognosedContinuationEnumerator.Current);
384              testRSquaredCalculators[t].Add(actualContinuationEnumerator.Current, prognosedContinuationEnumerator.Current);
385              testNmseCalculators[t].Add(actualContinuationEnumerator.Current, prognosedContinuationEnumerator.Current);
386            }
387            if (actualContinuationEnumerator.MoveNext() | prognosedContinuationEnumerator.MoveNext())
388              throw new ArgumentException(
389                "Different number of elements in Actual continuation and prognosed continuation.");
390          }
391        }
392      }
393
394
395      TrainingMeanSquaredError = trainingMseCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
396        .ToArray();
397      TestMeanSquaredError = testMseCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
398        .ToArray();
399      TrainingMeanAbsoluteError = trainingMaeCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
400        .ToArray();
401      TestMeanAbsoluteError = testMaeCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
402        .ToArray();
403      TrainingRelativeError = trainingRelErrorCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
404        .ToArray();
405      TestRelativeError = testRelErrorCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
406        .ToArray();
407      TrainingRSquared = trainingRSquaredCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : 0.0)
408        .ToArray();
409      TestRSquared = testRSquaredCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : 0.0)
410        .ToArray();
411      TrainingNormalizedMeanSquaredError = trainingNmseCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
412        .ToArray();
413      TestNormalizedMeanSquaredError = testNmseCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
414        .ToArray();
415
416      TrainingDirectionalSymmetry = trainingDsCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : 0.0)
417        .ToArray();
418      TestDirectionalSymmetry = testDsCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : 0.0)
419        .ToArray();
420      TrainingWeightedDirectionalSymmetry = trainingWdsCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
421        .ToArray();
422      TestWeightedDirectionalSymmetry = testWdsCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
423        .ToArray();
424      TrainingTheilsUStatisticLast = trainingTheilsULastCalculators
425        .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
426        .ToArray();
427      TestTheilsUStatisticLast = testTheilsULastCalculators
428        .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
429        .ToArray();
430      TrainingTheilsUStatisticMean = trainingTheilsUMeanCalculators
431        .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
432        .ToArray();
433      TestTheilsUStatisticMean = testTheilsUMeanCalculators
434        .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
435        .ToArray();
436      TrainingTheilsUStatisticMovingAverage = trainingTheilsUMovingAverageCalculators
437        .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
438        .ToArray();
439      TestTheilsUStatisticMovingAverage = testTheilsUMovingAverageCalculators
440        .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity)
441        .ToArray();
442    }
443  }
444}
Note: See TracBrowser for help on using the repository browser.