FoundationsEvaluation metrics

Evaluation metrics

Background
  • Without the right metric, you cannot tell if a model is actually improving on the task that matters
  • Classification and regression require fundamentally different measurement frameworks
  • Metrics like AUC-ROC separate ranking ability from threshold choices
The Concept

Evaluation metrics are the yardstick for ML models. Classification metrics (accuracy, precision, recall, F1, AUC) measure different aspects of how well a model separates classes. Regression metrics (MAE, RMSE, R-squared) measure prediction distance from truth.

How it Links
  • Directly drives model selection and hyperparameter tuning
  • Connects to the confusion matrix, ROC curves, and calibration
  • Business KPIs map to specific metrics - picking the wrong one leads to deployments that fail in production
Concrete Example
A cancer screening test that flags every patient as positive gets 100% recall but terrible precision. A fraud detector tuned on accuracy alone might ignore the rare fraud cases entirely. The right metric reflects the real cost of each error type.
Learning outcomes
  • Compute precision, recall, F1, accuracy, and AUC by hand from a confusion matrix.
  • Decide between PR-AUC and ROC-AUC based on class imbalance.
  • Pick the right regression metric (MSE, MAE, R^2) for an asymmetric loss.
  • Distinguish discrimination from calibration and explain why both matter.

TL;DR

TL;DR

Choose your metric before you choose your model. Accuracy lies under class imbalance. PR-AUC is more informative than ROC-AUC when positives are rare. Calibration is a separate skill from discrimination and a separate failure mode. For ranking, NDCG and MRR are the defaults.

Intuition

Every metric is a one-dimensional projection of a multidimensional reality. Pick the wrong projection and you will optimise something irrelevant. Three principles cover most cases:

  1. Match the metric to the cost structure. False positives and false negatives rarely have equal cost.
  2. Watch for class imbalance. Anything that ignores the prior class rates will look fine while failing on the minority class.
  3. Discrimination is not calibration. A model can rank perfectly and still output miscalibrated probabilities.

Interactive: confusion matrix and threshold

Move the threshold slider to see precision, recall, F1, and accuracy update live. Notice how a single threshold change can flip you from a high-recall to a high-precision regime.

Confusion matrix at threshold τ
Pred +
Pred −
Actual +
TP 168
FN 32
Actual −
FP 41
TN 159
  • precision = TP/(TP+FP) = 0.804
  • recall = TP/(TP+FN) = 0.840
  • F1 = 2·P·R/(P+R) = 0.822
  • accuracy = (TP+TN)/N = 0.818

Interactive Demo

Explore the ROC curve and precision-recall curve for a simulated binary classifier. Drag the threshold slider to see how the operating point shifts, and observe the precision-recall trade-off in real time.

ROC Curve and PR Curve Demo

Move the threshold slider to see how the operating point shifts on the ROC and PR curves.

TP: 73
FP: 20
TN: 80
FN: 27
Precision
0.785
Recall / TPR
0.730
FPR
0.200
F1
0.756
ROC-AUC:0.838

ROC Curve — FPR vs TPR. The dot marks the current threshold.

What to try: Move the threshold slider to see the precision-recall trade-off in real time. A low threshold catches more positives (high recall, low precision). A high threshold is more precise but misses more. Switch to the PR Curve tab to see how the operating point moves there too.

Classification metrics

Definitions and identities for classification

The confusion matrix

For a binary classifier with threshold tt, every prediction falls into one of four buckets:

Predicted positivePredicted negative
Actual positiveTP (true positive)FN (false negative)
Actual negativeFP (false positive)TN (true negative)

Counting metrics

Accuracy=TP+TNTP+TN+FP+FN,Precision=TPTP+FP,Recall=TPTP+FN.\text{Accuracy} = \frac{TP + TN}{TP + TN + FP + FN}, \qquad \text{Precision} = \frac{TP}{TP + FP}, \qquad \text{Recall} = \frac{TP}{TP + FN}.

Recall is also called sensitivity or true positive rate (TPR). The complementary false positive rate is

FPR=FPFP+TN.\text{FPR} = \frac{FP}{FP + TN}.

F1 and friends

The F1 score is the harmonic mean of precision and recall:

F1=2⋅Precision⋅RecallPrecision+Recall.F_{1} = \frac{2 \cdot \text{Precision} \cdot \text{Recall}}{\text{Precision} + \text{Recall}}.

The generalised FβF_{\beta} weights recall higher than precision when β>1\beta > 1:

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

ROC-AUC and PR-AUC

The ROC curve plots TPR (recall) against FPR as the threshold sweeps over the score; the area under it is ROC-AUC. It equals the probability that a random positive example scores higher than a random negative example.

The PR curve plots precision against recall as the threshold sweeps; the area under it is PR-AUC. It is sensitive to class imbalance in a way ROC-AUC is not [1].

Log-loss (cross-entropy)

For a probabilistic classifier producing p^i\hat{p}_{i} on example ii with true label yi∈{0,1}y_{i} \in \{0, 1\},

L=−1N∑i=1N[yilog⁡p^i+(1−yi)log⁡(1−p^i)].\mathcal{L} = -\frac{1}{N} \sum_{i=1}^{N} \bigl[ y_{i} \log \hat{p}_{i} + (1 - y_{i}) \log (1 - \hat{p}_{i}) \bigr].

This is the training loss for almost every classification model and is also a meaningful evaluation metric: it penalises confident wrong predictions much more harshly than confident correct ones.

Calibration

A classifier is well calibrated if among examples it predicts with probability p^\hat{p}, the empirical positive rate is p^\hat{p}. Calibration is measured by reliability diagrams or Brier score:

Brier=1N∑i(p^i−yi)2.\text{Brier} = \frac{1}{N} \sum_{i} (\hat{p}_{i} - y_{i})^{2}.

Niculescu-Mizil and Caruana [3] showed that boosted trees and SVMs systematically miscalibrate (they push probabilities toward 0 and 1) and recommend Platt scaling or isotonic regression as a post-hoc fix.

Regression metrics

MSE, RMSE, MAE, R^2, and asymmetric losses

Squared and absolute errors

MSE=1N∑i=1N(yi−y^i)2,RMSE=MSE,\text{MSE} = \frac{1}{N} \sum_{i=1}^{N} (y_{i} - \hat{y}_{i})^{2}, \qquad \text{RMSE} = \sqrt{\text{MSE}},MAE=1N∑i=1N∣yi−y^i∣.\text{MAE} = \frac{1}{N} \sum_{i=1}^{N} |y_{i} - \hat{y}_{i}|.

MSE/RMSE penalise large errors quadratically; MAE penalises them linearly. The optimal predictor under MSE is E[y∣x]\mathbb{E}[y \mid x]; under MAE it is the conditional median.

Coefficient of determination

R2=1−∑i(yi−y^i)2∑i(yi−yˉ)2.R^{2} = 1 - \frac{\sum_{i} (y_{i} - \hat{y}_{i})^{2}}{\sum_{i} (y_{i} - \bar{y})^{2}}.

R2R^{2} measures how much of the target variance the model explains relative to the constant predictor yˉ\bar{y}. It can be negative if the model is worse than the mean.

Mean absolute percentage error

MAPE=1N∑i=1N∣yi−y^iyi∣⋅100%.\text{MAPE} = \frac{1}{N} \sum_{i=1}^{N} \left| \frac{y_{i} - \hat{y}_{i}}{y_{i}} \right| \cdot 100\%.

Useful when scale matters across orders of magnitude (forecasting demand in different store sizes), but undefined when yi=0y_{i} = 0 and unstable when yiy_{i} is small.

Asymmetric losses

When the cost of over-prediction differs from under-prediction, use a quantile loss (pinball loss):

Lτ(y,y^)={τ(y−y^)if y≥y^(1−τ)(y^−y)otherwise.\mathcal{L}_{\tau}(y, \hat{y}) = \begin{cases} \tau (y - \hat{y}) & \text{if } y \ge \hat{y} \\ (1 - \tau)(\hat{y} - y) & \text{otherwise} \end{cases}.

Minimising Lτ\mathcal{L}_{\tau} produces the conditional τ\tau-quantile, which is the right answer when over- and under-predictions cost different amounts (inventory, capacity planning, risk pricing).

Ranking metrics

NDCG and MRR for ranked output

Discounted Cumulative Gain (DCG)

Given relevance scores reli\text{rel}_{i} for the top kk ranked items,

DCG@k=∑i=1k2reli−1log⁡2(i+1).\text{DCG}@k = \sum_{i=1}^{k} \frac{2^{\text{rel}_{i}} - 1}{\log_{2}(i + 1)}.

The numerator gives exponentially more weight to highly relevant items; the denominator discounts by rank.

Normalised DCG (NDCG)

NDCG normalises by the ideal ranking:

NDCG@k=DCG@kIDCG@k.\text{NDCG}@k = \frac{\text{DCG}@k}{\text{IDCG}@k}.

NDCG@k lies in [0,1][0, 1] and is the default offline metric for search and recommendation.

Mean Reciprocal Rank (MRR)

For queries each of which has one correct answer at rank rir_{i},

MRR=1Q∑i=1Q1ri.\text{MRR} = \frac{1}{Q} \sum_{i=1}^{Q} \frac{1}{r_{i}}.

MRR is the right metric when there is a single correct answer per query (factoid QA, code completion).

Mean Average Precision (MAP)

For queries with multiple relevant items, MAP=1Q∑iAPi\text{MAP} = \frac{1}{Q} \sum_{i} \text{AP}_{i} where AP\text{AP} is the average of precision at each relevant retrieval. MAP rewards both precision and recall over the full ranking.

When to use what

MetricWhen to useWhen NOT to useTypical pitfall
AccuracyBalanced classes, simple binary or multiclass tasks.Imbalanced classes (class prior < 30%).A constant predictor of the majority class scores the prior. 99% accuracy on a 99% negative dataset is meaningless.
PrecisionWhen false positives are expensive (spam filter, ad fraud).When you need to balance against recall, or as a single number alone.Trivially maxed by predicting positive only on the most certain example.
RecallWhen false negatives are expensive (cancer screening, fraud detection).Alone, without a precision constraint.Trivially maxed by predicting positive on everything.
F1Balanced view of precision and recall on the positive class.Highly imbalanced classes where you care about probability calibration too.F1 is threshold-dependent; report it at a fixed operating point.
ROC-AUCComparing rankers; relatively balanced classes.Highly imbalanced classes (positive prior < 5%).ROC looks great at 0.9 even when PR is at 0.3 because most negatives are easy.
PR-AUCImbalanced classes; positive class is the focus.When you need calibrated probabilities.PR is not threshold-invariant in the same way; baseline depends on prior.
Log-lossProbabilistic classifiers where calibration matters.Hard-classifier outputs (no probabilities).Single confident-but-wrong prediction can dominate the average.
Brier scoreCalibration assessment.Pure ranking comparison.Decomposes into reliability + resolution + uncertainty; report all three for clarity.
MSE / RMSEContinuous targets, symmetric error cost.Heavy-tailed target distributions where outliers should not dominate.A few large errors swamp the average; check residual histograms.
MAEContinuous targets where outliers should not dominate.When you need to optimise the conditional mean.Optimum is the conditional median, not the mean; can mislead if you needed the mean.
R^2Comparing across models on the same target distribution.Comparing across different datasets or transformations of y.Negative R^2 is possible and common when the model is worse than the mean baseline.
NDCGRanked-list outputs (search, recommendation).Single-answer tasks.NDCG@k changes with k; report multiple cutoffs.
MRRSingle correct answer per query (QA, retrieval).Multi-answer or no-correct-answer tasks.Insensitive to anything past the first correct answer.

Three things that lie

Accuracy lies under imbalance

A 99% accurate model on a 99%-negative dataset is the constant “negative” predictor. The metric is hiding all the failure on the minority class. Defaults:

  • For imbalanced binary classification, report PR-AUC, not accuracy.
  • Set the operating threshold by the cost of FP vs FN, not by 0.5 or by accuracy.
  • Report metrics on slices, not just averages.

Hastie, Tibshirani, and Friedman [4] chapter 7 give the canonical statistical treatment.

ROC-AUC lies on rare positives

When the positive class is below 5% of the data, ROC-AUC is dominated by easy negatives. A model can have ROC-AUC = 0.95 and PR-AUC = 0.30 on the same data. PR-AUC is more sensitive because the y-axis (precision) is the rate of positives among the model’s positive predictions, which is what users see [2].

Saito and Rehmsmeier give the canonical worked example: two classifiers identical in ROC-AUC but radically different in PR-AUC, with the difference making one usable in production and the other not.

Calibration is not discrimination

Discrimination measures how well a model ranks positives above negatives. Calibration measures whether the model’s “70%” actually corresponds to 70% empirical positive rate.

These are independent. A boosted tree can have ROC-AUC 0.92 and be wildly miscalibrated [3]. If your downstream system thresholds the probability or sets prices proportional to it, calibration matters as much as discrimination. Fix with Platt scaling (sigmoid transform of scores) or isotonic regression (non-parametric monotonic mapping).

Pop quiz

Check yourself
Score: 0 / 5(0 answered)
  1. 1. A binary classifier on a fraud dataset (1% positive prior) reports ROC-AUC = 0.95 and PR-AUC = 0.30. Which is the more honest assessment?
  2. 2. You optimise a regression model with MAE instead of MSE. The optimal predictor for each x is now:
  3. 3. A classifier outputs probabilities. ROC-AUC is 0.90 but the Brier score is high and the reliability diagram shows the predicted 0.7 corresponds to 0.4 empirical positive rate. What is wrong?
  4. 4. Which metric is appropriate for evaluating a search-ranking model where each query has multiple relevant documents?
  5. 5. You are predicting demand for inventory planning, where over-stocking costs $1 and under-stocking costs $4. Which loss should you use?

Interview Q&A

A team reports their model has 99% accuracy. What questions do you ask?phone

In order:

  1. What is the class prior? A 99% negative dataset gives 99% accuracy to a constant predictor. The first question is whether 99% beats the trivial baseline meaningfully.
  2. What is precision, recall, F1, and PR-AUC on the positive class? Accuracy can hide complete failure on the minority class.
  3. At what threshold? A single accuracy number assumes a fixed cutoff. The threshold should be set by the cost of FP vs FN.
  4. On what data? Held-out, time-based, and sliced by demographic / geography to surface localized failures.
  5. Calibrated probabilities? If the system thresholds, prices, or ranks based on probability, calibration matters as much as accuracy.

A good model report has more than one number; a single 99% should always be challenged.

When would you use PR-AUC instead of ROC-AUC?onsite

When the positive class is rare (typically below 5-10%) and the cost is concentrated on the positive class. The argument is concrete [1][2]:

  • ROC-AUC is the probability that a random positive scores higher than a random negative. With many easy negatives, this number is dominated by easy comparisons and looks high even when the model fails on hard cases.
  • PR-AUC is sensitive to the prior because precision depends on TP / (TP + FP), and the FP count is sensitive to where you set the threshold relative to the negative score distribution.

Saito and Rehmsmeier 2015 show two classifiers with identical ROC curves but PR-AUCs of 0.3 and 0.7 on the same data. In a production fraud or rare-disease setting, one is usable, the other is not.

The general rule: if your audience cares about precision among predicted positives, report PR. If they care about ranking quality across the full distribution, report ROC. In rare-positive settings, prefer PR.

What does it mean for a probabilistic classifier to be calibrated, and how would you fix miscalibration?research

A classifier is perfectly calibrated if for every predicted probability p^\hat{p}, the empirical fraction of positives in examples receiving that prediction equals p^\hat{p}. Mathematically,

P(Y=1∣P^=p)=pfor all p∈[0,1].P(Y = 1 \mid \hat{P} = p) = p \quad \text{for all } p \in [0, 1].

Calibration is measured by reliability diagrams (binned p^\hat{p} vs empirical positive rate) or Brier score (which decomposes into reliability + resolution + uncertainty).

Common causes of miscalibration:

  • Boosted trees push probabilities toward 0 and 1 due to the multiplicative ensemble.
  • SVMs produce decision-function scores, not probabilities; naive normalisation is biased.
  • Class imbalance training tricks (oversampling, class weights) break calibration unless corrected.

Fixes [3]:

  1. Platt scaling. Fit a sigmoid σ(a⋅s+b)\sigma(a \cdot s + b) on the validation set, mapping scores ss to calibrated probabilities. Two parameters; works well for SVMs and small datasets.
  2. Isotonic regression. Fit a non-parametric monotonic mapping from scores to probabilities. More flexible, better for large validation sets, no monotonicity assumption beyond rank-preserving.
  3. Train with a calibration-aware loss. Cross-entropy with no class re-weighting tends to produce calibrated probabilities natively; if you must re-weight, calibrate post hoc.

The general practice: train for discrimination (ROC-AUC, log-loss), calibrate post hoc, evaluate both separately.

Pick a metric for a model that recommends videos to users, and justify the choice.research

Defaults: NDCG@k as the offline ranking metric, paired with calibration on click-through probability if any downstream pricing or filtering uses probabilities.

The reasoning:

  1. Output is a ranked list, not a single label. Accuracy and F1 are wrong category. Ranking metrics are right category.
  2. Relevance is graded, not binary (some videos are partial-matches). NDCG handles graded relevance natively; MAP forces a binary cut.
  3. Position matters with diminishing returns. Users rarely scroll past the first 10 items. NDCG@10 captures both relevance and position.
  4. Calibration matters if the downstream system thresholds probabilities (e.g., to filter out low-confidence recommendations) or uses them for pricing or A/B-test analysis.
  5. Slice metrics by user segment (new vs returning), content category, and country. Global NDCG@10 = 0.5 can hide that a 5% segment is at 0.1.

Online, the offline NDCG should track an A/B test of click-through rate or watch time. If they do not correlate, the offline metric is mis-aligned and needs revisiting.

References

  1. Davis, J. and Goadrich, M. (2006). The Relationship Between Precision-Recall and ROC Curves. ICML 23, 233-240. doi:10.1145/1143844.1143874
  2. Saito, T. and Rehmsmeier, M. (2015). The Precision-Recall Plot Is More Informative than the ROC Plot When Evaluating Binary Classifiers on Imbalanced Datasets. PLoS ONE 10(3), e0118432. doi:10.1371/journal.pone.0118432
  3. Niculescu-Mizil, A. and Caruana, R. (2005). Predicting Good Probabilities With Supervised Learning. ICML 22. doi:10.1145/1102351.1102430
  4. Hastie, T., Tibshirani, R., and Friedman, J. (2009). The Elements of Statistical Learning (2nd ed.). Springer. https://hastie.su.domains/ElemStatLearn/