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;
|
---|
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 | }
|
---|