Classification Metrics

A formal treatment of evaluation metrics for classification: precision, recall, F-scores, ROC curves, AUC, calibration, and the mathematical relationships between confusion matrix quantities and probabilistic thresholds.

Intermediate

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. The Confusion Matrix
  5. Precision, Recall, and F-Score
  6. The Precision-Recall Tradeoff
  7. ROC Curve and AUC
  8. Calibration
  9. Multi-Class Extensions
  10. Proper Scoring Rules
  11. Common Pitfalls
  12. Summary
  13. Exercises

Learning Objectives

  1. Define and compute all confusion matrix-derived metrics.
  2. Derive the relationship between classification threshold and precision/recall.
  3. Prove that AUC equals the probability a random positive ranks above a random negative.
  4. Define calibration and explain reliability diagrams.
  5. Distinguish proper from improper scoring rules.
  6. Choose appropriate metrics for different problem settings (imbalanced classes, ranking, probability estimation).

Notation

  • TP,FP,TN,FN\text{TP}, \text{FP}, \text{TN}, \text{FN} — true/false positives/negatives
  • P=TP+FNP = \text{TP} + \text{FN} — total positives
  • N=TN+FPN = \text{TN} + \text{FP} — total negatives
  • p^(x)[0,1]\hat{p}(x) \in [0,1] — predicted probability of positive class
  • τ[0,1]\tau \in [0,1] — classification threshold

Core Intuition

Accuracy alone is misleading for imbalanced datasets. A spam filter that labels everything "not spam" achieves 99% accuracy if only 1% of emails are spam, yet catches nothing. We need metrics that separately measure performance on positive and negative classes, and that evaluate the quality of probability estimates, not just hard decisions.

Confusion Matrix & Threshold

PredictedActualPositiveNegative+TP10Pred +, Actual +FP0Pred +, Actual −FN0Pred −, Actual +TN10Pred −, Actual −
Precision
1.000
Recall
1.000
F1
1.000
Accuracy
1.000
Threshold
0.50
Explore: Lower threshold → more positives (higher recall, more FP). Higher threshold → fewer positives (higher precision, more FN).

The Confusion Matrix

For a binary classifier with threshold τ\tau (predict positive if p^(x)τ\hat{p}(x) \geq \tau):

Predicted +Predicted -Actual +TPFNActual -FPTN(1)\begin{array}{c|cc} & \text{Predicted +} & \text{Predicted -} \\ \hline \text{Actual +} & \text{TP} & \text{FN} \\ \text{Actual -} & \text{FP} & \text{TN} \end{array} \tag{1}

Precision, Recall, and F-Score

Precision (positive predictive value):

Precision=TPTP+FP=P(Y=1Y^=1).(2)\text{Precision} = \frac{\text{TP}}{\text{TP} + \text{FP}} = P(Y = 1 \mid \hat{Y} = 1). \tag{2}

Recall (sensitivity, true positive rate):

Recall=TPTP+FN=P(Y^=1Y=1).(3)\text{Recall} = \frac{\text{TP}}{\text{TP} + \text{FN}} = P(\hat{Y} = 1 \mid Y = 1). \tag{3}

Fβ_\beta-Score (harmonic mean weighted by β\beta):

Fβ=(1+β2)PrecisionRecallβ2Precision+Recall.(4)F_\beta = (1 + \beta^2)\frac{\text{Precision} \cdot \text{Recall}}{\beta^2\cdot\text{Precision} + \text{Recall}}. \tag{4}

F1F_1 (β=1\beta = 1) weights precision and recall equally. β>1\beta > 1 emphasizes recall; β<1\beta < 1 emphasizes precision.


The Precision-Recall Tradeoff

As threshold τ\tau decreases:

  • More samples classified as positive → TP increases, FP increases.
  • Recall increases (we catch more positives).
  • Precision may decrease (more false positives among predictions).

The precision-recall curve plots precision vs. recall as τ\tau varies from 1 to 0. The area under the PR curve (AP) summarizes performance across all thresholds.


ROC Curve and AUC

The ROC curve plots True Positive Rate vs. False Positive Rate:

TPR(τ)=TP(τ)P,FPR(τ)=FP(τ)N.(5)\text{TPR}(\tau) = \frac{\text{TP}(\tau)}{P}, \qquad \text{FPR}(\tau) = \frac{\text{FP}(\tau)}{N}. \tag{5}

Theorem. The AUC (Area Under ROC Curve) equals the probability that a randomly chosen positive example has a higher predicted score than a randomly chosen negative:

AUC=P(p^(X+)>p^(X)),(6)\text{AUC} = P(\hat{p}(X^+) > \hat{p}(X^-)), \tag{6}

where X+PXY=1X^+ \sim P_{X|Y=1} and XPXY=0X^- \sim P_{X|Y=0}.

Proof sketch. The AUC can be written as 01TPR(FPR1(t))dt\int_0^1 \text{TPR}(\text{FPR}^{-1}(t))\,dt. By a change of variables using the score distributions of positives and negatives, this equals the Wilcoxon-Mann-Whitney U-statistic, which computes pairwise comparison probabilities. \blacksquare

Interpretation: AUC = 0.5 is random guessing; AUC = 1.0 is perfect separation.


Calibration

A classifier is calibrated if:

P(Y=1p^(X)=q)=q,q[0,1].(7)P(Y = 1 \mid \hat{p}(X) = q) = q, \quad \forall q \in [0,1]. \tag{7}

Reliability diagram: Bin predictions by p^\hat{p}, plot observed frequency vs. predicted probability. A perfectly calibrated model lies on the diagonal.

Expected Calibration Error (ECE):

ECE=b=1BSbnacc(Sb)conf(Sb),(8)\text{ECE} = \sum_{b=1}^B \frac{|S_b|}{n}|\text{acc}(S_b) - \text{conf}(S_b)|, \tag{8}

where SbS_b is the set of samples in bin bb, acc\text{acc} is accuracy, and conf\text{conf} is average predicted probability.

Platt scaling: Post-hoc calibration by fitting P(Y=1f(x))=σ(af(x)+b)P(Y=1 \mid f(x)) = \sigma(af(x) + b) on a validation set.


Multi-Class Extensions

For KK classes, metrics are aggregated:

  • Macro-average: Compute metric per class, then average. Treats all classes equally.
  • Micro-average: Aggregate TP, FP, FN across classes, then compute metric. Weighted by class frequency.
  • Weighted average: Average per-class metric weighted by support (class size).

Multi-class AUC: One-vs-rest AUC averaged across classes, or pairwise AUC averaged across all ((K2))(K \choose 2) pairs.


Proper Scoring Rules

Definition. A scoring rule S(p,y)S(p, y) is proper if the expected score is maximized (or minimized, depending on convention) when the predicted distribution equals the true distribution.

  • Log loss (cross-entropy): proper. S(p,y)=ylogp(1y)log(1p)S(p, y) = -y\log p - (1-y)\log(1-p).
  • Brier score: proper. S(p,y)=(py)2S(p, y) = (p - y)^2.
  • Accuracy at threshold 0.5: improper (does not incentivize calibrated probabilities).

Theorem. A proper scoring rule incentivizes honest probability reporting: if the true probability is qq, the forecaster minimizes expected loss by reporting p^=q\hat{p} = q.


Common Pitfalls

Pitfall 1. Using accuracy on imbalanced data. A 1% positive rate means 99% accuracy by always predicting negative. Use PR-AUC or F1 instead.

Pitfall 2. Comparing AUC across different datasets. AUC depends on the negative class distribution; a model with 0.95 AUC on one task isn't necessarily better than 0.90 on another.

Pitfall 3. Optimizing for F1 during training. F1 is not differentiable and threshold-dependent. Train with a proper scoring rule (log loss), then optimize the threshold for F1 post-hoc.


Summary

  • The confusion matrix encodes all binary classification outcomes at a given threshold.
  • Precision and recall measure complementary aspects; F1F_1 balances them.
  • ROC-AUC measures ranking quality; equals the pairwise comparison probability.
  • Calibration measures probability quality; assessed via reliability diagrams and ECE.
  • Proper scoring rules (log loss, Brier) incentivize honest probability estimates.
  • Choose metrics based on the task: ranking → AUC; imbalanced detection → PR-AUC/F1; probability estimation → log loss + calibration.

Exercises

Exercise 1. Prove that F1=2TP2TP+FP+FNF_1 = \frac{2\text{TP}}{2\text{TP} + \text{FP} + \text{FN}}.

Exercise 2. Show that AUC = 0.5 for a random scorer and AUC = 1 for a perfect separator.

Exercise 3. Prove that log loss is a proper scoring rule: the expected log loss EY[Ylogp(1Y)log(1p)]\mathbb{E}_Y[-Y\log p - (1-Y)\log(1-p)] is minimized at p=P(Y=1)p = P(Y=1).

Exercise 4. For a dataset with 100 positives and 10000 negatives, compute the precision of a classifier with TPR = 0.9 and FPR = 0.01.

Exercise 5. Derive the Brier score decomposition: BS=reliabilityresolution+uncertainty\text{BS} = \text{reliability} - \text{resolution} + \text{uncertainty}.