Free cookie consent management tool by TermsFeed Policy Generator

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

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