[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 OnlineWeightedDirectionalSymmetryCalculator : DeepCloneable, IOnlineTimeSeriesCalculator {
|
---|
[6802] | 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 |
|
---|
[14465] | 44 | protected OnlineWeightedDirectionalSymmetryCalculator(OnlineWeightedDirectionalSymmetryCalculator original, Cloner cloner)
|
---|
| 45 | : base(original, cloner) {
|
---|
| 46 | n = original.n;
|
---|
| 47 | correctSum = original.correctSum;
|
---|
| 48 | incorrectSum = original.incorrectSum;
|
---|
| 49 | errorState = original.errorState;
|
---|
[14293] | 50 | }
|
---|
| 51 |
|
---|
[14465] | 52 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 53 | return new OnlineWeightedDirectionalSymmetryCalculator(this, cloner);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[6802] | 56 | public double Value {
|
---|
| 57 | get { return WeightedDirectionalSymmetry; }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | private OnlineCalculatorError errorState;
|
---|
| 61 | public OnlineCalculatorError ErrorState {
|
---|
| 62 | get { return errorState; }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[7099] | 65 | public void Add(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation) {
|
---|
| 66 | if (double.IsNaN(startValue) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
|
---|
[6802] | 67 | errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
| 68 | } else {
|
---|
[7099] | 69 | var actualEnumerator = actualContinuation.GetEnumerator();
|
---|
| 70 | var predictedEnumerator = predictedContinuation.GetEnumerator();
|
---|
| 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 | double err = Math.Abs(actual - predicted);
|
---|
| 78 | // count as correct only if the trend (positive/negative/no change) is predicted correctly
|
---|
| 79 | if ((actual - startValue) * (predicted - startValue) > 0.0 ||
|
---|
| 80 | (actual - startValue).IsAlmost(predicted - startValue)) {
|
---|
| 81 | correctSum += err;
|
---|
| 82 | } else {
|
---|
| 83 | incorrectSum += err;
|
---|
| 84 | }
|
---|
| 85 | n++;
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 | // check if both enumerators are at the end to make sure both enumerations have the same length
|
---|
| 89 | if (actualEnumerator.MoveNext() || predictedEnumerator.MoveNext()) {
|
---|
| 90 | errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
[6802] | 91 | } else {
|
---|
[7099] | 92 | errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
|
---|
[6802] | 93 | }
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | public void Reset() {
|
---|
| 98 | n = 0;
|
---|
| 99 | correctSum = 0;
|
---|
| 100 | incorrectSum = 0;
|
---|
| 101 | errorState = OnlineCalculatorError.InsufficientElementsAdded;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[8468] | 104 | public static double Calculate(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation, out OnlineCalculatorError errorState) {
|
---|
| 105 | OnlineWeightedDirectionalSymmetryCalculator calculator = new OnlineWeightedDirectionalSymmetryCalculator();
|
---|
| 106 | calculator.Add(startValue, actualContinuation, predictedContinuation);
|
---|
| 107 | errorState = calculator.ErrorState;
|
---|
| 108 | return calculator.WeightedDirectionalSymmetry;
|
---|
| 109 | }
|
---|
[6802] | 110 |
|
---|
[7099] | 111 | public static double Calculate(IEnumerable<double> startValues, IEnumerable<IEnumerable<double>> actualContinuations, IEnumerable<IEnumerable<double>> predictedContinuations, out OnlineCalculatorError errorState) {
|
---|
| 112 | IEnumerator<double> startValueEnumerator = startValues.GetEnumerator();
|
---|
| 113 | IEnumerator<IEnumerable<double>> actualContinuationsEnumerator = actualContinuations.GetEnumerator();
|
---|
| 114 | IEnumerator<IEnumerable<double>> predictedContinuationsEnumerator = predictedContinuations.GetEnumerator();
|
---|
| 115 | OnlineWeightedDirectionalSymmetryCalculator calculator = new OnlineWeightedDirectionalSymmetryCalculator();
|
---|
[6802] | 116 |
|
---|
[7099] | 117 | // always move forward all enumerators (do not use short-circuit evaluation!)
|
---|
| 118 | while (startValueEnumerator.MoveNext() & actualContinuationsEnumerator.MoveNext() & predictedContinuationsEnumerator.MoveNext()) {
|
---|
| 119 | calculator.Add(startValueEnumerator.Current, actualContinuationsEnumerator.Current, predictedContinuationsEnumerator.Current);
|
---|
| 120 | if (calculator.ErrorState != OnlineCalculatorError.None) break;
|
---|
[6802] | 121 | }
|
---|
| 122 |
|
---|
[7099] | 123 | // check if all enumerators are at the end to make sure both enumerations have the same length
|
---|
| 124 | if (calculator.ErrorState == OnlineCalculatorError.None &&
|
---|
| 125 | (startValueEnumerator.MoveNext() || actualContinuationsEnumerator.MoveNext() || predictedContinuationsEnumerator.MoveNext())) {
|
---|
| 126 | throw new ArgumentException("Number of elements in startValues, actualContinuations and estimatedValues predictedContinuations doesn't match.");
|
---|
[6802] | 127 | } else {
|
---|
[7099] | 128 | errorState = calculator.ErrorState;
|
---|
| 129 | return calculator.WeightedDirectionalSymmetry;
|
---|
[6802] | 130 | }
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | }
|
---|