Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14376 was 14376, checked in by bburlacu, 7 years ago

#2672: Updated cloning mechanism to conform to the HL standard.

File size: 6.0 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 : DeepCloneable, 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    protected OnlineWeightedDirectionalSymmetryCalculator(OnlineWeightedDirectionalSymmetryCalculator other, Cloner cloner) {
45      n = other.n;
46      correctSum = other.correctSum;
47      incorrectSum = other.incorrectSum;
48      errorState = other.errorState;
49    }
50
51    public double Value {
52      get { return WeightedDirectionalSymmetry; }
53    }
54
55    private OnlineCalculatorError errorState;
56    public OnlineCalculatorError ErrorState {
57      get { return errorState; }
58    }
59
60    public void Add(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation) {
61      if (double.IsNaN(startValue) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
62        errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
63      } else {
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;
86        } else {
87          errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
88        }
89      }
90    }
91
92    public void Reset() {
93      n = 0;
94      correctSum = 0;
95      incorrectSum = 0;
96      errorState = OnlineCalculatorError.InsufficientElementsAdded;
97    }
98
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    }
105
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();
111
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;
116      }
117
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.");
122      } else {
123        errorState = calculator.ErrorState;
124        return calculator.WeightedDirectionalSymmetry;
125      }
126    }
127
128    public override IDeepCloneable Clone(Cloner cloner) {
129      return new OnlineWeightedDirectionalSymmetryCalculator(this, cloner);
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.