[6802] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[7085] | 24 | using HeuristicLab.Common;
|
---|
[6802] | 25 |
|
---|
| 26 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
[7085] | 27 | public class OnlineTheilsUStatisticCalculator : IOnlineTimeSeriesCalculator {
|
---|
[6802] | 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 |
|
---|
[7085] | 53 | public void Add(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation) {
|
---|
| 54 | if (double.IsNaN(startValue) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
|
---|
[6802] | 55 | errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
| 56 | } else {
|
---|
[7085] | 57 | var actualEnumerator = actualContinuation.GetEnumerator();
|
---|
| 58 | var predictedEnumerator = predictedContinuation.GetEnumerator();
|
---|
| 59 | while (actualEnumerator.MoveNext() & predictedEnumerator.MoveNext() & ErrorState != OnlineCalculatorError.InvalidValueAdded) {
|
---|
| 60 | double actual = actualEnumerator.Current;
|
---|
| 61 | double predicted = predictedEnumerator.Current;
|
---|
| 62 | if (double.IsNaN(actual) || double.IsNaN(predicted)) {
|
---|
| 63 | errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
| 64 | } else {
|
---|
| 65 | // error of predicted change
|
---|
| 66 | double errorPredictedChange = (predicted - startValue) - (actual - startValue);
|
---|
| 67 | squaredErrorMeanCalculator.Add(errorPredictedChange * errorPredictedChange);
|
---|
[6802] | 68 |
|
---|
[7085] | 69 | double errorNoChange = (actual - startValue);
|
---|
| 70 | unbiasedEstimatorMeanCalculator.Add(errorNoChange * errorNoChange);
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | // check if both enumerators are at the end to make sure both enumerations have the same length
|
---|
| 74 | if (actualEnumerator.MoveNext() || predictedEnumerator.MoveNext()) {
|
---|
| 75 | errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
| 76 | } else {
|
---|
| 77 | errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
|
---|
| 78 | }
|
---|
[6802] | 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | public void Reset() {
|
---|
| 83 | squaredErrorMeanCalculator.Reset();
|
---|
| 84 | unbiasedEstimatorMeanCalculator.Reset();
|
---|
| 85 | errorState = OnlineCalculatorError.InsufficientElementsAdded;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | #endregion
|
---|
| 89 |
|
---|
[7085] | 90 | public static double Calculate(IEnumerable<double> startValues, IEnumerable<IEnumerable<double>> actualContinuations, IEnumerable<IEnumerable<double>> predictedContinuations, out OnlineCalculatorError errorState) {
|
---|
| 91 | IEnumerator<double> startValueEnumerator = startValues.GetEnumerator();
|
---|
| 92 | IEnumerator<IEnumerable<double>> actualContinuationsEnumerator = actualContinuations.GetEnumerator();
|
---|
| 93 | IEnumerator<IEnumerable<double>> predictedContinuationsEnumerator = predictedContinuations.GetEnumerator();
|
---|
[6802] | 94 | OnlineTheilsUStatisticCalculator calculator = new OnlineTheilsUStatisticCalculator();
|
---|
| 95 |
|
---|
[7085] | 96 | // always move forward all enumerators (do not use short-circuit evaluation!)
|
---|
| 97 | while (startValueEnumerator.MoveNext() & actualContinuationsEnumerator.MoveNext() & predictedContinuationsEnumerator.MoveNext()) {
|
---|
| 98 | calculator.Add(startValueEnumerator.Current, actualContinuationsEnumerator.Current, predictedContinuationsEnumerator.Current);
|
---|
[6802] | 99 | if (calculator.ErrorState != OnlineCalculatorError.None) break;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[7085] | 102 | // check if all enumerators are at the end to make sure both enumerations have the same length
|
---|
[6802] | 103 | if (calculator.ErrorState == OnlineCalculatorError.None &&
|
---|
[7085] | 104 | (startValueEnumerator.MoveNext() || actualContinuationsEnumerator.MoveNext() || predictedContinuationsEnumerator.MoveNext())) {
|
---|
| 105 | throw new ArgumentException("Number of elements in startValues, actualContinuations and estimatedValues predictedContinuations doesn't match.");
|
---|
[6802] | 106 | } else {
|
---|
| 107 | errorState = calculator.ErrorState;
|
---|
| 108 | return calculator.TheilsUStatistic;
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | }
|
---|