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

Last change on this file since 6980 was 6980, checked in by mkommend, 11 years ago

#1579: Removed unnecessary local variables in OnlineWightedDirectionalSymmetryCalculator.

File size: 5.1 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;
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
93    public static double Calculate(IEnumerable<double> startValues, IEnumerable<IEnumerable<double>> actualContinuations, IEnumerable<IEnumerable<double>> predictedContinuations, out OnlineCalculatorError errorState) {
94      IEnumerator<double> startValueEnumerator = startValues.GetEnumerator();
95      IEnumerator<IEnumerable<double>> actualContinuationsEnumerator = actualContinuations.GetEnumerator();
96      IEnumerator<IEnumerable<double>> predictedContinuationsEnumerator = predictedContinuations.GetEnumerator();
97      OnlineWeightedDirectionalSymmetryCalculator calculator = new OnlineWeightedDirectionalSymmetryCalculator();
98
99      // always move forward all enumerators (do not use short-circuit evaluation!)
100      while (startValueEnumerator.MoveNext() & actualContinuationsEnumerator.MoveNext() & predictedContinuationsEnumerator.MoveNext()) {
101        calculator.Add(startValueEnumerator.Current, actualContinuationsEnumerator.Current, predictedContinuationsEnumerator.Current);
102        if (calculator.ErrorState != OnlineCalculatorError.None) break;
103      }
104
105      // check if all enumerators are at the end to make sure both enumerations have the same length
106      if (calculator.ErrorState == OnlineCalculatorError.None &&
107          (startValueEnumerator.MoveNext() || actualContinuationsEnumerator.MoveNext() || predictedContinuationsEnumerator.MoveNext())) {
108        throw new ArgumentException("Number of elements in startValues, actualContinuations and estimatedValues predictedContinuations doesn't match.");
109      } else {
110        errorState = calculator.ErrorState;
111        return calculator.WeightedDirectionalSymmetry;
112      }
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.