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