1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
27 | public class OnlineAccuracyCalculator : IOnlineCalculator, IDeepCloneable {
|
---|
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 | // private constructor used internally by the Clone() method
|
---|
42 | protected OnlineAccuracyCalculator(OnlineAccuracyCalculator other, Cloner cloner = null) {
|
---|
43 | correctlyClassified = other.correctlyClassified;
|
---|
44 | n = other.n;
|
---|
45 | errorState = other.errorState;
|
---|
46 | }
|
---|
47 |
|
---|
48 | #region IOnlineCalculator Members
|
---|
49 | private OnlineCalculatorError errorState;
|
---|
50 | public OnlineCalculatorError ErrorState {
|
---|
51 | get { return errorState; }
|
---|
52 | }
|
---|
53 | public double Value {
|
---|
54 | get { return Accuracy; }
|
---|
55 | }
|
---|
56 | public void Reset() {
|
---|
57 | n = 0;
|
---|
58 | correctlyClassified = 0;
|
---|
59 | errorState = OnlineCalculatorError.InsufficientElementsAdded;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public void Add(double original, double estimated) {
|
---|
63 | // ignore cases where original is NaN completly
|
---|
64 | if (!double.IsNaN(original)) {
|
---|
65 | // increment number of observed samples
|
---|
66 | n++;
|
---|
67 | if (original.IsAlmost(estimated)) {
|
---|
68 | // original = estimated = +Inf counts as correctly classified
|
---|
69 | // original = estimated = -Inf counts as correctly classified
|
---|
70 | correctlyClassified++;
|
---|
71 | }
|
---|
72 | errorState = OnlineCalculatorError.None; // number of (non-NaN) samples >= 1
|
---|
73 | }
|
---|
74 | }
|
---|
75 | #endregion
|
---|
76 |
|
---|
77 | public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
|
---|
78 | IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
|
---|
79 | IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
|
---|
80 | OnlineAccuracyCalculator accuracyCalculator = new OnlineAccuracyCalculator();
|
---|
81 |
|
---|
82 | // always move forward both enumerators (do not use short-circuit evaluation!)
|
---|
83 | while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
|
---|
84 | double original = originalEnumerator.Current;
|
---|
85 | double estimated = estimatedEnumerator.Current;
|
---|
86 | accuracyCalculator.Add(original, estimated);
|
---|
87 | if (accuracyCalculator.ErrorState != OnlineCalculatorError.None) break;
|
---|
88 | }
|
---|
89 |
|
---|
90 | // check if both enumerators are at the end to make sure both enumerations have the same length
|
---|
91 | if (accuracyCalculator.ErrorState == OnlineCalculatorError.None &&
|
---|
92 | (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
|
---|
93 | throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
|
---|
94 | } else {
|
---|
95 | errorState = accuracyCalculator.ErrorState;
|
---|
96 | return accuracyCalculator.Accuracy;
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | // IDeepCloneable interface members
|
---|
101 | public object Clone() {
|
---|
102 | return new OnlineAccuracyCalculator(this);
|
---|
103 | }
|
---|
104 |
|
---|
105 | public IDeepCloneable Clone(Cloner cloner) {
|
---|
106 | var clone = cloner.GetClone(this);
|
---|
107 | if (clone == null) {
|
---|
108 | clone = new OnlineAccuracyCalculator(this);
|
---|
109 | cloner.RegisterClonedObject(this, clone);
|
---|
110 | }
|
---|
111 | return clone;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|