[6802] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12009] | 3 | * Copyright (C) 2002-2015 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[7099] | 24 | using HeuristicLab.Common;
|
---|
[6802] | 25 |
|
---|
| 26 |
|
---|
| 27 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
[7099] | 28 | public class OnlineDirectionalSymmetryCalculator : IOnlineTimeSeriesCalculator {
|
---|
[6802] | 29 | private int n;
|
---|
| 30 | private int nCorrect;
|
---|
| 31 |
|
---|
| 32 | public double DirectionalSymmetry {
|
---|
| 33 | get {
|
---|
[7099] | 34 | if (n < 1) return 0.0;
|
---|
| 35 | return (double)nCorrect / n;
|
---|
[6802] | 36 | }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | public OnlineDirectionalSymmetryCalculator() {
|
---|
| 40 | Reset();
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | public double Value {
|
---|
| 44 | get { return DirectionalSymmetry; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | private OnlineCalculatorError errorState;
|
---|
| 48 | public OnlineCalculatorError ErrorState {
|
---|
| 49 | get { return errorState; }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[7099] | 52 | public void Add(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation) {
|
---|
| 53 | if (double.IsNaN(startValue) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
|
---|
[6802] | 54 | errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
| 55 | } else {
|
---|
[7099] | 56 | var actualEnumerator = actualContinuation.GetEnumerator();
|
---|
| 57 | var predictedEnumerator = predictedContinuation.GetEnumerator();
|
---|
| 58 | double prevActual = startValue;
|
---|
| 59 | double prevPredicted = startValue;
|
---|
| 60 | while (actualEnumerator.MoveNext() & predictedEnumerator.MoveNext() & errorState != OnlineCalculatorError.InvalidValueAdded) {
|
---|
| 61 | double actual = actualEnumerator.Current;
|
---|
| 62 | double predicted = predictedEnumerator.Current;
|
---|
| 63 | if (double.IsNaN(actual) || double.IsNaN(predicted)) {
|
---|
| 64 | errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
| 65 | } else {
|
---|
| 66 | // count a prediction correct if the trend (positive/negative/no change) is predicted correctly
|
---|
| 67 | if ((actual - prevActual) * (predicted - prevPredicted) > 0.0 ||
|
---|
| 68 | (actual - prevActual).IsAlmost(predicted - prevPredicted)
|
---|
| 69 | ) {
|
---|
| 70 | nCorrect++;
|
---|
| 71 | }
|
---|
| 72 | n++;
|
---|
| 73 | }
|
---|
[6802] | 74 | }
|
---|
[7099] | 75 | // check if both enumerators are at the end to make sure both enumerations have the same length
|
---|
| 76 | if (actualEnumerator.MoveNext() || predictedEnumerator.MoveNext()) {
|
---|
| 77 | errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
| 78 | } else {
|
---|
| 79 | errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
|
---|
| 80 | }
|
---|
[6802] | 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | public void Reset() {
|
---|
| 85 | n = 0;
|
---|
| 86 | nCorrect = 0;
|
---|
| 87 | errorState = OnlineCalculatorError.InsufficientElementsAdded;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[8468] | 90 | public static double Calculate(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation, out OnlineCalculatorError errorState) {
|
---|
| 91 | OnlineDirectionalSymmetryCalculator dsCalculator = new OnlineDirectionalSymmetryCalculator();
|
---|
| 92 | dsCalculator.Add(startValue, actualContinuation, predictedContinuation);
|
---|
| 93 | errorState = dsCalculator.ErrorState;
|
---|
| 94 | return dsCalculator.DirectionalSymmetry;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[7099] | 97 | public static double Calculate(IEnumerable<double> startValues, IEnumerable<IEnumerable<double>> actualContinuations, IEnumerable<IEnumerable<double>> predictedContinuations, out OnlineCalculatorError errorState) {
|
---|
| 98 | IEnumerator<double> startValueEnumerator = startValues.GetEnumerator();
|
---|
| 99 | IEnumerator<IEnumerable<double>> actualContinuationsEnumerator = actualContinuations.GetEnumerator();
|
---|
| 100 | IEnumerator<IEnumerable<double>> predictedContinuationsEnumerator = predictedContinuations.GetEnumerator();
|
---|
[6802] | 101 | OnlineDirectionalSymmetryCalculator dsCalculator = new OnlineDirectionalSymmetryCalculator();
|
---|
| 102 |
|
---|
[7099] | 103 | // always move forward all enumerators (do not use short-circuit evaluation!)
|
---|
| 104 | while (startValueEnumerator.MoveNext() & actualContinuationsEnumerator.MoveNext() & predictedContinuationsEnumerator.MoveNext()) {
|
---|
| 105 | dsCalculator.Add(startValueEnumerator.Current, actualContinuationsEnumerator.Current, predictedContinuationsEnumerator.Current);
|
---|
[6802] | 106 | if (dsCalculator.ErrorState != OnlineCalculatorError.None) break;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[7099] | 109 | // check if all enumerators are at the end to make sure both enumerations have the same length
|
---|
[6802] | 110 | if (dsCalculator.ErrorState == OnlineCalculatorError.None &&
|
---|
[7099] | 111 | (startValueEnumerator.MoveNext() || actualContinuationsEnumerator.MoveNext() || predictedContinuationsEnumerator.MoveNext())) {
|
---|
| 112 | throw new ArgumentException("Number of elements in startValues, actualContinuations and estimatedValues predictedContinuations doesn't match.");
|
---|
[6802] | 113 | } else {
|
---|
| 114 | errorState = dsCalculator.ErrorState;
|
---|
| 115 | return dsCalculator.DirectionalSymmetry;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | }
|
---|