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