Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14293 was 14293, checked in by bburlacu, 8 years ago

#2672: Implement online calculator cloning.

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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, IDeepCloneable {
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    // private constructor used internally by the Clone() method
45    private OnlineWeightedDirectionalSymmetryCalculator(OnlineWeightedDirectionalSymmetryCalculator other) {
46      n = other.n;
47      correctSum = other.correctSum;
48      incorrectSum = other.incorrectSum;
49      errorState = other.errorState;
50    }
51
52    public double Value {
53      get { return WeightedDirectionalSymmetry; }
54    }
55
56    private OnlineCalculatorError errorState;
57    public OnlineCalculatorError ErrorState {
58      get { return errorState; }
59    }
60
61    public void Add(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation) {
62      if (double.IsNaN(startValue) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
63        errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
64      } else {
65        var actualEnumerator = actualContinuation.GetEnumerator();
66        var predictedEnumerator = predictedContinuation.GetEnumerator();
67        while (actualEnumerator.MoveNext() & predictedEnumerator.MoveNext() & errorState != OnlineCalculatorError.InvalidValueAdded) {
68          double actual = actualEnumerator.Current;
69          double predicted = predictedEnumerator.Current;
70          if (double.IsNaN(actual) || double.IsNaN(predicted)) {
71            errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
72          } else {
73            double err = Math.Abs(actual - predicted);
74            // count as correct only if the trend (positive/negative/no change) is predicted correctly
75            if ((actual - startValue) * (predicted - startValue) > 0.0 ||
76              (actual - startValue).IsAlmost(predicted - startValue)) {
77              correctSum += err;
78            } else {
79              incorrectSum += err;
80            }
81            n++;
82          }
83        }
84        // check if both enumerators are at the end to make sure both enumerations have the same length
85        if (actualEnumerator.MoveNext() || predictedEnumerator.MoveNext()) {
86          errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
87        } else {
88          errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
89        }
90      }
91    }
92
93    public void Reset() {
94      n = 0;
95      correctSum = 0;
96      incorrectSum = 0;
97      errorState = OnlineCalculatorError.InsufficientElementsAdded;
98    }
99
100    public static double Calculate(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation, out OnlineCalculatorError errorState) {
101      OnlineWeightedDirectionalSymmetryCalculator calculator = new OnlineWeightedDirectionalSymmetryCalculator();
102      calculator.Add(startValue, actualContinuation, predictedContinuation);
103      errorState = calculator.ErrorState;
104      return calculator.WeightedDirectionalSymmetry;
105    }
106
107    public static double Calculate(IEnumerable<double> startValues, IEnumerable<IEnumerable<double>> actualContinuations, IEnumerable<IEnumerable<double>> predictedContinuations, out OnlineCalculatorError errorState) {
108      IEnumerator<double> startValueEnumerator = startValues.GetEnumerator();
109      IEnumerator<IEnumerable<double>> actualContinuationsEnumerator = actualContinuations.GetEnumerator();
110      IEnumerator<IEnumerable<double>> predictedContinuationsEnumerator = predictedContinuations.GetEnumerator();
111      OnlineWeightedDirectionalSymmetryCalculator calculator = new OnlineWeightedDirectionalSymmetryCalculator();
112
113      // always move forward all enumerators (do not use short-circuit evaluation!)
114      while (startValueEnumerator.MoveNext() & actualContinuationsEnumerator.MoveNext() & predictedContinuationsEnumerator.MoveNext()) {
115        calculator.Add(startValueEnumerator.Current, actualContinuationsEnumerator.Current, predictedContinuationsEnumerator.Current);
116        if (calculator.ErrorState != OnlineCalculatorError.None) break;
117      }
118
119      // check if all enumerators are at the end to make sure both enumerations have the same length
120      if (calculator.ErrorState == OnlineCalculatorError.None &&
121          (startValueEnumerator.MoveNext() || actualContinuationsEnumerator.MoveNext() || predictedContinuationsEnumerator.MoveNext())) {
122        throw new ArgumentException("Number of elements in startValues, actualContinuations and estimatedValues predictedContinuations doesn't match.");
123      } else {
124        errorState = calculator.ErrorState;
125        return calculator.WeightedDirectionalSymmetry;
126      }
127    }
128
129    // IDeepCloneable members
130    public object Clone() {
131      return new OnlineWeightedDirectionalSymmetryCalculator(this);
132    }
133
134    public IDeepCloneable Clone(Cloner cloner) {
135      var clone = cloner.GetClone(this);
136      if (clone == null) {
137        clone = (IDeepCloneable)this.Clone();
138        cloner.RegisterClonedObject(this, clone);
139      }
140      return clone;
141    }
142  }
143}
Note: See TracBrowser for help on using the repository browser.