1 · Score distributions
2 · Confusion matrix
What to notice right now
3 · Metrics
Highlighted column = current positive class. “Symmetric” = unchanged when you swap the positive class.4 · Threshold-free view: ROC and Precision–Recall
5 · Costs: choosing the threshold for a rare, expensive class
6 · Concepts
The confusion matrix and the choice of positive class
Every prediction falls into one of four cells. TP: positive, correctly flagged. FN: positive, missed. FP: negative, falsely flagged (a false alarm). TN: negative, correctly left alone. All metrics in section 3 are just different ratios of these four numbers.
Choose as positive the class whose detection triggers an action, usually the rare event you are looking for (disease, fraud, brain death). Choosing the positive class is about meaning, not about which error is expensive. Costs are handled separately (section 5). The choice matters because precision, recall and F-scores ignore TN and so describe only the positive class. Accuracy, balanced accuracy, MCC, κ and ROC-AUC are symmetric. Try the A/B switch.
Software pitfall: R's caret::confusionMatrix() and yardstick treat the first factor level as positive by default. scikit-learn uses pos_label=1. Always set it explicitly.
Why accuracy misleads when a class is rare (the accuracy paradox)
With prevalence π, the trivial rule “always predict the majority class” scores accuracy max(π, 1−π). At π = 1% that is 99%, with zero useful detections. A model can therefore look excellent on accuracy and be useless, or be very useful while having lower accuracy than the trivial rule. Use the “maximizes accuracy” button on a rare-class scenario: it barely flags anything.
A second effect, sometimes called the base-rate fallacy: a specificity of 99% sounds excellent, but 1% of a huge negative class can outnumber all the positives. Precision then collapses even though sensitivity and specificity are high:
ROC curve and AUC
Each threshold gives one (FPR, TPR) pair. Sweeping the threshold from +∞ to −∞ traces the ROC curve from (0,0) to (1,1). To build one by hand, sort instances by score in descending order: each positive moves the curve up by 1/P, and each negative moves it right by 1/N. Only the ranking of scores matters.
AUC = probability that a randomly chosen positive gets a higher score than a randomly chosen negative (the Mann–Whitney U statistic). 0.5 = random, 1 = perfect. For this binormal model, AUC = Φ(d′/√2).
Because TPR is computed only from positives and FPR only from negatives, ROC is blind to prevalence. Change the prevalence slider: the ROC curve stays put while the PR curve and precision collapse. For rare classes, look at the low-FPR region (use zoom), at partial AUC, or at the PR curve. AUC measures ranking, not calibration.
Precision–Recall curve and Average Precision
The PR curve plots precision against recall over all thresholds. Unlike ROC, it depends on prevalence. The random-classifier baseline is a horizontal line at precision = π, not 0.5. Average Precision (AP), the area under the step PR curve, should therefore always be read relative to π. When the positive class is rare and false alarms matter, PR is usually more informative than ROC.
Cost-sensitive decisions and the Bayes threshold
Let p = P(A | x) be a calibrated probability. Predict A when the expected cost of doing so is lower:
which gives
With equal error costs, p* = 0.5 (which maximizes accuracy). With CFP = 1000·CFN, p* ≈ 0.999: you flag only when you are almost certain. On the ROC plot, all classifiers with equal expected cost lie on a line with slope
The best operating point is where such a line touches the curve (its convex hull). Prevalence and cost ratio enter only through their product: changing costs is equivalent to changing the class prior (Elkan, “The Foundations of Cost-Sensitive Learning”, IJCAI 2001).
Strategies for rare classes
- Threshold moving (what this lab shows): keep the model, change the decision threshold using p*. It requires calibrated probabilities.
- Cost-sensitive learning / class weights: weight training errors by their cost, e.g.
class_weightin scikit-learn, orweightsorcase.weightsin R. - Resampling (undersampling the majority, oversampling or SMOTE for the minority): this changes the prior seen by the learner, so predicted probabilities are no longer calibrated for the real prevalence. Correct them before applying p*, with the training prevalence πs and the true prevalence π:
p = ps·(π/πs) / [ps·(π/πs) + (1−ps)·((1−π)/(1−πs))]
- Evaluate honestly: resample only the training folds, never the test set. Use stratified cross-validation, and report precision/recall, AP, or TPR at a fixed acceptable FPR, not accuracy.
- Cascades: when false positives are catastrophic (e.g. brain-death determination), require two independent tests to agree. With independence, FPRtotal = FPR1·FPR2, at the cost of TPR1·TPR2.
- Small numbers: with 10 positives, every metric about the positive class is very noisy. Press “New random sample” with n = 1000 and π = 1% and watch precision jump around.
7 · Exercises
- Brain death scenario: press “maximizes accuracy”. How many living patients are declared dead? Compare the accuracy with the trivial rule “always Alive”.
- Set the threshold so that specificity ≈ 99%. Move the prevalence slider from 50% down to 0.5%. What happens to the ROC curve? To the PR curve? To precision? Explain using the base-rate formula.
- Swap the positive class. List the metrics that change and the ones that don’t, and explain why using TN.
- Press “minimizes cost (data)” and “Bayes rule (theory)”. Why are the two thresholds not identical? What happens with n = 100,000?
- Change CFP from 1000 to 10. How do the iso-cost line, p* and the optimal operating point move?
- Cancer screening scenario: here the expensive error is the FN. Is “Cancer” still the positive class? Why?
- With d′ = 0 (useless model), what are AUC, AP and MCC? Which threshold minimizes cost now?