<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/a-budget-of-classifier-evaluation-measures/"></div></div><div><a data-url="http://www.r-bloggers.com/a-budget-of-classifier-evaluation-measures/" data-counturl="http://www.r-bloggers.com/a-budget-of-classifier-evaluation-measures/" data-count="horizontal" data-text="A budget of classifier evaluation measures" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2016/07/a-budget-of-classifier-evaluation-measures/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Beginning analysts and data scientists often ask: “how does one remember and master the seemingly endless number of classifier metrics?”</p>
<p>My concrete advice is:</p>
<ul>
<li>Read <a target="_blank" href="https://ninazumel.com/">Nina Zumel</a>’s excellent <a target="_blank" href="http://www.win-vector.com/blog/2009/11/i-dont-think-that-means-what-you-think-it-means-statistics-to-english-translation-part-1-accuracy-measures/">series on scoring classifiers</a>.</li>
<li>Keep notes.</li>
<li>Settle on one or two metrics as you move project to project. We prefer “AUC” early in a project (when you want a flexible score) and “deviance” late in a project (when you want a strict score).</li>
<li>When working on practical problems work with your business partners to find out which of precision/recall, or sensitivity/specificity most match their business needs. If you have time show them and explain the ROC plot and invite them to price and pick points along the ROC curve that most fit their business goals. Finance partners will rapidly recognize the ROC curve as “the efficient frontier” of classifier performance and be very comfortable working with this summary.</li>
</ul>
<p>That being said it always seems like there is a bit of <a target="_blank" href="https://en.wikipedia.org/wiki/Gamesmanship">gamesmanship</a> in that somebody always brings up yet another score, often apparently in the hope you may not have heard of it. Some choice of measure is signaling your pedigree (precision/recall implies a data mining background, sensitivity/specificity a medical science background) and hoping to befuddle others.</p>
<p><center><br>
<img title="Mathmanship.png" alt="Mathmanship" src="http://i0.wp.com/www.win-vector.com/blog/wp-content/uploads/2016/07/Mathmanship.png?resize=291%2C375" border="0" data-recalc-dims="1">
<p>Stanley Wyatt illustration from “Mathmanship” Nicholas Vanserg, 1958, collected in <em>A Stress Analysis of a Strapless evening Gown</em>, Robert A. Baker, Prentice-Hall, 1963</p>
<p></p>
<p>The rest of this note is some help in dealing with this menagerie of common competing classifier evaluation scores.</p>
<p><span></span></p>
<div>
<h2>Definitions</h2>
<p>Lets define our terms. We are going to work with “binary classification” problems. These are problems where we have example instances (also called rows) that are either “in the class” (we will call these instances “true”) or not (and we will call these instances “false”). A classifier is a function that given the description of an instance tries to determine if the instance is in the class or not. The classifier may either return a decision of “positive”/“negative” (indicating the classifier thinks the instance is in or out of the class) or a probability score denoting the estimated probability of being in the class.</p>
</div>
<div>
<h2>Decision or Hard Classifiers</h2>
<p>For decision based (or “hard”) classifiers (those returning only a positive/negative determination) the “confusion matrix” is a <a target="_blank" href="https://en.wikipedia.org/wiki/Sufficient_statistic">sufficient statistic</a> in the sense it contains all of the information summarizing classifier quality. All other classification measures can be derived from it.</p>
<p>For a decision classifier (one that returns “positive” and “negative”, and not probabilities) the classifier’s performance is completely determined by four counts:</p>
<ul>
<li>The True Positive count, this is the number of items that are in the true class that the classifier declares to be positive.</li>
<li>The True Negative count, this is the number of items that in the false class that the classifier declares to be negative.</li>
<li>The False Positive count, this is the number of items that are not in the true class that the classifier declares to be positive.</li>
<li>The False Negative count, this is the number of items in the true class the that classifier declares to be negative.</li>
</ul>
<p>Notice true and false are being used to indicate if the classifier is correct (and not the actual category of each item) in these terms. This is traditional nomenclature. The first two quantities are where the classifier is correct (positive corresponding to true and negative corresponding to false) and the second two quantities count instances where the classifier is incorrect.</p>
<p>It is traditional to arrange these quantities into a 2 by 2 table called the confusion matrix. If we define:</p>
<pre><code>library('ggplot2')
library('caret')</code></pre>
<pre><code>## Loading required package: lattice</code></pre>
<pre><code>library('rSymPy')</code></pre>
<pre><code>## Loading required package: rJython</code></pre>
<pre><code>## Loading required package: rJava</code></pre>
<pre><code>## Loading required package: rjson</code></pre>
<pre><code>A = Var('TruePositives')
B = Var('FalsePositives')
C = Var('FalseNegatives')
D = Var('TrueNegatives')</code></pre>
<p>(Note all code shared <a target="_blank" href="https://github.com/WinVector/ClassifierMetrics">here</a>.)</p>
<p>Then the <a target="_blank" href="http://topepo.github.io/caret/index.html">caret</a> <a target="_blank" href="https://cran.r-project.org/">R package</a> defines the confusion matrix as follows (see <code>help(&quot;confusionMatrix&quot;)</code>) as follows:</p>
<pre><code>          Reference 
Predicted   Event   No Event
     Event  A       B
  No Event  C       D</code></pre>
<p>Reference is “ground truth” or actual outcome. We will call examples that have true ground truth “true examples” (again, please don’t confuse this with “TrueNegatives” which are “false examples” that are correctly scored as being false. We would prefer to have the classifier indicate columns instead of rows, but we will use the caret notation for consistency.</p>
<p>We can encode what we have written about these confusion matrix summaries as algebraic statements. Caret’s <code>help(&quot;confusionMatrix&quot;)</code> then gives us definitions of a number of common classifier scores:</p>
<pre><code># (A+C) and (B+D) are facts about the data, independent of classifier.
Sensitivity = A/(A+C)
Specificity = D/(B+D)
Prevalence = (A+C)/(A+B+C+D)
PPV = (Sensitivity * Prevalence)/((Sensitivity*Prevalence) + ((1-Specificity)*(1-Prevalence)))
NPV = (Specificity * (1-Prevalence))/(((1-Sensitivity)*Prevalence) + ((Specificity)*(1-Prevalence)))
DetectionRate = A/(A+B+C+D)
DetectionPrevalence = (A+B)/(A+B+C+D)
BalancedAccuracy = (Sensitivity+Specificity)/2</code></pre>
<p>We can (from our notes) also define some more common metrics:</p>
<pre><code>TPR = A/(A+C)     # True Positive Rate
FPR = B/(B+D)     # False Positive Rate
FNR = C/(A+C)     # False Negative Rate
TNR = D/(B+D)     # True Negative Rate
Recall = A/(A+C)
Precision = A/(A+B)
Accuracy = (A+D)/(A+B+C+D)</code></pre>
<p>By writing everything down it becomes obvious that<code>Sensitivity==TPR==Recall</code>. That won’t stop somebody from complaining if you say “recall” when they prefer “sensitivity”, but that is how things are.</p>
<p>By declaring all of these quantities as <a target="_blank" href="http://www.sympy.org/en/index.html">sympy</a> variables and expressions we can now check much more. We confirm formal equality of various measures by checking that their difference algebraically simplifies to zero.</p>
<pre><code># Confirm TPR == 1 - FNR
sympy(paste(&quot;simplify(&quot;,TPR-(1-FNR),&quot;)&quot;))</code></pre>
<pre><code>## [1] &quot;0&quot;</code></pre>
<pre><code># Confirm Recall == Sensitivity
sympy(paste(&quot;simplify(&quot;,Recall-Sensitivity,&quot;)&quot;))</code></pre>
<pre><code>## [1] &quot;0&quot;</code></pre>
<pre><code># Confirm PPV == Precision
sympy(paste(&quot;simplify(&quot;,PPV-Precision,&quot;)&quot;))</code></pre>
<pre><code>## [1] &quot;0&quot;</code></pre>
<p>We can also confirm non-identity by simplifying and checking an instance:</p>
<pre><code># Confirm Precision != Specificity
expr &lt;- sympy(paste(&quot;simplify(&quot;,Precision-Specificity,&quot;)&quot;))
print(expr)</code></pre>
<pre><code>## [1] &quot;(FalsePositives*TruePositives - FalsePositives*TrueNegatives)/(FalsePositives*TrueNegatives + FalsePositives*TruePositives + TrueNegatives*TruePositives + FalsePositives**2)&quot;</code></pre>
<pre><code>sub &lt;- function(expr,
                TruePositives,FalsePositives,FalseNegatives,TrueNegatives) {
  eval(expr)
}

sub(parse(text=expr),
    TruePositives=0,FalsePositives=1,FalseNegatives=0,TrueNegatives=1)</code></pre>
<pre><code>## [1] -0.5</code></pre>
</div>
<div>
<h2>More difficult checks</h2>
<div>
<h3>Balanced Accuracy</h3>
<p>We can denote the probability of a true (in-class) instances scoring higher than a false (not in class) instance (with 1/2 point for ties) as <code>Prob[score(true)>score(false)] (with half point on ties)</code>. We can confirm <code>Prob[score(true)>score(false)] (with half point on ties) == BalancedAccuracy</code> for hard or decision classifiers by defining <code>score(true)>score(false)</code> as:</p>
<pre><code>A D : True Positive and True Negative: Correct sorting 1 point
A B : True Positive and False Positive (same prediction &quot;Positive&quot;, different outcomes): 1/2 point
C D : False Negative and True Negative (same prediction &quot;Negative&quot;, different outcomes): 1/2 point
C B : False Negative and True Negative: Wrong order 0 points</code></pre>
<p>Then <code>ScoreTrueGTFalse ==</code>Prob[score(true)>score(false)]` is:</p>
<pre><code>ScoreTrueGTFalse = (1*A*D  + 0.5*A*B + 0.5*C*D + 0*C*B)/((A+C)*(B+D))</code></pre>
<p>Which we can confirm is equal to balanced accuracy.</p>
<pre><code>sympy(paste(&quot;simplify(&quot;,ScoreTrueGTFalse-BalancedAccuracy,&quot;)&quot;))</code></pre>
<pre><code>## [1] &quot;0&quot;</code></pre>
</div>
<div>
<h3>AUC</h3>
<p>We can also confirm <code>Prob[score(true)>score(false)]</code> (with half point on ties) == <code>AUC</code>. We can compute the <code>AUC</code> (the area under the drawn curve) of the above confusion matrix by referring to the following diagram.</p>
<p><center><br>
<img title="AUC.png" alt="AUC" src="http://i2.wp.com/www.win-vector.com/blog/wp-content/uploads/2016/07/AUC.png?resize=599%2C474" border="0" data-recalc-dims="1">
<p>ComputingArea</p>
<p></p>
<p>Then we can check for general equality:</p>
<pre><code>AUC = (1/2)*FPR*TPR + (1/2)*(1-FPR)*(1-TPR) + (1-FPR)*TPR
sympy(paste(&quot;simplify(&quot;,ScoreTrueGTFalse-AUC,&quot;)&quot;))</code></pre>
<pre><code>## [1] &quot;0&quot;</code></pre>
<p>This AUC score (with half point credit on ties) equivalence holds in general (see also <a target="_blank" href="http://www.win-vector.com/blog/2013/01/more-on-rocauc/">More on ROC/AUC</a>, though I got this wrong the first time).</p>

<div>
<h3>F1</h3>
<p>We can show <code>F1</code> is different than Balanced Accuracy by plotting results they differ on:</p>
<pre><code># Wikipedia https://en.wikipedia.org/wiki/F1_score
F1 = 2*Precision*Recall/(Precision+Recall)
F1 = sympy(paste(&quot;simplify(&quot;,F1,&quot;)&quot;))
print(F1)</code></pre>
<pre><code>## [1] &quot;2*TruePositives/(FalseNegatives + FalsePositives + 2*TruePositives)&quot;</code></pre>
<pre><code>print(BalancedAccuracy)</code></pre>
<pre><code>## [1] &quot;TrueNegatives/(2*(FalsePositives + TrueNegatives)) + TruePositives/(2*(FalseNegatives + TruePositives))&quot;</code></pre>
<pre><code># Show F1 and BalancedAccuracy do not always vary together (even for hard classifiers)
F1formula = parse(text=F1)
BAformula = parse(text=BalancedAccuracy)
frm = c()
for(TotTrue in 1:5) {
  for(TotFalse in 1:5) {
    for(TruePositives in 0:TotTrue) {
      for(TrueNegatives in 0:TotFalse) {
        FalsePositives = TotFalse-TrueNegatives
        FalseNegatives = TotTrue-TruePositives
        F1a &lt;- sub(F1formula,
                   TruePositives=TruePositives,FalsePositives=FalsePositives,
                   FalseNegatives=FalseNegatives,TrueNegatives=TrueNegatives)
        BAa &lt;- sub(BAformula,
                   TruePositives=TruePositives,FalsePositives=FalsePositives,
                   FalseNegatives=FalseNegatives,TrueNegatives=TrueNegatives)
        if((F1a&lt;=0)&amp;&amp;(BAa>0.5)) {
          stop()
        }
        fi = data.frame(
          TotTrue=TotTrue,
          TotFalse=TotFalse,
          TruePositives=TruePositives,FalsePositives=FalsePositives,
          FalseNegatives=FalseNegatives,TrueNegatives=TrueNegatives,
          F1=F1a,BalancedAccuracy=BAa,
          stringsAsFactors = FALSE)
        frm = rbind(frm,fi) # bad n^2 accumulation
      }
    }
  }
}

ggplot(data=frm,aes(x=F1,y=BalancedAccuracy)) + 
  geom_point() + 
  ggtitle(&quot;F1 versus balancedAccuarcy/AUC&quot;)</code></pre>
</div>
<p><center><br>
<img title="NewImage.png" alt="NewImage" src="http://i1.wp.com/www.win-vector.com/blog/wp-content/uploads/2016/07/NewImage.png?resize=599%2C428" border="0" data-recalc-dims="1">
<p>
F1 versus BalancedAccuracy/AUC<br>
</center></p>
<div>
<h2>Baroque measures</h2>
<p>In various sciences over the years over 20 measures of “scoring correspondence” have been introduced by playing games with publication priority, symmetry, and incorporating significance (“chance adjustments”) directly into the measure.</p>
<p>Each measure presumably exists because it avoids flaws of all of the others. However the sheer number of them (in my opinion) triggers what I call “De Morgan’s objection”:</p>
<blockquote>
<p>If I had before me a fly and an elephant, having never seen more than one such magnitude of either kind; and if the fly were to endeavor to persuade me that he was larger than the elephant, I might by possibility be placed in a difficulty. The apparently little creature might use such arguments about the effect of distance, and might appeal to such laws of sight and hearing as I, if unlearned in those things, might be unable wholly to reject. But if there were a thousand flies, all buzzing, to appearance, about the great creature; and, to a fly, declaring, each one for himself, that he was bigger than the quadruped; and all giving different and frequently contradictory reasons; and each one despising and opposing the reasons of the others—I should feel quite at my ease. I should certainly say, My little friends, the case of each one of you is destroyed by the rest.</p>
<p>(Augustus De Morgan, “A Budget of Paradoxes” 1872)</p>
</blockquote>
<p>There is actually an excellent literature stream investigating which of these measures are roughly equivalent (say arbitrary monotone functions of each other) and which are different (leave aside which are even useful).</p>
<p>Two excellent guides to this rat hole include:</p>
<ul>
<li>
<p>Ackerman, M., &amp; Ben-David, S. (2008). “Measures of clustering quality: A working set of axioms for clustering.”&quot; Advances in Neural Information Processing Systems: Proceedings of the 2008 Conference.</p>
</li>
<li>
<p>Warrens, M. (2008). “On similarity coefficients for 2× 2 tables and correction for chance.” Psychometrika, 73(3), 487–502.</p>
</li>
</ul>
<p>The point is: you not only can get a publication trying to sort this mess, you can actually do truly interesting work trying to relate these measures.</p>
</div>
<div>
<h2>Further directions</h2>
<p>One can take finding relations and invariants much further as in <a target="_blank" href="https://math.berkeley.edu/~bernd/owl.pdf">“Lectures on Algebraic Statistics” Mathias Drton, Bernd Sturmfels, Seth Sullivant, 2008</a>.</p>
</div>
<div>
<h1>Conclusion</h1>
<p>It is a bit much to hope to only need to know “one best measure” or to claim to be familiar (let alone expert) in all plausible measures. Instead, find a few common evaluation measures that work well and stick with them.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/a-budget-of-classifier-evaluation-measures/"></div></div><div><a data-url="http://www.r-bloggers.com/a-budget-of-classifier-evaluation-measures/" data-counturl="http://www.r-bloggers.com/a-budget-of-classifier-evaluation-measures/" data-count="vertical" data-text="A budget of classifier evaluation measures" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.win-vector.com/blog/2016/07/a-budget-of-classifier-evaluation-measures/"> R – Win-Vector Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Ml5_jzvj4YA"></a></div></div></center></div></div></center>