Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineDirectionalSymmetryCalculator.cs @ 10479

Last change on this file since 10479 was 9462, checked in by swagner, 12 years ago

Updated copyright year and incremented version of plugins, applications and assembly files (#1889)

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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;
24using HeuristicLab.Common;
25
26
27namespace HeuristicLab.Problems.DataAnalysis {
28  public class OnlineDirectionalSymmetryCalculator : IOnlineTimeSeriesCalculator {
29    private int n;
30    private int nCorrect;
31
32    public double DirectionalSymmetry {
33      get {
34        if (n < 1) return 0.0;
35        return (double)nCorrect / n;
36      }
37    }
38
39    public OnlineDirectionalSymmetryCalculator() {
40      Reset();
41    }
42
43    public double Value {
44      get { return DirectionalSymmetry; }
45    }
46
47    private OnlineCalculatorError errorState;
48    public OnlineCalculatorError ErrorState {
49      get { return errorState; }
50    }
51
52    public void Add(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation) {
53      if (double.IsNaN(startValue) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
54        errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
55      } else {
56        var actualEnumerator = actualContinuation.GetEnumerator();
57        var predictedEnumerator = predictedContinuation.GetEnumerator();
58        double prevActual = startValue;
59        double prevPredicted = startValue;
60        while (actualEnumerator.MoveNext() & predictedEnumerator.MoveNext() & errorState != OnlineCalculatorError.InvalidValueAdded) {
61          double actual = actualEnumerator.Current;
62          double predicted = predictedEnumerator.Current;
63          if (double.IsNaN(actual) || double.IsNaN(predicted)) {
64            errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
65          } else {
66            // count a prediction correct if the trend (positive/negative/no change) is predicted correctly
67            if ((actual - prevActual) * (predicted - prevPredicted) > 0.0 ||
68                (actual - prevActual).IsAlmost(predicted - prevPredicted)
69              ) {
70              nCorrect++;
71            }
72            n++;
73          }
74        }
75        // check if both enumerators are at the end to make sure both enumerations have the same length
76        if (actualEnumerator.MoveNext() || predictedEnumerator.MoveNext()) {
77          errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
78        } else {
79          errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
80        }
81      }
82    }
83
84    public void Reset() {
85      n = 0;
86      nCorrect = 0;
87      errorState = OnlineCalculatorError.InsufficientElementsAdded;
88    }
89
90    public static double Calculate(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation, out OnlineCalculatorError errorState) {
91      OnlineDirectionalSymmetryCalculator dsCalculator = new OnlineDirectionalSymmetryCalculator();
92      dsCalculator.Add(startValue, actualContinuation, predictedContinuation);
93      errorState = dsCalculator.ErrorState;
94      return dsCalculator.DirectionalSymmetry;
95    }
96   
97    public static double Calculate(IEnumerable<double> startValues, IEnumerable<IEnumerable<double>> actualContinuations, IEnumerable<IEnumerable<double>> predictedContinuations, out OnlineCalculatorError errorState) {
98      IEnumerator<double> startValueEnumerator = startValues.GetEnumerator();
99      IEnumerator<IEnumerable<double>> actualContinuationsEnumerator = actualContinuations.GetEnumerator();
100      IEnumerator<IEnumerable<double>> predictedContinuationsEnumerator = predictedContinuations.GetEnumerator();
101      OnlineDirectionalSymmetryCalculator dsCalculator = new OnlineDirectionalSymmetryCalculator();
102
103      // always move forward all enumerators (do not use short-circuit evaluation!)
104      while (startValueEnumerator.MoveNext() & actualContinuationsEnumerator.MoveNext() & predictedContinuationsEnumerator.MoveNext()) {
105        dsCalculator.Add(startValueEnumerator.Current, actualContinuationsEnumerator.Current, predictedContinuationsEnumerator.Current);
106        if (dsCalculator.ErrorState != OnlineCalculatorError.None) break;
107      }
108
109      // check if all enumerators are at the end to make sure both enumerations have the same length
110      if (dsCalculator.ErrorState == OnlineCalculatorError.None &&
111          (startValueEnumerator.MoveNext() || actualContinuationsEnumerator.MoveNext() || predictedContinuationsEnumerator.MoveNext())) {
112        throw new ArgumentException("Number of elements in startValues, actualContinuations and estimatedValues predictedContinuations doesn't match.");
113      } else {
114        errorState = dsCalculator.ErrorState;
115        return dsCalculator.DirectionalSymmetry;
116      }
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.