Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14293 was 14293, checked in by bburlacu, 8 years ago

#2672: Implement online calculator cloning.

File size: 8.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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, IDeepCloneable {
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    // private constructor used internally by the Clone() method
49    private OnlineTheilsUStatisticCalculator(OnlineMeanAndVarianceCalculator squaredErrorMeanCalculator, OnlineMeanAndVarianceCalculator unbiasedEstimatorMeanCalculator) {
50      this.squaredErrorMeanCalculator = squaredErrorMeanCalculator;
51      this.unbiasedEstimatorMeanCalculator = unbiasedEstimatorMeanCalculator;
52    }
53
54    #region IOnlineEvaluator Members
55    public double Value {
56      get { return TheilsUStatistic; }
57    }
58
59    public void Add(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation) {
60      throw new NotSupportedException();
61    }
62
63    public void Add(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> referenceContinuation, IEnumerable<double> predictedContinuation) {
64      if (double.IsNaN(startValue) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
65        errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
66      } else {
67        var actualEnumerator = actualContinuation.GetEnumerator();
68        var predictedEnumerator = predictedContinuation.GetEnumerator();
69        var referenceEnumerator = referenceContinuation.GetEnumerator();
70        while (actualEnumerator.MoveNext() & predictedEnumerator.MoveNext() & referenceEnumerator.MoveNext()
71          & ErrorState != OnlineCalculatorError.InvalidValueAdded) {
72          double actual = actualEnumerator.Current;
73          double predicted = predictedEnumerator.Current;
74          double reference = referenceEnumerator.Current;
75          if (double.IsNaN(actual) || double.IsNaN(predicted) || double.IsNaN(reference)) {
76            errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
77          } else {
78            // error of predicted change
79            double errorPredictedChange = (predicted - startValue) - (actual - startValue);
80            squaredErrorMeanCalculator.Add(errorPredictedChange * errorPredictedChange);
81
82            double errorReference = (reference - startValue) - (actual - startValue);
83            unbiasedEstimatorMeanCalculator.Add(errorReference * errorReference);
84          }
85        }
86        // check if both enumerators are at the end to make sure both enumerations have the same length
87        if (actualEnumerator.MoveNext() || predictedEnumerator.MoveNext() || referenceEnumerator.MoveNext()) {
88          errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
89        } else {
90          errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
91        }
92      }
93    }
94
95
96    public void Reset() {
97      squaredErrorMeanCalculator.Reset();
98      unbiasedEstimatorMeanCalculator.Reset();
99      errorState = OnlineCalculatorError.InsufficientElementsAdded;
100    }
101
102    #endregion
103
104    public static double Calculate(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> referenceContinuation, IEnumerable<double> predictedContinuation, out OnlineCalculatorError errorState) {
105      OnlineTheilsUStatisticCalculator calculator = new OnlineTheilsUStatisticCalculator();
106      calculator.Add(startValue, actualContinuation, referenceContinuation, predictedContinuation);
107      errorState = calculator.ErrorState;
108      return calculator.TheilsUStatistic;
109    }
110
111    public static double Calculate(IEnumerable<double> startValues, IEnumerable<IEnumerable<double>> actualContinuations, IEnumerable<IEnumerable<double>> referenceContinuations, IEnumerable<IEnumerable<double>> predictedContinuations, out OnlineCalculatorError errorState) {
112      IEnumerator<double> startValueEnumerator = startValues.GetEnumerator();
113      IEnumerator<IEnumerable<double>> actualContinuationsEnumerator = actualContinuations.GetEnumerator();
114      IEnumerator<IEnumerable<double>> referenceContinuationsEnumerator = referenceContinuations.GetEnumerator();
115      IEnumerator<IEnumerable<double>> predictedContinuationsEnumerator = predictedContinuations.GetEnumerator();
116
117      OnlineTheilsUStatisticCalculator calculator = new OnlineTheilsUStatisticCalculator();
118
119      // always move forward all enumerators (do not use short-circuit evaluation!)
120      while (startValueEnumerator.MoveNext() & actualContinuationsEnumerator.MoveNext() & referenceContinuationsEnumerator.MoveNext() & predictedContinuationsEnumerator.MoveNext()) {
121        calculator.Add(startValueEnumerator.Current, actualContinuationsEnumerator.Current, referenceContinuationsEnumerator.Current, predictedContinuationsEnumerator.Current);
122        if (calculator.ErrorState != OnlineCalculatorError.None) break;
123      }
124
125      // check if all enumerators are at the end to make sure both enumerations have the same length
126      if (calculator.ErrorState == OnlineCalculatorError.None &&
127          (startValueEnumerator.MoveNext() || actualContinuationsEnumerator.MoveNext() || referenceContinuationsEnumerator.MoveNext() || predictedContinuationsEnumerator.MoveNext())) {
128        throw new ArgumentException("Number of elements in startValues, actualContinuations, referenceContinuation and estimatedValues predictedContinuations doesn't match.");
129      } else {
130        errorState = calculator.ErrorState;
131        return calculator.TheilsUStatistic;
132      }
133    }
134
135    // IDeepCloneable members
136    public object Clone() {
137      var squaredErrorMeanCalculatorClone = (OnlineMeanAndVarianceCalculator)squaredErrorMeanCalculator.Clone();
138      var unbiasedEstimatorMeanCalculatorClone = (OnlineMeanAndVarianceCalculator)unbiasedEstimatorMeanCalculator.Clone();
139      return new OnlineTheilsUStatisticCalculator(squaredErrorMeanCalculatorClone, unbiasedEstimatorMeanCalculatorClone);
140    }
141
142    public IDeepCloneable Clone(Cloner cloner) {
143      var clone = cloner.GetClone(this);
144      if (clone == null) {
145        var squaredErrorMeanCalculatorClone = (OnlineMeanAndVarianceCalculator)squaredErrorMeanCalculator.Clone(cloner);
146        var unbiasedEstimatorMeanCalculatorClone = (OnlineMeanAndVarianceCalculator)unbiasedEstimatorMeanCalculator.Clone(cloner);
147        clone = new OnlineTheilsUStatisticCalculator(squaredErrorMeanCalculatorClone, unbiasedEstimatorMeanCalculatorClone);
148        cloner.RegisterClonedObject(this, clone);
149      }
150      return clone;
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.