Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/ClassificationPerformanceMeasuresCalculator.cs @ 14294

Last change on this file since 14294 was 14294, checked in by bburlacu, 8 years ago

#2672: Add cloning constructors.

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