Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineDirectionalSymmetryCalculator.cs @ 7080

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