Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Evaluators/OnlineTheilsUStatisticEvaluator.cs @ 5469

Last change on this file since 5469 was 5305, checked in by gkronber, 14 years ago

worked on data analysis feature exploration branch. #1142

File size: 3.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Problems.DataAnalysis.Evaluators;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9using HeuristicLab.Parameters;
10
11namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis {
12  public class OnlineTheilsUStatisticEvaluator : IOnlineTimeSeriesPrognosisEvaluator {
13    private OnlineMeanAndVarianceCalculator squaredErrorMeanCalculator;
14    private OnlineMeanAndVarianceCalculator unbiasedEstimatorMeanCalculator;
15    private double prevOriginal;
16    private int windowSize;
17    private Queue<double> movingAverageWindow;
18
19    public double TheilsUStatistic {
20      get {
21        return Math.Sqrt(squaredErrorMeanCalculator.Mean) / Math.Sqrt(unbiasedEstimatorMeanCalculator.Mean);
22      }
23    }
24
25    public OnlineTheilsUStatisticEvaluator()
26      : this(1) {
27    }
28
29    public OnlineTheilsUStatisticEvaluator(int movingAverageWindowSize) {
30      this.windowSize = movingAverageWindowSize;
31      movingAverageWindow = new Queue<double>(windowSize);
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
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));
62        unbiasedEstimatorMeanCalculator.Add(errorNoChange * errorNoChange);
63      }
64    }
65
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
75    public void Reset() {
76      prevOriginal = double.NaN;
77      squaredErrorMeanCalculator.Reset();
78      unbiasedEstimatorMeanCalculator.Reset();
79      movingAverageWindow.Clear();
80    }
81
82    #endregion
83
84    #region IOnlineTimeSeriesPrognosisEvaluator Members
85
86    public void StartNewPredictionWindow(double referenceOriginalValue) {
87      prevOriginal = referenceOriginalValue;
88      movingAverageWindow.Enqueue(referenceOriginalValue);
89    }
90
91    #endregion
92  }
93}
Note: See TracBrowser for help on using the repository browser.