Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2672: Add cloning constructors.

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