Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineTheilsUStatisticCalculator.cs @ 6802

Last change on this file since 6802 was 6802, checked in by gkronber, 13 years ago

#1081 added classes (problem, evaluators, analyzers, solution, model, online-calculators, and views) for time series prognosis problems and added an algorithm implementation to generation linear AR (auto-regressive) time series prognosis solution.

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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
22using System;
23using System.Collections.Generic;
24
25namespace HeuristicLab.Problems.DataAnalysis {
26  public class OnlineTheilsUStatisticCalculator : IOnlineCalculator {
27    private OnlineMeanAndVarianceCalculator squaredErrorMeanCalculator;
28    private OnlineMeanAndVarianceCalculator unbiasedEstimatorMeanCalculator;
29    private double prevOriginal;
30    private int n;
31
32    public double TheilsUStatistic {
33      get {
34        return Math.Sqrt(squaredErrorMeanCalculator.Mean) / Math.Sqrt(unbiasedEstimatorMeanCalculator.Mean);
35      }
36    }
37
38    private OnlineCalculatorError errorState;
39    public OnlineCalculatorError ErrorState {
40      get { return errorState | squaredErrorMeanCalculator.MeanErrorState | unbiasedEstimatorMeanCalculator.MeanErrorState; }
41    }
42
43    public OnlineTheilsUStatisticCalculator() {
44      squaredErrorMeanCalculator = new OnlineMeanAndVarianceCalculator();
45      unbiasedEstimatorMeanCalculator = new OnlineMeanAndVarianceCalculator();
46      Reset();
47    }
48
49    #region IOnlineEvaluator Members
50    public double Value {
51      get { return TheilsUStatistic; }
52    }
53
54    public void Add(double original, double estimated) {
55      if (double.IsNaN(estimated) || double.IsInfinity(estimated) || double.IsNaN(original) || double.IsInfinity(original) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
56        errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
57      } else if (n == 0) {
58        prevOriginal = original;
59        n++;
60      } else {
61        // error of predicted change
62        double errorEstimatedChange = (estimated - original);
63        squaredErrorMeanCalculator.Add(errorEstimatedChange * errorEstimatedChange);
64
65        double errorNoChange = (original - prevOriginal);
66        unbiasedEstimatorMeanCalculator.Add(errorNoChange * errorNoChange);
67        errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded);        // n >= 1
68      }
69    }
70
71
72    public void Reset() {
73      prevOriginal = double.NaN;
74      n = 0;
75      squaredErrorMeanCalculator.Reset();
76      unbiasedEstimatorMeanCalculator.Reset();
77      errorState = OnlineCalculatorError.InsufficientElementsAdded;
78    }
79
80    #endregion
81
82    public static double Calculate(IEnumerable<double> estimatedValues, IEnumerable<double> originalValues, out OnlineCalculatorError errorState) {
83      IEnumerator<double> originalValuesEnumerator = originalValues.GetEnumerator();
84      IEnumerator<double> estimatedValuesEnumerator = estimatedValues.GetEnumerator();
85      OnlineTheilsUStatisticCalculator calculator = new OnlineTheilsUStatisticCalculator();
86
87      // add first element of time series as a reference point
88      originalValuesEnumerator.MoveNext();
89      estimatedValuesEnumerator.MoveNext();
90      calculator.Add(originalValuesEnumerator.Current, estimatedValuesEnumerator.Current);
91
92      // always move forward both enumerators (do not use short-circuit evaluation!)
93      while (originalValuesEnumerator.MoveNext() & estimatedValuesEnumerator.MoveNext()) {
94        double estimated = estimatedValuesEnumerator.Current;
95        double original = originalValuesEnumerator.Current;
96        calculator.Add(original, estimated);
97        if (calculator.ErrorState != OnlineCalculatorError.None) break;
98      }
99
100      // check if both enumerators are at the end to make sure both enumerations have the same length
101      if (calculator.ErrorState == OnlineCalculatorError.None &&
102          (estimatedValuesEnumerator.MoveNext() || originalValuesEnumerator.MoveNext())) {
103        throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
104      } else {
105        errorState = calculator.ErrorState;
106        return calculator.TheilsUStatistic;
107      }
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.