Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineWeightedDirectionalSymmetryCalculator.cs @ 6807

Last change on this file since 6807 was 6807, checked in by gkronber, 13 years ago

#1081 added views for time series prognosis

File size: 4.2 KB
RevLine 
[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
22using System;
23using System.Collections.Generic;
24
25
26namespace HeuristicLab.Problems.DataAnalysis {
27  public class OnlineWeightedDirectionalSymmetryCalculator : IOnlineCalculator {
28    private double prevEstimated;
29    private double prevOriginal;
30    private int n;
31    private double correctSum;
32    private double incorrectSum;
33
34    public double WeightedDirectionalSymmetry {
35      get {
36        if (n <= 1) return 0.0;
37        return incorrectSum / correctSum;
38      }
39    }
40
41    public OnlineWeightedDirectionalSymmetryCalculator() {
42      Reset();
43    }
44
45    public double Value {
46      get { return WeightedDirectionalSymmetry; }
47    }
48
49    private OnlineCalculatorError errorState;
50    public OnlineCalculatorError ErrorState {
51      get { return errorState; }
52    }
53
54    public void Add(double original, double estimated) {
55      if (double.IsNaN(estimated) || double.IsInfinity(estimated) || double.IsNaN(original) || double.IsInfinity(original) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
56        errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
57      } else if (n == 0) {
58        prevOriginal = original;
59        prevEstimated = estimated;
60        n++;
61      } else {
62        double err = Math.Abs(original - estimated);
63        if ((original - prevOriginal) * (estimated - prevEstimated) >= 0.0) {
64          correctSum += err;
65        } else {
66          incorrectSum += err;
67        }
68        n++;
69        errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded);        // n >= 1
70        prevOriginal = original;
71        prevEstimated = estimated;
72      }
73    }
74
75    public void Reset() {
76      n = 0;
77      correctSum = 0;
78      incorrectSum = 0;
79      prevOriginal = double.NaN;
80      prevEstimated = double.NaN;
81      errorState = OnlineCalculatorError.InsufficientElementsAdded;
82    }
83
84
85    public static double Calculate(IEnumerable<double> first, IEnumerable<double> second, out OnlineCalculatorError errorState) {
86      IEnumerator<double> firstEnumerator = first.GetEnumerator();
87      IEnumerator<double> secondEnumerator = second.GetEnumerator();
[6807]88      OnlineWeightedDirectionalSymmetryCalculator dsCalculator = new OnlineWeightedDirectionalSymmetryCalculator();
[6802]89     
90      // add first element of time series as a reference point
91      firstEnumerator.MoveNext();
92      secondEnumerator.MoveNext();
93      dsCalculator.Add(firstEnumerator.Current, secondEnumerator.Current);
94
95      // always move forward both enumerators (do not use short-circuit evaluation!)
96      while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
97        double estimated = secondEnumerator.Current;
98        double original = firstEnumerator.Current;
99        dsCalculator.Add(original, estimated);
100        if (dsCalculator.ErrorState != OnlineCalculatorError.None) break;
101      }
102
103      // check if both enumerators are at the end to make sure both enumerations have the same length
104      if (dsCalculator.ErrorState == OnlineCalculatorError.None &&
105          (secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) {
106        throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
107      } else {
108        errorState = dsCalculator.ErrorState;
[6807]109        return dsCalculator.WeightedDirectionalSymmetry;
[6802]110      }
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.