[6802] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17097] | 3 | * Copyright (C) 2002-2019 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[14801] | 24 | using HeuristicLab.Common;
|
---|
[6802] | 25 |
|
---|
| 26 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
[14801] | 27 | public class OnlineTheilsUStatisticCalculator : DeepCloneable, IOnlineTimeSeriesCalculator {
|
---|
| 28 | private readonly OnlineMeanAndVarianceCalculator squaredErrorMeanCalculator;
|
---|
| 29 | private readonly OnlineMeanAndVarianceCalculator unbiasedEstimatorMeanCalculator;
|
---|
[6802] | 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 |
|
---|
[14801] | 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 |
|
---|
[6802] | 57 | #region IOnlineEvaluator Members
|
---|
| 58 | public double Value {
|
---|
| 59 | get { return TheilsUStatistic; }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[7099] | 62 | public void Add(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation) {
|
---|
[8468] | 63 | throw new NotSupportedException();
|
---|
[7160] | 64 | }
|
---|
| 65 |
|
---|
[8430] | 66 | public void Add(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> referenceContinuation, IEnumerable<double> predictedContinuation) {
|
---|
[7099] | 67 | if (double.IsNaN(startValue) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
|
---|
[6802] | 68 | errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
| 69 | } else {
|
---|
[7099] | 70 | var actualEnumerator = actualContinuation.GetEnumerator();
|
---|
| 71 | var predictedEnumerator = predictedContinuation.GetEnumerator();
|
---|
[7160] | 72 | var referenceEnumerator = referenceContinuation.GetEnumerator();
|
---|
| 73 | while (actualEnumerator.MoveNext() & predictedEnumerator.MoveNext() & referenceEnumerator.MoveNext()
|
---|
| 74 | & ErrorState != OnlineCalculatorError.InvalidValueAdded) {
|
---|
[7099] | 75 | double actual = actualEnumerator.Current;
|
---|
| 76 | double predicted = predictedEnumerator.Current;
|
---|
[7160] | 77 | double reference = referenceEnumerator.Current;
|
---|
| 78 | if (double.IsNaN(actual) || double.IsNaN(predicted) || double.IsNaN(reference)) {
|
---|
[7099] | 79 | errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
| 80 | } else {
|
---|
| 81 | // error of predicted change
|
---|
| 82 | double errorPredictedChange = (predicted - startValue) - (actual - startValue);
|
---|
| 83 | squaredErrorMeanCalculator.Add(errorPredictedChange * errorPredictedChange);
|
---|
[6802] | 84 |
|
---|
[7160] | 85 | double errorReference = (reference - startValue) - (actual - startValue);
|
---|
| 86 | unbiasedEstimatorMeanCalculator.Add(errorReference * errorReference);
|
---|
[7099] | 87 | }
|
---|
| 88 | }
|
---|
| 89 | // check if both enumerators are at the end to make sure both enumerations have the same length
|
---|
[7160] | 90 | if (actualEnumerator.MoveNext() || predictedEnumerator.MoveNext() || referenceEnumerator.MoveNext()) {
|
---|
[7099] | 91 | errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
| 92 | } else {
|
---|
| 93 | errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
|
---|
| 94 | }
|
---|
[6802] | 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[7160] | 98 |
|
---|
[6802] | 99 | public void Reset() {
|
---|
| 100 | squaredErrorMeanCalculator.Reset();
|
---|
| 101 | unbiasedEstimatorMeanCalculator.Reset();
|
---|
| 102 | errorState = OnlineCalculatorError.InsufficientElementsAdded;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | #endregion
|
---|
| 106 |
|
---|
[8468] | 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 |
|
---|
[8010] | 114 | public static double Calculate(IEnumerable<double> startValues, IEnumerable<IEnumerable<double>> actualContinuations, IEnumerable<IEnumerable<double>> referenceContinuations, IEnumerable<IEnumerable<double>> predictedContinuations, out OnlineCalculatorError errorState) {
|
---|
[7099] | 115 | IEnumerator<double> startValueEnumerator = startValues.GetEnumerator();
|
---|
| 116 | IEnumerator<IEnumerable<double>> actualContinuationsEnumerator = actualContinuations.GetEnumerator();
|
---|
[8010] | 117 | IEnumerator<IEnumerable<double>> referenceContinuationsEnumerator = referenceContinuations.GetEnumerator();
|
---|
[7099] | 118 | IEnumerator<IEnumerable<double>> predictedContinuationsEnumerator = predictedContinuations.GetEnumerator();
|
---|
[8010] | 119 |
|
---|
[6802] | 120 | OnlineTheilsUStatisticCalculator calculator = new OnlineTheilsUStatisticCalculator();
|
---|
| 121 |
|
---|
[7099] | 122 | // always move forward all enumerators (do not use short-circuit evaluation!)
|
---|
[8010] | 123 | while (startValueEnumerator.MoveNext() & actualContinuationsEnumerator.MoveNext() & referenceContinuationsEnumerator.MoveNext() & predictedContinuationsEnumerator.MoveNext()) {
|
---|
| 124 | calculator.Add(startValueEnumerator.Current, actualContinuationsEnumerator.Current, referenceContinuationsEnumerator.Current, predictedContinuationsEnumerator.Current);
|
---|
[6802] | 125 | if (calculator.ErrorState != OnlineCalculatorError.None) break;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[7099] | 128 | // check if all enumerators are at the end to make sure both enumerations have the same length
|
---|
[6802] | 129 | if (calculator.ErrorState == OnlineCalculatorError.None &&
|
---|
[8010] | 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.");
|
---|
[6802] | 132 | } else {
|
---|
| 133 | errorState = calculator.ErrorState;
|
---|
| 134 | return calculator.TheilsUStatistic;
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 | }
|
---|