Free cookie consent management tool by TermsFeed Policy Generator

source: branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineDirectionalSymmetryCalculator.cs @ 14351

Last change on this file since 14351 was 14330, checked in by gkronber, 8 years ago

#2650 Merged r14282:14322 from trunk to branch (fixing conflicts)

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