Measuring Performance
Accuracy, precision, recall, ROC curves and choosing what to optimize.
One number often decides which model ships, so choosing that number is a modeling decision in its own right. This lesson builds every common metric from the confusion matrix, shows exactly how each depends on the threshold and the class balance, extends them to many classes and to regression, and ends with the scores that judge probabilities themselves.
What you’ll learn
- Derive accuracy, precision, recall, specificity, the F-scores and the Matthews correlation from a confusion matrix, and express precision through prevalence with Bayes' rule.
- Turn misclassification costs into an expected-cost criterion, and explain how ROC and precision–recall curves summarize all thresholds and how each reacts to prevalence.
- Compute macro, micro and weighted averages for multiclass problems, and the MSE, RMSE, MAE, and MAPE of a regression, knowing which statistic each one rewards.
- Prove that log loss and the Brier score are strictly proper, and separate calibration from discrimination.
- Choose a metric from the decision the model supports.
The confusion matrix and its rates
Take binary labels , with the positive class, and a classifier that compares a score with a threshold . On a test set of examples, the confusion matrix counts true and false positives and negatives, TP, FP, FN and TN, and the prevalence is .
Every common metric normalizes one row of the matrix (a true class), one column (a predicted class), or all of it. That choice decides whether the metric moves when the class balance changes.
| Metric | Formula | Normalizes by | Also called |
|---|---|---|---|
| Accuracy | everything | error rate | |
| Recall | actual positives (row) | sensitivity, true-positive rate (TPR) | |
| Specificity | actual negatives (row) | true-negative rate; FPR specificity | |
| Precision | predicted positives (column) | positive predictive value (PPV) | |
| NPV | predicted negatives (column) | negative predictive value | |
| F1 | ignores TN | harmonic mean of precision and recall | |
| Balanced accuracy | both rows, equally | balanced error rate | |
| MCC | everything | Matthews correlation: Pearson's between and |
Precision is a conditional probability, so Bayes' rule applies, with TPR and FPR :
In odds form the structure is clearer: the posterior odds of a flagged case being positive are the prior odds times the positive likelihood ratio , a property of the classifier alone.
So a classifier with fixed error rates has precision that falls toward zero as . At the prior odds are , and even a likelihood ratio of 18 (recall 0.9, FPR 0.05) leaves posterior odds of : precision 15%.
A screening model has recall 0.9, and 1% of the people screened have the condition. What specificity (in %) does it need for at least half of its positive calls to be correct?
Show the answer
99.1 %
Precision means posterior odds , so the likelihood ratio must reach the inverse prior odds: , i.e. . Specificity must be at least about 99.1%. A specificity of 95%, which sounds excellent, gives precision of only 15% at this prevalence.
Thresholds, costs and F-scores
A scoring model is a whole family of classifiers, one per threshold. If a false negative costs and a false positive , the expected cost per example at threshold is
Error rate is the special case . If the scores are calibrated probabilities, the minimizer is the rule , derived in Logistic Regression & Linear Classifiers; otherwise choose by minimizing the empirical cost on a validation set, never on the test set.
When costs are hard to state, the F-scores are a common compromise. With for precision and for recall,
so weighs missed positives more and false alarms; is the harmonic mean of and , which is dragged toward the smaller of the two. F-scores ignore TN entirely. That suits retrieval, where the negatives are an enormous uninteresting background, but it also means F1 is not an expected cost: its implicit price per error depends on the confusion matrix itself, and its best threshold moves with prevalence. When the scores are calibrated probabilities and F1 is computed from expected counts (a very large test set), that best threshold has a neat form: it is half the best achievable F1 (Lipton, Elkan & Narayanaswamy, 2014).
A model has precision 0.5 and recall 1.0. What is its F1 score?
- 0.75
- about 0.667
- 0.5
- about 0.707
Show the answer
B. about 0.667
. The harmonic mean sits below the arithmetic mean (0.75) and the geometric mean (0.707), closer to the weaker of the two rates, so a classifier cannot buy a high F1 with recall alone.
ROC and precision–recall curves
As falls from to , the point traces the ROC curve from to . A classifier that flags each case with probability , ignoring , sits on the diagonal at , so the diagonal is the chance baseline. The area under the curve, the AUC, has a clean meaning: it is the probability that a randomly drawn positive outscores a randomly drawn negative, with ties counted as one half. On data it is the fraction of positive–negative pairs ranked correctly, which is the Mann–Whitney statistic divided by :
The precision–recall (PR) curve plots precision against recall over the same thresholds. It is the curve to watch when positives are rare and the top of the ranking is what gets acted on, because precision feels the flood of negatives that a small FPR still lets through. Its chance baseline is a horizontal line at precision , and its usual summary, average precision, is over the thresholds in decreasing order.
| Property | ROC curve | Precision–recall curve |
|---|---|---|
| Axes | FPR (x), TPR (y) | recall (x), precision (y) |
| Chance baseline | the diagonal; AUC = 0.5 | precision ; AP |
| Depends on prevalence? | no: both rates condition on the true class | yes, through precision |
| Perfect classifier | passes through ; AUC = 1 | passes through ; AP = 1 |
| Best for | the whole ranking; comparing models across settings | rare positives, where the top of the ranking matters |
| Interpolating between points | a straight line, achievable by randomizing between two thresholds | not a straight line: precision is not linear along that segment |
Think first Two models have the same AUC, 0.90. Must they be equally good for your application?
No. ROC curves can cross: one model may be better at low false-positive rates, which matters for screening a large population where false alarms are expensive, and the other at high recall. AUC averages over every threshold, including thresholds you would never use. Look at the part of the curve where you will operate, or use a partial AUC over that range of FPR; the Convergence lesson ROC, AUC & Precision–Recall in Depth also explains why the AUC has been criticized as a measure of misclassification cost.
Many classes, and numbers instead of classes
With classes the confusion matrix is , and each class gets its own one-vs-rest precision, recall and F1. Three ways to average them answer different questions:
- Macro: the plain mean over classes. Every class counts equally, however rare.
- Weighted: the mean weighted by each class's number of true examples (its support).
- Micro: pool TP, FP and FN over all classes, then compute. In single-label problems each error is one FP (for the predicted class) and one FN (for the true class), so micro-precision, micro-recall and micro-F1 all equal accuracy.
| True class (count) | Predicted A | Predicted B | Predicted C | Precision | Recall | F1 |
|---|---|---|---|---|---|---|
| A (800) | 780 | 15 | 5 | 0.945 | 0.975 | 0.960 |
| B (150) | 20 | 125 | 5 | 0.862 | 0.833 | 0.847 |
| C (50) | 25 | 5 | 20 | 0.667 | 0.400 | 0.500 |
For regression, with errors on test points:
RMSE is in the units of and punishes large errors heavily; MAE is robust to a few wild ones. compares the model with predicting the evaluation set's own mean, so out of sample it can be negative. For example, house prices in thousands predicted as give MSE 420, RMSE 20.5, MAE 18, and MAPE 11.7%, more than half of which comes from the 33% error on the cheapest house.
Suppose you must predict a random target with a single number , as a model must at each input. Squared error splits into a variance and a bias term, so the mean is optimal:
For absolute error, the derivative in is the probability mass below minus the mass above; it vanishes at the median:
For positive , absolute percentage error is absolute error with weight , so its minimizer is the median of the distribution reweighted by , which favors small values: MAPE rewards under-forecasting, and it is undefined whenever . Scaled errors, which divide by the error of a simple benchmark forecast, avoid both problems (Hyndman & Koehler, 2006).
Tomorrow's demand will be either 50 or 150 units, each with probability ½. Which single forecast minimizes the expected MAPE?
Show the answer
50 units
For a forecast between 50 and 150 the expected absolute percentage error is , which grows with , and below 50 it grows again. So the best forecast is 50, with expected MAPE 33%. Squared error would choose the mean, 100; absolute error is indifferent across . A team rewarded on MAPE learns to forecast low.
Scoring probabilities, and choosing the metric
When a model outputs probabilities , judge the probabilities themselves with a scoring rule: the log loss or the Brier score (Brier, 1950), averaged over the test set. A scoring rule is strictly proper if, whenever , its expected value is minimized only by reporting : honesty is the unique best strategy (proper scoring rules; Gneiting & Raftery, 2007).
Brier. Expand around ; the cross term vanishes because :
The first term is zero only at ; the second, , is the irreducible uncertainty of the outcome. Log loss. The expected loss is the binary entropy of plus a Kullback–Leibler divergence, which by Gibbs' inequality is positive unless :
Accuracy is not strictly proper. Scoring a probability by whether matches gives every the same expected score when : reporting 0.51 or 0.99 makes no difference, so accuracy cannot reward good probabilities.
Proper scores measure two things at once. Discrimination is how well the scores separate the classes, which is all the AUC sees. Calibration is whether the stated probabilities match observed frequencies. A model can rank perfectly and be badly calibrated, and when the distortion is monotone, as it usually is, recalibration with a strictly increasing map repairs calibration without touching the ranking.
| The decision the model supports | Metric to report |
|---|---|
| A fixed cut-off with known costs | expected cost at the chosen threshold (error rate only if costs are equal) |
| Act on the top cases of a ranked list (a budget of calls, audits, inspections) | precision at , recall at |
| Threshold chosen later, or by different users; positives not rare | ROC AUC, as a summary of the ranking |
| Rare positives; the top of the ranking matters | the PR curve and average precision |
| Probabilities feed further computation (pricing, triage, expected values) | log loss or Brier score, with a reliability diagram |
| Several classes that matter equally | macro-averaged scores, and per-class tables |
| Regression: cost grows with squared error / absolute error / relative error | (R)MSE / MAE / MAPE only with no zeros and with care, or scaled errors |
Key takeaways
- Every metric normalizes part of the confusion matrix. Recall, specificity and FPR condition on the true class and ignore prevalence; precision and accuracy mix the classes, with precision odds = prior odds × TPR/FPR.
- A score plus a threshold is a classifier. Choose the threshold from costs on validation data, and summarize all thresholds with the ROC curve (prevalence-free) or the PR curve (prevalence-aware, honest about rare positives).
- Micro-averaging over classes gives accuracy; macro-averaging lets rare classes count. In regression, MSE rewards the conditional mean, MAE the median, and MAPE rewards under-forecasting.
- Log loss and the Brier score are strictly proper: they reward honest probabilities and see calibration, which AUC, a pure ranking measure, cannot.
- Pick the metric from the decision the model supports, and do it before looking at the results.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
A classifier for three classes has micro-averaged F1 0.93 and macro-averaged F1 0.71. What does this most likely mean?
- The classifier is well balanced across classes; the two averages always differ by about this much
- It performs poorly on at least one class, most likely a rare one
- Macro-averaging is wrong for multiclass problems
- Its accuracy is 0.71
Show the answer
B. It performs poorly on at least one class, most likely a rare one
Micro-F1 pools every decision, so for single-label problems it equals accuracy and is dominated by the large classes. Macro-F1 averages the per-class F1 scores, so one class with a low F1 pulls it down. A gap this large usually means a small class is handled badly.
A regression model scores on a held-out test set. What does that mean?
- The computation must be wrong: lies between 0 and 1
- The predictions are negatively correlated with the targets
- The model explains 20% of the variance, with the opposite sign
- Its squared error is larger than that of predicting the test set's mean for every example
Show the answer
D. Its squared error is larger than that of predicting the test set's mean for every example
, where SST is the squared error of the constant prediction . Out of sample nothing forces SSE below SST, so simply means the model is worse than that constant. Only in-sample least squares with an intercept guarantees .
You replace a classifier's scores by , a strictly increasing transformation. Which quantity is guaranteed not to change?
- The ROC AUC
- The log loss of the probabilities
- The accuracy at threshold
- The Brier score
Show the answer
A. The ROC AUC
AUC depends only on how the scores order the positives against the negatives, and a strictly increasing map preserves every order. Log loss and Brier score depend on the probability values, and a fixed threshold now cuts the scores at a different place ( means ).
A diagnostic test has the same sensitivity and specificity in two hospitals, but the disease is ten times more common in the first. Which quantity differs between the hospitals?
- The ROC curve
- The recall
- The precision (positive predictive value)
- The false-positive rate
Show the answer
C. The precision (positive predictive value)
Sensitivity (recall), specificity and hence the ROC curve condition on the true class and stay the same. Precision mixes the classes: its odds are the prior odds times , so the hospital with ten times the prevalence (at low prevalence) sees roughly ten times the precision odds.
You swap which class is called "positive" and recompute. Which metric changes?
- F1
- Accuracy
- Balanced accuracy
- The Matthews correlation coefficient
Show the answer
A. F1
Swapping the labels exchanges TP with TN and FP with FN. Accuracy, balanced accuracy and MCC are symmetric under that exchange. F1 ignores TN, so it measures something different after the swap: performance on the other class.
Go deeper
- ROC, AUC & Precision–Recall in Depth · What AUC really measures, and when PR curves tell the truth.
- Calibration & Proper Scoring Rules · When a predicted 80% should mean 80%.
- Comparing Models Statistically · Paired tests, McNemar and honest benchmark comparisons.
Further reading
- T. Fawcett, "An introduction to ROC analysis," Pattern Recognition Letters 27(8), 861–874 (2006).
- T. Saito & M. Rehmsmeier, "The precision-recall plot is more informative than the ROC plot when evaluating binary classifiers on imbalanced datasets," PLOS ONE 10(3), e0118432 (2015).
- R. J. Hyndman & A. B. Koehler, "Another look at measures of forecast accuracy," International Journal of Forecasting 22(4), 679–688 (2006).
- T. Gneiting & A. E. Raftery, "Strictly proper scoring rules, prediction, and estimation," Journal of the American Statistical Association 102(477), 359–378 (2007).