[4113] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using HeuristicLab.Core;
|
---|
| 6 | using HeuristicLab.Data;
|
---|
| 7 | using HeuristicLab.Problems.DataAnalysis.Evaluators;
|
---|
| 8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 9 | using HeuristicLab.Parameters;
|
---|
| 10 |
|
---|
| 11 | namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis {
|
---|
| 12 | public class OnlineTheilsUStatisticEvaluator : IOnlineTimeSeriesPrognosisEvaluator {
|
---|
| 13 | private OnlineMeanAndVarianceCalculator squaredErrorMeanCalculator;
|
---|
| 14 | private OnlineMeanAndVarianceCalculator unbiasedEstimatorMeanCalculator;
|
---|
| 15 | private double prevOriginal;
|
---|
[5305] | 16 | private int windowSize;
|
---|
| 17 | private Queue<double> movingAverageWindow;
|
---|
[4113] | 18 |
|
---|
| 19 | public double TheilsUStatistic {
|
---|
| 20 | get {
|
---|
| 21 | return Math.Sqrt(squaredErrorMeanCalculator.Mean) / Math.Sqrt(unbiasedEstimatorMeanCalculator.Mean);
|
---|
| 22 | }
|
---|
| 23 | }
|
---|
| 24 |
|
---|
[5305] | 25 | public OnlineTheilsUStatisticEvaluator()
|
---|
| 26 | : this(1) {
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | public OnlineTheilsUStatisticEvaluator(int movingAverageWindowSize) {
|
---|
| 30 | this.windowSize = movingAverageWindowSize;
|
---|
| 31 | movingAverageWindow = new Queue<double>(windowSize);
|
---|
[4113] | 32 | squaredErrorMeanCalculator = new OnlineMeanAndVarianceCalculator();
|
---|
| 33 | unbiasedEstimatorMeanCalculator = new OnlineMeanAndVarianceCalculator();
|
---|
| 34 | Reset();
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | #region IOnlineEvaluator Members
|
---|
| 38 | public double Value {
|
---|
| 39 | get { return TheilsUStatistic; }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public void Add(double original, double estimated) {
|
---|
| 43 | if (double.IsInfinity(original) || double.IsNaN(original) || double.IsInfinity(estimated) || double.IsNaN(estimated)) {
|
---|
| 44 | throw new ArgumentException("Theil's U-statistic is not defined for series containing NaN or infinity values.");
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | if (!double.IsNaN(prevOriginal)) {
|
---|
| 48 | // error of predicted change
|
---|
| 49 | double errorEstimatedChange = (estimated - original);
|
---|
| 50 | squaredErrorMeanCalculator.Add(errorEstimatedChange * errorEstimatedChange);
|
---|
| 51 |
|
---|
[5305] | 52 | // calculate trend observed in the MA window
|
---|
| 53 | double d = CalculateTrend(movingAverageWindow);
|
---|
| 54 |
|
---|
| 55 | // shift window forward
|
---|
| 56 | if (movingAverageWindow.Count == windowSize) {
|
---|
| 57 | movingAverageWindow.Dequeue();
|
---|
| 58 | }
|
---|
| 59 | movingAverageWindow.Enqueue(original);
|
---|
| 60 |
|
---|
| 61 | double errorNoChange = (original - prevOriginal * (1+d));
|
---|
[4113] | 62 | unbiasedEstimatorMeanCalculator.Add(errorNoChange * errorNoChange);
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[5305] | 66 | private double CalculateTrend(Queue<double> movingAverageWindow) {
|
---|
| 67 | double[] xs = movingAverageWindow.ToArray();
|
---|
| 68 | double sum = 0.0;
|
---|
| 69 | for (int i = 0; i < xs.Length - 1; i++) {
|
---|
| 70 | sum += (xs[i + 1] - xs[i]) / xs[i];
|
---|
| 71 | }
|
---|
| 72 | return sum / xs.Length;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[4113] | 75 | public void Reset() {
|
---|
| 76 | prevOriginal = double.NaN;
|
---|
| 77 | squaredErrorMeanCalculator.Reset();
|
---|
| 78 | unbiasedEstimatorMeanCalculator.Reset();
|
---|
[5305] | 79 | movingAverageWindow.Clear();
|
---|
[4113] | 80 | }
|
---|
| 81 |
|
---|
| 82 | #endregion
|
---|
| 83 |
|
---|
| 84 | #region IOnlineTimeSeriesPrognosisEvaluator Members
|
---|
| 85 |
|
---|
| 86 | public void StartNewPredictionWindow(double referenceOriginalValue) {
|
---|
| 87 | prevOriginal = referenceOriginalValue;
|
---|
[5305] | 88 | movingAverageWindow.Enqueue(referenceOriginalValue);
|
---|
[4113] | 89 | }
|
---|
| 90 |
|
---|
| 91 | #endregion
|
---|
| 92 | }
|
---|
| 93 | }
|
---|