Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2520_PersistenceReintegration/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineDirectionalSymmetryCalculator.cs @ 16453

Last change on this file since 16453 was 16453, checked in by jkarder, 6 years ago

#2520: updated year of copyrights

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 OnlineDirectionalSymmetryCalculator : DeepCloneable, IOnlineTimeSeriesCalculator {
29    private int n;
30    private int nCorrect;
31
32    public double DirectionalSymmetry {
33      get {
34        if (n < 1) return 0.0;
35        return (double)nCorrect / n;
36      }
37    }
38
39    public OnlineDirectionalSymmetryCalculator() {
40      Reset();
41    }
42
43    protected OnlineDirectionalSymmetryCalculator(OnlineDirectionalSymmetryCalculator original, Cloner cloner = null)
44      : base(original, cloner) {
45      n = original.n;
46      nCorrect = original.nCorrect;
47      errorState = original.errorState;
48    }
49
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new OnlineDirectionalSymmetryCalculator(this, cloner);
52    }
53
54    public double Value {
55      get { return DirectionalSymmetry; }
56    }
57
58    private OnlineCalculatorError errorState;
59    public OnlineCalculatorError ErrorState {
60      get { return errorState; }
61    }
62
63    public void Add(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation) {
64      if (double.IsNaN(startValue) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
65        errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
66      } else {
67        var actualEnumerator = actualContinuation.GetEnumerator();
68        var predictedEnumerator = predictedContinuation.GetEnumerator();
69        double prevActual = startValue;
70        double prevPredicted = startValue;
71        while (actualEnumerator.MoveNext() & predictedEnumerator.MoveNext() & errorState != OnlineCalculatorError.InvalidValueAdded) {
72          double actual = actualEnumerator.Current;
73          double predicted = predictedEnumerator.Current;
74          if (double.IsNaN(actual) || double.IsNaN(predicted)) {
75            errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
76          } else {
77            // count a prediction correct if the trend (positive/negative/no change) is predicted correctly
78            if ((actual - prevActual) * (predicted - prevPredicted) > 0.0 ||
79                (actual - prevActual).IsAlmost(predicted - prevPredicted)
80              ) {
81              nCorrect++;
82            }
83            n++;
84          }
85        }
86        // check if both enumerators are at the end to make sure both enumerations have the same length
87        if (actualEnumerator.MoveNext() || predictedEnumerator.MoveNext()) {
88          errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
89        } else {
90          errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
91        }
92      }
93    }
94
95    public void Reset() {
96      n = 0;
97      nCorrect = 0;
98      errorState = OnlineCalculatorError.InsufficientElementsAdded;
99    }
100
101    public static double Calculate(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation, out OnlineCalculatorError errorState) {
102      OnlineDirectionalSymmetryCalculator dsCalculator = new OnlineDirectionalSymmetryCalculator();
103      dsCalculator.Add(startValue, actualContinuation, predictedContinuation);
104      errorState = dsCalculator.ErrorState;
105      return dsCalculator.DirectionalSymmetry;
106    }
107
108    public static double Calculate(IEnumerable<double> startValues, IEnumerable<IEnumerable<double>> actualContinuations, IEnumerable<IEnumerable<double>> predictedContinuations, out OnlineCalculatorError errorState) {
109      IEnumerator<double> startValueEnumerator = startValues.GetEnumerator();
110      IEnumerator<IEnumerable<double>> actualContinuationsEnumerator = actualContinuations.GetEnumerator();
111      IEnumerator<IEnumerable<double>> predictedContinuationsEnumerator = predictedContinuations.GetEnumerator();
112      OnlineDirectionalSymmetryCalculator dsCalculator = new OnlineDirectionalSymmetryCalculator();
113
114      // always move forward all enumerators (do not use short-circuit evaluation!)
115      while (startValueEnumerator.MoveNext() & actualContinuationsEnumerator.MoveNext() & predictedContinuationsEnumerator.MoveNext()) {
116        dsCalculator.Add(startValueEnumerator.Current, actualContinuationsEnumerator.Current, predictedContinuationsEnumerator.Current);
117        if (dsCalculator.ErrorState != OnlineCalculatorError.None) break;
118      }
119
120      // check if all enumerators are at the end to make sure both enumerations have the same length
121      if (dsCalculator.ErrorState == OnlineCalculatorError.None &&
122          (startValueEnumerator.MoveNext() || actualContinuationsEnumerator.MoveNext() || predictedContinuationsEnumerator.MoveNext())) {
123        throw new ArgumentException("Number of elements in startValues, actualContinuations and estimatedValues predictedContinuations doesn't match.");
124      } else {
125        errorState = dsCalculator.ErrorState;
126        return dsCalculator.DirectionalSymmetry;
127      }
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.