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