Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2877_HiveImprovements/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/ClassificationPerformanceMeasuresCalculator.cs @ 15612

Last change on this file since 15612 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 6.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 ClassificationPerformanceMeasuresCalculator : DeepCloneable {
28
29    public ClassificationPerformanceMeasuresCalculator(string positiveClassName, double positiveClassValue) {
30      this.positiveClassName = positiveClassName;
31      this.positiveClassValue = positiveClassValue;
32      Reset();
33    }
34
35    protected ClassificationPerformanceMeasuresCalculator(ClassificationPerformanceMeasuresCalculator original, Cloner cloner)
36      : base(original, cloner) {
37      positiveClassName = original.positiveClassName;
38      positiveClassValue = original.positiveClassValue;
39      truePositiveCount = original.truePositiveCount;
40      falsePositiveCount = original.falsePositiveCount;
41      trueNegativeCount = original.trueNegativeCount;
42      falseNegativeCount = original.falseNegativeCount;
43      errorState = original.errorState;
44    }
45    public override IDeepCloneable Clone(Cloner cloner) {
46      return new ClassificationPerformanceMeasuresCalculator(this, cloner);
47    }
48
49    #region Properties
50    private int truePositiveCount, falsePositiveCount, trueNegativeCount, falseNegativeCount;
51
52    private readonly string positiveClassName;
53    public string PositiveClassName {
54      get { return positiveClassName; }
55    }
56
57    private readonly double positiveClassValue;
58    public double PositiveClassValue {
59      get { return positiveClassValue; }
60    }
61    public double TruePositiveRate {
62      get {
63        double divisor = truePositiveCount + falseNegativeCount;
64        return divisor.IsAlmost(0) ? double.NaN : truePositiveCount / divisor;
65      }
66    }
67    public double TrueNegativeRate {
68      get {
69        double divisor = falsePositiveCount + trueNegativeCount;
70        return divisor.IsAlmost(0) ? double.NaN : trueNegativeCount / divisor;
71      }
72    }
73    public double PositivePredictiveValue {
74      get {
75        double divisor = truePositiveCount + falsePositiveCount;
76        return divisor.IsAlmost(0) ? double.NaN : truePositiveCount / divisor;
77      }
78    }
79    public double NegativePredictiveValue {
80      get {
81        double divisor = trueNegativeCount + falseNegativeCount;
82        return divisor.IsAlmost(0) ? double.NaN : trueNegativeCount / divisor;
83      }
84    }
85    public double FalsePositiveRate {
86      get {
87        double divisor = falsePositiveCount + trueNegativeCount;
88        return divisor.IsAlmost(0) ? double.NaN : falsePositiveCount / divisor;
89      }
90    }
91    public double FalseDiscoveryRate {
92      get {
93        double divisor = falsePositiveCount + truePositiveCount;
94        return divisor.IsAlmost(0) ? double.NaN : falsePositiveCount / divisor;
95      }
96    }
97
98    private OnlineCalculatorError errorState;
99    public OnlineCalculatorError ErrorState {
100      get { return errorState; }
101    }
102    #endregion
103
104    public void Reset() {
105      truePositiveCount = 0;
106      falseNegativeCount = 0;
107      trueNegativeCount = 0;
108      falseNegativeCount = 0;
109      errorState = OnlineCalculatorError.InsufficientElementsAdded;
110    }
111
112    public void Add(double originalClassValue, double estimatedClassValue) {
113      // ignore cases where original is NaN completely
114      if (double.IsNaN(originalClassValue)) return;
115
116      if (originalClassValue.IsAlmost(positiveClassValue)
117            || estimatedClassValue.IsAlmost(positiveClassValue)) { //positive class/positive class estimation
118        if (estimatedClassValue.IsAlmost(originalClassValue)) {
119          truePositiveCount++;
120        } else {
121          if (estimatedClassValue.IsAlmost(positiveClassValue)) //misclassification of the negative class
122            falsePositiveCount++;
123          else //misclassification of the positive class
124            falseNegativeCount++;
125        }
126      } else { //negative class/negative class estimation
127        //In a multiclass classification all misclassifications of the negative class
128        //will be treated as true negatives except on positive class estimations
129        trueNegativeCount++;
130      }
131
132      errorState = OnlineCalculatorError.None; // number of (non-NaN) samples >= 1
133    }
134
135    public void Calculate(IEnumerable<double> originalClassValues, IEnumerable<double> estimatedClassValues) {
136      IEnumerator<double> originalEnumerator = originalClassValues.GetEnumerator();
137      IEnumerator<double> estimatedEnumerator = estimatedClassValues.GetEnumerator();
138
139      // always move forward both enumerators (do not use short-circuit evaluation!)
140      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
141        double original = originalEnumerator.Current;
142        double estimated = estimatedEnumerator.Current;
143        Add(original, estimated);
144        if (ErrorState != OnlineCalculatorError.None) break;
145      }
146
147      // check if both enumerators are at the end to make sure both enumerations have the same length
148      if (ErrorState == OnlineCalculatorError.None && (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
149        throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
150      }
151      errorState = ErrorState;
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.