Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineWeightedDirectionalSymmetryCalculator.cs @ 10066

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