Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4949 was 4113, checked in by gkronber, 14 years ago

Added plugin for time series prognosis. #1081

File size: 2.3 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
17    public double TheilsUStatistic {
18      get {
19        return Math.Sqrt(squaredErrorMeanCalculator.Mean) / Math.Sqrt(unbiasedEstimatorMeanCalculator.Mean);
20      }
21    }
22
23    public OnlineTheilsUStatisticEvaluator() {
24      squaredErrorMeanCalculator = new OnlineMeanAndVarianceCalculator();
25      unbiasedEstimatorMeanCalculator = new OnlineMeanAndVarianceCalculator();
26      Reset();
27    }
28
29    #region IOnlineEvaluator Members
30    public double Value {
31      get { return TheilsUStatistic; }
32    }
33
34    public void Add(double original, double estimated) {
35      if (double.IsInfinity(original) || double.IsNaN(original) || double.IsInfinity(estimated) || double.IsNaN(estimated)) {
36        throw new ArgumentException("Theil's U-statistic is not defined for series containing NaN or infinity values.");
37      }
38
39      if (!double.IsNaN(prevOriginal)) {
40        // error of predicted change
41        double errorEstimatedChange = (estimated - original);
42        squaredErrorMeanCalculator.Add(errorEstimatedChange * errorEstimatedChange);
43
44        // error of naive model y(t+1) = y(t)
45        double errorNoChange = (original - prevOriginal);
46        unbiasedEstimatorMeanCalculator.Add(errorNoChange * errorNoChange);
47      }
48    }
49
50    public void Reset() {
51      prevOriginal = double.NaN;
52      squaredErrorMeanCalculator.Reset();
53      unbiasedEstimatorMeanCalculator.Reset();
54    }
55
56    #endregion
57
58    #region IOnlineTimeSeriesPrognosisEvaluator Members
59
60    public void StartNewPredictionWindow(double referenceOriginalValue) {
61      prevOriginal = referenceOriginalValue;
62    }
63
64    #endregion
65  }
66}
Note: See TracBrowser for help on using the repository browser.