[6802] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) 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 {
|
---|
[14376] | 28 | public class OnlineDirectionalSymmetryCalculator : DeepCloneable, 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 |
|
---|
[14465] | 43 | protected OnlineDirectionalSymmetryCalculator(OnlineDirectionalSymmetryCalculator original, Cloner cloner = null)
|
---|
| 44 | : base(original, cloner) {
|
---|
| 45 | n = original.n;
|
---|
| 46 | nCorrect = original.nCorrect;
|
---|
| 47 | errorState = original.errorState;
|
---|
[14293] | 48 | }
|
---|
| 49 |
|
---|
[14465] | 50 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 51 | return new OnlineDirectionalSymmetryCalculator(this, cloner);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[6802] | 54 | public double Value {
|
---|
| 55 | get { return DirectionalSymmetry; }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | private OnlineCalculatorError errorState;
|
---|
| 59 | public OnlineCalculatorError ErrorState {
|
---|
| 60 | get { return errorState; }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[7099] | 63 | public void Add(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation) {
|
---|
| 64 | if (double.IsNaN(startValue) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
|
---|
[6802] | 65 | errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
| 66 | } else {
|
---|
[7099] | 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 | }
|
---|
[6802] | 85 | }
|
---|
[7099] | 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 | }
|
---|
[6802] | 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | public void Reset() {
|
---|
| 96 | n = 0;
|
---|
| 97 | nCorrect = 0;
|
---|
| 98 | errorState = OnlineCalculatorError.InsufficientElementsAdded;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[8468] | 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 | }
|
---|
[14293] | 107 |
|
---|
[7099] | 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();
|
---|
[6802] | 112 | OnlineDirectionalSymmetryCalculator dsCalculator = new OnlineDirectionalSymmetryCalculator();
|
---|
| 113 |
|
---|
[7099] | 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);
|
---|
[6802] | 117 | if (dsCalculator.ErrorState != OnlineCalculatorError.None) break;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[7099] | 120 | // check if all enumerators are at the end to make sure both enumerations have the same length
|
---|
[6802] | 121 | if (dsCalculator.ErrorState == OnlineCalculatorError.None &&
|
---|
[7099] | 122 | (startValueEnumerator.MoveNext() || actualContinuationsEnumerator.MoveNext() || predictedContinuationsEnumerator.MoveNext())) {
|
---|
| 123 | throw new ArgumentException("Number of elements in startValues, actualContinuations and estimatedValues predictedContinuations doesn't match.");
|
---|
[6802] | 124 | } else {
|
---|
| 125 | errorState = dsCalculator.ErrorState;
|
---|
| 126 | return dsCalculator.DirectionalSymmetry;
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 | }
|
---|