Unit 7 · ML & applied AI · Lesson 4 of 13
Metrics, calibration, thresholds and uncertainty
Step 1: Learn the ideas
- Confusion matrix, precision, recall, F1 and F-beta
- ROC-AUC versus PR-AUC (average precision) under class imbalance
- Regression metrics: RMSE, MAE and MAPE pitfalls
- Ranking metrics: recall@k, precision@k, MRR, NDCG
- Proper scoring rules: log loss and Brier score
- Calibration: reliability diagrams, ECE, temperature, Platt and isotonic scaling
- Cost-based and capacity-based threshold selection
- Uncertainty: intervals for metrics, abstention and review
Why it matters
A metric is a claim about what matters. Fraud detection, search ranking and medical triage need different metrics, and the same model can look excellent or useless depending on the choice. Interviews test whether you can pick the metric from the decision, explain a surprising number, and turn scores into actions with thresholds tied to costs.
Core ideas
Classification. From the confusion matrix: precision TP / (TP + FP) asks how many flagged items are real; recall TP / (TP + FN) asks how many real items were caught. F1 is their harmonic mean, and F-beta weights recall beta times as heavily as precision. Accuracy is dominated by the majority class: with 1% positives, always predicting "negative" scores 99%.
ROC-AUC versus PR-AUC. The ROC curve plots true-positive rate against false-positive rate, and its AUC equals the probability that a random positive is scored above a random negative. Because the false-positive rate divides by the number of negatives, which is huge under imbalance, a model can raise thousands of false alarms while its FPR stays tiny and ROC-AUC looks excellent. The precision-recall curve exposes those false alarms, and its baseline is the positive rate rather than 0.5. When positives are rare and they are what you act on, report average precision (PR-AUC) and precision at the operating point. ROC-AUC remains useful for comparing rankers independently of prevalence. Neither measures calibration.
Regression. RMSE squares errors, so large misses dominate it, and it is minimized by predicting the mean; MAE is linear in the error and minimized by the median. Choose by the cost of big errors. MAPE explodes near zero targets and treats over- and under-prediction asymmetrically.
Ranking. For each query's ranked results:
- recall@k: the fraction of relevant items that appear in the top k. In RAG it is the first retrieval metric to check, because the generator only sees the top k.
- MRR: the mean over queries of
1 / rank of the first relevant item; good when one correct result suffices. - NDCG@k:
DCG = sum over positions i of rel_i / log2(i + 1), divided by the DCG of the ideal ordering; it handles graded relevance and rewards placing the best items first.
Calibration. A classifier is calibrated if, among predictions of 0.8, about 80% are positive. A reliability diagram bins predictions and plots observed frequency against mean confidence; expected calibration error (ECE) summarizes the gaps but depends on the binning and can hide problems. Log loss and Brier score are proper scoring rules: they reward both ranking quality and calibration. Modern deep networks are often overconfident. Temperature scaling divides the logits by one scalar T fitted to minimize log loss on held-out data; it changes confidence without changing the argmax, so accuracy is untouched. Platt scaling (a logistic fit on scores) and isotonic regression (monotone, needs more data) are alternatives. Calibration holds only for the distribution it was fitted on; a change in class prior breaks it.
Thresholds from costs. With calibrated probability p, a false-positive cost C_FP and a false-negative cost C_FN, flag when p * C_FN > (1 - p) * C_FP, that is when p > C_FP / (C_FP + C_FN). If a false alarm costs one unit and a miss costs 50, the threshold is about 0.02, not 0.5. Without calibrated probabilities, choose the threshold on validation data to meet a constraint, such as precision of at least 90% or a review team's capacity per day.
Uncertainty. Report metrics with intervals (bootstrap over examples, or over queries for ranking). For individual predictions, let the system abstain or route to human review when confidence is low; ensembles and conformal prediction give more principled uncertainty.
Common misconceptions
- "High ROC-AUC means the model is usable." Under imbalance, precision at the recall you need can still be poor; check the PR curve at the operating point.
- "A score of 0.9 means a 90% chance." Only if the model is calibrated on this distribution.
- "Temperature scaling improves accuracy." It cannot change the argmax; it fixes confidence only.
- "F1 is the best default metric." It ignores true negatives and fixes the precision-recall trade at equal weight; real costs rarely agree.
- "Calibrate on the training set." Fit calibration on held-out data, or it learns the overconfident training behavior.
- "NDCG and recall@k measure the same thing." Recall@k ignores order within the top k; NDCG rewards ordering and graded relevance.
How to approach it
For any scenario ask: what action follows a prediction, what do errors cost, how rare is the positive class, and are probabilities consumed downstream? Derive the metric from the answers. Practice ranking metrics by hand, plot a reliability diagram for a real classifier, and fit a temperature. Senior-level discussions add prevalence shift and metric uncertainty.
Suggested order
- 1Read the Google Machine Learning Crash Course pages on thresholds and the confusion matrix, on accuracy, precision and recall, and on ROC and AUC as a fast refresher.
- 2Read the scikit-learn "Metrics and scoring" guide sections on classification metrics (precision-recall, ROC, average precision) and regression metrics.
- 3Read "Evaluation of ranked retrieval results" in Introduction to Information Retrieval (Ch. 8), then compute recall@k, MRR and NDCG by hand for two toy rankings.
- 4Read Guo et al., "On Calibration of Modern Neural Networks", for the definitions of reliability diagrams and expected calibration error and for temperature scaling; skim the experiments.
- 5Read the scikit-learn guides on probability calibration and on tuning the decision threshold for class prediction.
- 6Do the calibration and threshold exercise in the practice guidance.
Step 2: Check off what you can do
Tap each one you can genuinely do or explain. Each earns 10 XP.
Step 3: Go deeper
scikit-learn User GuideStart here
scikit-learn documentationSection: Metrics and scoring: quantifying the quality of predictions
Precise definitions and caveats for classification, regression and ranking metrics, including how average precision is computed.
Open(opens in a new tab)Link responded, 5 Oct 2026On Calibration of Modern Neural NetworksStart here
arXivReliability diagrams, expected calibration error and temperature scaling for deep classifiers.
Open(opens in a new tab)Link responded, 5 Oct 2026Machine Learning Crash Course
Google for DevelopersSection: Classification: ROC and AUC
Visual refresher on ROC curves and AUC; continue with the thresholds and precision-recall pages of the same module.
Open(opens in a new tab)Link responded, 5 Oct 2026Introduction to Information Retrieval
Cambridge University Press (free online edition hosted by the Stanford NLP Group)Section: Evaluation of ranked retrieval results (Ch. 8)
Ranked-retrieval metrics such as precision at k, mean average precision and NDCG, with the reasoning behind each.
Open(opens in a new tab)Link responded, 5 Oct 2026
Step 4: Practice
(1) Train a classifier on an imbalanced dataset with a 1 to 5% positive rate; plot ROC and PR curves and explain why they tell different stories. (2) Plot a reliability diagram, fit a temperature on a validation split, and compare log loss, Brier score and ECE before and after on the test split. (3) Assign costs to false positives and false negatives, choose the threshold analytically from calibrated probabilities and empirically on validation data, and compare the expected cost on test. (4) For five queries with graded relevance labels, compute recall@5, MRR and NDCG@5 by hand, then check with code. (5) Bootstrap a 95% interval for average precision.
No external problems are linked; follow the guidance above.
Record attempts on each problem’s page (+15 XP, +10 more when solved on your own).
Step 5: Test yourself
Answer each one out loud or on paper before you finish the lesson.
- Question 1With 0.5% positives, why can ROC-AUC be high while precision at 80% recall is low?
- Question 2What does temperature scaling change, and what does it leave untouched?
- Question 3When would you prefer MAE to RMSE?
- Question 4Why is log loss a proper scoring rule while accuracy is not?
- Question 5How do you choose a threshold when the review team can handle 200 cases per day?
- Question 6What does MRR reward that recall@k ignores?
- Question 7A calibrated model is deployed where the positive rate has doubled. What happens to its calibration?
You’re done when you can
- Explain ROC-AUC versus PR-AUC under imbalance with a numeric example in under three minutes.
- Compute NDCG@5 and MRR for a toy ranking by hand without errors.
- Fit temperature scaling and show its effect on a reliability diagram and on log loss.
- Derive the cost-based threshold formula and apply it to a described scenario.
Finish the lesson
Unlocks next: Retrieval, embeddings and retrieval-augmented generation, Evaluating ML and LLM systems


