Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3043-Regression-Instances-For-Scaling/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineAccuracyCalculator.cs

Last change on this file was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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;
24using HeuristicLab.Common;
25
26namespace HeuristicLab.Problems.DataAnalysis {
27  public class OnlineAccuracyCalculator : DeepCloneable, IOnlineCalculator {
28
29    private int correctlyClassified;
30    private int n;
31    public double Accuracy {
32      get {
33        return correctlyClassified / (double)n;
34      }
35    }
36
37    public OnlineAccuracyCalculator() {
38      Reset();
39    }
40
41    protected OnlineAccuracyCalculator(OnlineAccuracyCalculator original, Cloner cloner)
42      : base(original, cloner) {
43      correctlyClassified = original.correctlyClassified;
44      n = original.n;
45      errorState = original.errorState;
46    }
47
48    public override IDeepCloneable Clone(Cloner cloner) {
49      return new OnlineAccuracyCalculator(this, cloner);
50    }
51
52
53    #region IOnlineCalculator Members
54    private OnlineCalculatorError errorState;
55    public OnlineCalculatorError ErrorState {
56      get { return errorState; }
57    }
58    public double Value {
59      get { return Accuracy; }
60    }
61    public void Reset() {
62      n = 0;
63      correctlyClassified = 0;
64      errorState = OnlineCalculatorError.InsufficientElementsAdded;
65    }
66
67    public void Add(double original, double estimated) {
68      // ignore cases where original is NaN completly
69      if (!double.IsNaN(original)) {
70        // increment number of observed samples
71        n++;
72        if (original.IsAlmost(estimated)) {
73          // original = estimated = +Inf counts as correctly classified
74          // original = estimated = -Inf counts as correctly classified
75          correctlyClassified++;
76        }
77        errorState = OnlineCalculatorError.None; // number of (non-NaN) samples >= 1
78      }
79    }
80    #endregion
81
82    public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
83      IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
84      IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
85      OnlineAccuracyCalculator accuracyCalculator = new OnlineAccuracyCalculator();
86
87      // always move forward both enumerators (do not use short-circuit evaluation!)
88      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
89        double original = originalEnumerator.Current;
90        double estimated = estimatedEnumerator.Current;
91        accuracyCalculator.Add(original, estimated);
92        if (accuracyCalculator.ErrorState != OnlineCalculatorError.None) break;
93      }
94
95      // check if both enumerators are at the end to make sure both enumerations have the same length
96      if (accuracyCalculator.ErrorState == OnlineCalculatorError.None &&
97          (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
98        throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
99      } else {
100        errorState = accuracyCalculator.ErrorState;
101        return accuracyCalculator.Accuracy;
102      }
103    }
104
105  }
106}
Note: See TracBrowser for help on using the repository browser.