Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10635 was 9462, checked in by swagner, 12 years ago

Updated copyright year and incremented version of plugins, applications and assembly files (#1889)

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