Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineTheilsUStatisticCalculator.cs @ 7624

Last change on this file since 7624 was 7160, checked in by gkronber, 13 years ago

#1081 worked on multi-variate time series prognosis

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