Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8430 was 8430, checked in by mkommend, 12 years ago

#1081: Intermediate commit of trunk updates - interpreter changes must be redone.

File size: 6.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 System.Linq;
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      Add(startValue, actualContinuation.Select(x => startValue), actualContinuation, predictedContinuation);
55    }
56
57    public void Add(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> referenceContinuation, IEnumerable<double> predictedContinuation) {
58      if (double.IsNaN(startValue) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
59        errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
60      } else {
61        var actualEnumerator = actualContinuation.GetEnumerator();
62        var predictedEnumerator = predictedContinuation.GetEnumerator();
63        var referenceEnumerator = referenceContinuation.GetEnumerator();
64        while (actualEnumerator.MoveNext() & predictedEnumerator.MoveNext() & referenceEnumerator.MoveNext()
65          & ErrorState != OnlineCalculatorError.InvalidValueAdded) {
66          double actual = actualEnumerator.Current;
67          double predicted = predictedEnumerator.Current;
68          double reference = referenceEnumerator.Current;
69          if (double.IsNaN(actual) || double.IsNaN(predicted) || double.IsNaN(reference)) {
70            errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
71          } else {
72            // error of predicted change
73            double errorPredictedChange = (predicted - startValue) - (actual - startValue);
74            squaredErrorMeanCalculator.Add(errorPredictedChange * errorPredictedChange);
75
76            double errorReference = (reference - startValue) - (actual - startValue);
77            unbiasedEstimatorMeanCalculator.Add(errorReference * errorReference);
78          }
79        }
80        // check if both enumerators are at the end to make sure both enumerations have the same length
81        if (actualEnumerator.MoveNext() || predictedEnumerator.MoveNext() || referenceEnumerator.MoveNext()) {
82          errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
83        } else {
84          errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
85        }
86      }
87    }
88
89
90    public void Reset() {
91      squaredErrorMeanCalculator.Reset();
92      unbiasedEstimatorMeanCalculator.Reset();
93      errorState = OnlineCalculatorError.InsufficientElementsAdded;
94    }
95
96    #endregion
97
98    public static double Calculate(IEnumerable<double> startValues, IEnumerable<IEnumerable<double>> actualContinuations, IEnumerable<IEnumerable<double>> referenceContinuations, IEnumerable<IEnumerable<double>> predictedContinuations, out OnlineCalculatorError errorState) {
99      IEnumerator<double> startValueEnumerator = startValues.GetEnumerator();
100      IEnumerator<IEnumerable<double>> actualContinuationsEnumerator = actualContinuations.GetEnumerator();
101      IEnumerator<IEnumerable<double>> referenceContinuationsEnumerator = referenceContinuations.GetEnumerator();
102      IEnumerator<IEnumerable<double>> predictedContinuationsEnumerator = predictedContinuations.GetEnumerator();
103
104      OnlineTheilsUStatisticCalculator calculator = new OnlineTheilsUStatisticCalculator();
105
106      // always move forward all enumerators (do not use short-circuit evaluation!)
107      while (startValueEnumerator.MoveNext() & actualContinuationsEnumerator.MoveNext() & referenceContinuationsEnumerator.MoveNext() & predictedContinuationsEnumerator.MoveNext()) {
108        calculator.Add(startValueEnumerator.Current, actualContinuationsEnumerator.Current, referenceContinuationsEnumerator.Current, predictedContinuationsEnumerator.Current);
109        if (calculator.ErrorState != OnlineCalculatorError.None) break;
110      }
111
112      // check if all enumerators are at the end to make sure both enumerations have the same length
113      if (calculator.ErrorState == OnlineCalculatorError.None &&
114          (startValueEnumerator.MoveNext() || actualContinuationsEnumerator.MoveNext() || referenceContinuationsEnumerator.MoveNext() || predictedContinuationsEnumerator.MoveNext())) {
115        throw new ArgumentException("Number of elements in startValues, actualContinuations, referenceContinuation and estimatedValues predictedContinuations doesn't match.");
116      } else {
117        errorState = calculator.ErrorState;
118        return calculator.TheilsUStatistic;
119      }
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.