Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Error estimation and ROC curves

Notes on estimating classifier error, confusion matrix rates, ROC curves, AUC and common evaluation procedures (cross-validation, empirical vs. Bayes error).

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Two or three sentences: where an engineer uses this.

Engineers use error estimation and ROC curves to measure how well a classifier will perform on new data. These tools tell you whether a classifier is reliable, how to choose thresholds, and how to compare classifiers (important in biometric systems, medical diagnosis, fault detection, and many other engineering tasks).

Key ideas

(Write plainly and define every term before using it.)

  • Classifier: a method or function that assigns an input x to one of K classes. We write predicted class as ŷ (read: y-hat).
  • Ground truth / true label: the actual class of an input, written y.
  • Confusion matrix (binary case): a 2×2 table that counts outcomes of a classifier when true labels are known. The four cells are: True Positive (TP), False Positive (FP), False Negative (FN), True Negative (TN). Draw a square with rows = predicted (Positive, Negative) and columns = actual (Positive, Negative). Label top-left TP, top-right FP, bottom-left FN, bottom-right TN.

From the confusion matrix we define rates (binary classification):

  • True Positive Rate (TPR) or Sensitivity or Recall: fraction of actual positives correctly predicted.
  • False Positive Rate (FPR): fraction of actual negatives wrongly predicted positive.
  • Precision: fraction of predicted positives that are actually positive.
  • Accuracy: fraction of all predictions that are correct.
  • Error rate: 1 − Accuracy = fraction of predictions that are wrong.

Exact statements (university style):

  • Given TP, FP, FN, TN (non-negative integers) with P = TP+FN (total actual positives) and N = FP+TN (total actual negatives):

    • TPR = TP / P, provided P > 0.
    • FPR = FP / N, provided N > 0.
    • Precision = TP / (TP + FP), provided TP+FP > 0.
    • Accuracy = (TP + TN) / (P + N).
    • Error rate = 1 − Accuracy = (FP + FN) / (P + N).
  • Receiver Operating Characteristic (ROC) curve: the parametric curve of points (FPR(t), TPR(t)) obtained by varying a decision threshold t on the classifier score. Plot with x-axis = FPR, y-axis = TPR. A perfect classifier reaches (0,1) (zero false positives, full true positives). The diagonal line y = x corresponds to the expected ROC of an uninformative (random) classifier and is independent of class priors. Also know the related Precision–Recall (PR) curve: plot Recall (TPR) on the x-axis versus Precision on the y-axis as the threshold varies; on imbalanced data PR curves are often more informative than ROC, and the no-skill baseline for Precision equals the positive prevalence P/(P+N).

  • Area Under the ROC Curve (AUC): the integral under the ROC curve from FPR = 0 to FPR = 1. It equals the probability that the classifier will rank a randomly chosen positive example higher than a randomly chosen negative example (this is the Mann–Whitney / Wilcoxon interpretation). In finite samples, handle ties explicitly: in the pairwise ranking view, count a tie as 0.5.

  • Empirical error (training error): fraction of mistakes made on the data used to train the classifier. It typically underestimates the error on new data.

  • Generalization (true) error: expected error on new samples drawn from the same source distribution. We estimate this using resampling methods like cross-validation or a held-out test set.

  • k-fold cross-validation: partition data into k roughly-equal folds; for each fold, train on the other k−1 folds and test on the held-out fold; average test errors to estimate generalization error. Assumes data are i.i.d. (independent and identically distributed). For classification, use stratified folds to preserve class proportions. If you tune hyperparameters, use nested cross-validation (inner loop for tuning, outer loop for unbiased error estimation). When samples are grouped or time-ordered (e.g., subjects, sessions, time series), use group-aware or time-series splits instead of i.i.d. k-fold to avoid leakage.

  • Bayes error: the minimal possible classification error achievable if the true class-conditional distributions and priors were known. Formally: at each x choose the class with maximum posterior probability; the Bayes error is the average probability that even this optimal rule misclassifies.

Exact formula and derivations universities ask for (step-by-step):

  1. Show that accuracy = 1 − error rate using definition of counts: Accuracy = (TP+TN)/(P+N), Error = (FP+FN)/(P+N) so sum = 1.
  2. Derive TPR, FPR from confusion matrix definitions (show P>0 and N>0 assumptions).
  3. For ROC/AUC: show that sorting by score and moving threshold produces monotone non-decreasing TPR as FPR increases from 0 to 1. Draw sequence of steps where each threshold that includes one more positive or negative changes TP or FP by 1; for sample-derived ROC, explain ties produce vertical or horizontal steps.
  4. AUC trapezoidal approximation: integrate discrete ROC using trapezoids between successive FPRs (explain formula below).

Diagrams to draw in exams:

  • Confusion matrix: draw a 2×2 table labelled Predicted/Actual, fill example numbers.
  • ROC curve: draw axes with x-axis labelled False Positive Rate (0 to 1) and y-axis labelled True Positive Rate (0 to 1). Plot diagonal line y = x for random guessing and mark classifier points; indicate AUC by shading under the curve. Mark point (0,1) as perfect classifier.

Formulas

(Each formula on its own line in backticks, then symbol meanings and units.)

TPR = TP / P TP = true positives (count, dimensionless), P = TP + FN (count), applies when P > 0.

FPR = FP / N FP = false positives (count), N = FP + TN (count), applies when N > 0.

Precision = TP / (TP + FP) Precision is dimensionless (fraction), applies when TP + FP > 0.

Accuracy = (TP + TN) / (P + N) Accuracy is dimensionless (fraction between 0 and 1), P + N is total samples (count). SE(Accuracy) ≈ sqrt(Accuracy * (1 - Accuracy) / (P + N)) Approximate standard error of Accuracy under i.i.d. Bernoulli assumptions; a simple 95% Wald CI is Accuracy ± 1.96 * SE (prefer Wilson for small samples).

ErrorRate = 1 - Accuracy = (FP + FN) / (P + N) Error rate is dimensionless. Specificity (TNR) = TN / N = 1 - FPR Specificity (True Negative Rate) is dimensionless; N = FP + TN (count). FNR = FN / P = 1 - TPR False Negative Rate is dimensionless; P = TP + FN (count). F1 = 2 * Precision * Recall / (Precision + Recall) Harmonic mean of Precision and Recall; dimensionless; defined when Precision + Recall > 0.

AUC ≈ Σ_i (x_{i+1} - x_i) * (y_{i+1} + y_i) / 2 Where (x_i, y_i) are ROC points ordered by increasing x (FPR). AUC is dimensionless and in [0,1]. This is the trapezoidal rule for discrete ROC.

EmpiricalError = (1 / n) Σ_{i=1..n} I(ŷ_i ≠ y_i) Where I(condition) is indicator (1 if condition true), n number of test samples (count). Dimensionless.

CrossValError = (1 / k) Σ_{fold=1..k} Error_fold Average of fold errors; dimensionless.

BayesError = ∫ [1 - max_c P(c | x)] p(x) dx Where p(x) is the feature density and P(c | x) are posterior probabilities; dimensionless. Assumes continuous x; discrete sum replaces integral when appropriate.

(Bias-variance note for squared loss — state only:) E[(ŷ - y)^2] = Bias^2 + Variance + Noise where each term is expectation over training sets and data-generating noise. Units are squared target units; applies in regression with squared loss and standard assumptions.

Worked examples

Two exam-level examples with numbered steps and units carried (here probabilities are dimensionless fractions).

Example 1 — Confusion matrix and derived rates (numerical): Given: a classifier tested on 300 samples produced counts: TP = 80, FN = 20, FP = 40, TN = 160. (Units: counts) Steps:

  1. Compute totals: P = TP + FN = 80 + 20 = 100; N = FP + TN = 40 + 160 = 200; total = 300.
  2. TPR = TP / P = 80 / 100 = 0.8. Formula: TPR = TP / P → TPR = 80 / 100 = 0.8 (dimensionless).
  3. FPR = FP / N = 40 / 200 = 0.2. Formula: FPR = FP / N → FPR = 40 / 200 = 0.2.
  4. Precision = TP / (TP + FP) = 80 / (80 + 40) = 80 / 120 = 0.6666667 ≈ 0.6667.
  5. Accuracy = (TP + TN) / total = (80 + 160) / 300 = 240 / 300 = 0.8. Error rate = 1 − Accuracy = 0.2. Final answers (bold): TPR = 0.8, FPR = 0.2, Precision ≈ 0.6667, Accuracy = 0.8.

Example 2 — ROC points from scored outputs and AUC (numerical): Data: 10 test samples with scores and labels (score, label): (0.95, P), (0.90, N), (0.85, P), (0.80, P), (0.70, N), (0.60, P), (0.55, N), (0.40, N), (0.30, P), (0.10, N). There are 5 positives and 5 negatives. Steps:

  1. Sort scores descending (already sorted). For thresholds at each unique score, compute TP and FP when we predict positive if score ≥ threshold.
  2. Compute (FPR, TPR) sequence (list these; full derivation in exam): (0.00, 0.00), (0.00, 0.20), (0.20, 0.20), (0.20, 0.40), (0.20, 0.60), (0.40, 0.60), (0.40, 0.80), (0.60, 0.80), (0.80, 0.80), (0.80, 1.00), (1.00, 1.00).
  3. Apply trapezoidal rule to compute AUC. Use formula AUC ≈ Σ (x_{i+1} - x_i)*(y_{i+1} + y_i)/2. Non-zero x-intervals give areas: 0.04, 0.12, 0.16, 0.16, 0.20 (calculation steps as in full answer). Sum = 0.68. Final answer (bold): AUC ≈ 0.68. (Interpretation: the classifier ranks a random positive above a random negative ~68% of the time.)

Common mistakes

  • Confusing precision and recall (precision relates to predicted positives; recall = TPR relates to actual positives).
  • Computing AUC incorrectly by summing vertical heights instead of trapezoidal areas or not ordering by FPR.
  • Not handling ties correctly when building ROC from scores (ties cause vertical/horizontal steps).
  • Reporting training (empirical) error as if it were generalization error without cross-validation or holdout test.

For your exam

Kinds of questions and what full-marks answers include:

  • Definitions: give concise, exact definitions and units (dimensionless) — e.g., define TPR, FPR, ROC, AUC, Bayes error.
  • Short notes: compare metrics (precision vs recall; ROC vs precision-recall curve) and when to use each (imbalanced data → precision-recall often more informative).
  • Derivations: show how Accuracy = 1 − ErrorRate; derive TPR and FPR from confusion matrix counts; derive trapezoidal AUC for discrete ROC points.
  • Numericals: compute confusion matrix rates and AUC from given scores — show all steps, list formulas first, carry units (counts → fractions), final answers bolded.
  • Diagrams: draw confusion matrix and ROC plot; label axes (FPR on x, TPR on y), show diagonal random-guess line, mark points and shade AUC.
  • Short programs (rare): implement cross-validation split and compute average error (show code and sample outputs). For code questions, include edge cases (ties, empty folds).
  • Uncertainty reporting: compute or interpret standard errors/confidence intervals for error rates (binomial model) and for AUC (e.g., DeLong or bootstrap).

A full-marks answer always: states assumptions (i.i.d. data, finite samples), shows formulas used, step-by-step arithmetic, and interprets results.

Quick check

  1. What is the formula for Precision?
  2. On an ROC plot which axis is FPR?
  3. If TP = 30, FP = 10, FN = 20, TN = 40, what is Accuracy?

Answers: Precision = TP / (TP + FP); FPR is on the x-axis; Accuracy = (TP + TN) / (TP+FP+FN+TN) = (30+40)/100 = 0.70.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?