source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineWeightedDirectionalSymmetryCalculator.cs @ 6974

Last change on this file since 6974 was 6974, checked in by gkronber, 8 years ago

#1081: cleared up definition of accuracy metrics for time series prognosis to make the distinction between one n-step forecast and n one-step forecasts clearer. Implemented calculators for time series accuracy metrics to support the calculation of the average accuracy over m n-step forecasts and adapted the unit tests accordingly.

File size: 5.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 HeuristicLab.Common;
25
26
27namespace HeuristicLab.Problems.DataAnalysis {
28  public class OnlineWeightedDirectionalSymmetryCalculator : IOnlineTimeSeriesCalculator {
29    private double prevEstimated;
30    private double prevOriginal;
31    private int n;
32    private double correctSum;
33    private double incorrectSum;
34
35    public double WeightedDirectionalSymmetry {
36      get {
37        if (n <= 1) return 0.0;
38        return incorrectSum / correctSum;
39      }
40    }
41
42    public OnlineWeightedDirectionalSymmetryCalculator() {
43      Reset();
44    }
45
46    public double Value {
47      get { return WeightedDirectionalSymmetry; }
48    }
49
50    private OnlineCalculatorError errorState;
51    public OnlineCalculatorError ErrorState {
52      get { return errorState; }
53    }
54
55    public void Add(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation) {
56      if (double.IsNaN(startValue) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
57        errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
58      } else {
59        var actualEnumerator = actualContinuation.GetEnumerator();
60        var predictedEnumerator = predictedContinuation.GetEnumerator();
61        while (actualEnumerator.MoveNext() & predictedEnumerator.MoveNext() & errorState != OnlineCalculatorError.InvalidValueAdded) {
62          double actual = actualEnumerator.Current;
63          double predicted = predictedEnumerator.Current;
64          if (double.IsNaN(actual) || double.IsNaN(predicted)) {
65            errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
66          } else {
67            double err = Math.Abs(actual - predicted);
68            // count as correct only if the trend (positive/negative/no change) is predicted correctly
69            if ((actual - startValue) * (predicted - startValue) > 0.0 ||
70              (actual - startValue).IsAlmost(predicted - startValue)) {
71              correctSum += err;
72            } else {
73              incorrectSum += err;
74            }
75            n++;
76          }
77        }
78        // check if both enumerators are at the end to make sure both enumerations have the same length
79        if (actualEnumerator.MoveNext() || predictedEnumerator.MoveNext()) {
80          errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
81        } else {
82          errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
83        }
84      }
85    }
86
87    public void Reset() {
88      n = 0;
89      correctSum = 0;
90      incorrectSum = 0;
91      prevOriginal = double.NaN;
92      prevEstimated = double.NaN;
93      errorState = OnlineCalculatorError.InsufficientElementsAdded;
94    }
95
96
97    public static double Calculate(IEnumerable<double> startValues, IEnumerable<IEnumerable<double>> actualContinuations, IEnumerable<IEnumerable<double>> predictedContinuations, out OnlineCalculatorError errorState) {
98      IEnumerator<double> startValueEnumerator = startValues.GetEnumerator();
99      IEnumerator<IEnumerable<double>> actualContinuationsEnumerator = actualContinuations.GetEnumerator();
100      IEnumerator<IEnumerable<double>> predictedContinuationsEnumerator = predictedContinuations.GetEnumerator();
101      OnlineWeightedDirectionalSymmetryCalculator calculator = new OnlineWeightedDirectionalSymmetryCalculator();
102
103      // always move forward all enumerators (do not use short-circuit evaluation!)
104      while (startValueEnumerator.MoveNext() & actualContinuationsEnumerator.MoveNext() & predictedContinuationsEnumerator.MoveNext()) {
105        calculator.Add(startValueEnumerator.Current, actualContinuationsEnumerator.Current, predictedContinuationsEnumerator.Current);
106        if (calculator.ErrorState != OnlineCalculatorError.None) break;
107      }
108
109      // check if all enumerators are at the end to make sure both enumerations have the same length
110      if (calculator.ErrorState == OnlineCalculatorError.None &&
111          (startValueEnumerator.MoveNext() || actualContinuationsEnumerator.MoveNext() || predictedContinuationsEnumerator.MoveNext())) {
112        throw new ArgumentException("Number of elements in startValues, actualContinuations and estimatedValues predictedContinuations doesn't match.");
113      } else {
114        errorState = calculator.ErrorState;
115        return calculator.WeightedDirectionalSymmetry;
116      }
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.