[6802] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 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 |
|
---|
| 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 OnlineWeightedDirectionalSymmetryCalculator : IOnlineTimeSeriesCalculator {
|
---|
[6802] | 29 | private double prevEstimated;
|
---|
| 30 | private double prevOriginal;
|
---|
| 31 | private int n;
|
---|
| 32 | private double correctSum;
|
---|
| 33 | private double incorrectSum;
|
---|
| 34 |
|
---|
| 35 | public double WeightedDirectionalSymmetry {
|
---|
| 36 | get {
|
---|
| 37 | if (n <= 1) return 0.0;
|
---|
| 38 | return incorrectSum / correctSum;
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public OnlineWeightedDirectionalSymmetryCalculator() {
|
---|
| 43 | Reset();
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public double Value {
|
---|
| 47 | get { return WeightedDirectionalSymmetry; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | private OnlineCalculatorError errorState;
|
---|
| 51 | public OnlineCalculatorError ErrorState {
|
---|
| 52 | get { return errorState; }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[7099] | 55 | public void Add(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation) {
|
---|
| 56 | if (double.IsNaN(startValue) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
|
---|
[6802] | 57 | errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
| 58 | } else {
|
---|
[7099] | 59 | var actualEnumerator = actualContinuation.GetEnumerator();
|
---|
| 60 | var predictedEnumerator = predictedContinuation.GetEnumerator();
|
---|
| 61 | while (actualEnumerator.MoveNext() & predictedEnumerator.MoveNext() & errorState != OnlineCalculatorError.InvalidValueAdded) {
|
---|
| 62 | double actual = actualEnumerator.Current;
|
---|
| 63 | double predicted = predictedEnumerator.Current;
|
---|
| 64 | if (double.IsNaN(actual) || double.IsNaN(predicted)) {
|
---|
| 65 | errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
| 66 | } else {
|
---|
| 67 | double err = Math.Abs(actual - predicted);
|
---|
| 68 | // count as correct only if the trend (positive/negative/no change) is predicted correctly
|
---|
| 69 | if ((actual - startValue) * (predicted - startValue) > 0.0 ||
|
---|
| 70 | (actual - startValue).IsAlmost(predicted - startValue)) {
|
---|
| 71 | correctSum += err;
|
---|
| 72 | } else {
|
---|
| 73 | incorrectSum += err;
|
---|
| 74 | }
|
---|
| 75 | n++;
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | // check if both enumerators are at the end to make sure both enumerations have the same length
|
---|
| 79 | if (actualEnumerator.MoveNext() || predictedEnumerator.MoveNext()) {
|
---|
| 80 | errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
[6802] | 81 | } else {
|
---|
[7099] | 82 | errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
|
---|
[6802] | 83 | }
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | public void Reset() {
|
---|
| 88 | n = 0;
|
---|
| 89 | correctSum = 0;
|
---|
| 90 | incorrectSum = 0;
|
---|
| 91 | prevOriginal = double.NaN;
|
---|
| 92 | prevEstimated = double.NaN;
|
---|
| 93 | errorState = OnlineCalculatorError.InsufficientElementsAdded;
|
---|
| 94 | }
|
---|
| 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();
|
---|
| 101 | OnlineWeightedDirectionalSymmetryCalculator calculator = new OnlineWeightedDirectionalSymmetryCalculator();
|
---|
[6802] | 102 |
|
---|
[7099] | 103 | // always move forward all enumerators (do not use short-circuit evaluation!)
|
---|
| 104 | while (startValueEnumerator.MoveNext() & actualContinuationsEnumerator.MoveNext() & predictedContinuationsEnumerator.MoveNext()) {
|
---|
| 105 | calculator.Add(startValueEnumerator.Current, actualContinuationsEnumerator.Current, predictedContinuationsEnumerator.Current);
|
---|
| 106 | if (calculator.ErrorState != OnlineCalculatorError.None) break;
|
---|
[6802] | 107 | }
|
---|
| 108 |
|
---|
[7099] | 109 | // check if all enumerators are at the end to make sure both enumerations have the same length
|
---|
| 110 | if (calculator.ErrorState == OnlineCalculatorError.None &&
|
---|
| 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 {
|
---|
[7099] | 114 | errorState = calculator.ErrorState;
|
---|
| 115 | return calculator.WeightedDirectionalSymmetry;
|
---|
[6802] | 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | }
|
---|