Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15301 was 14465, checked in by mkommend, 8 years ago

#2672: Corrected cloning of online calculators (added missing base ctor calls, moved clone method, renamed other to original, and used the cloner for cloning).

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