Paired Bootstrap Confidence Interval for a Metric Delta

Quick Overview

Implement a paired bootstrap confidence interval for the difference in a metric between a candidate model and a baseline. It tests why both models must share each resample, reproducible index draws, percentile intervals, the share of positive deltas and careful input validation.

Paired Bootstrap Confidence Interval for a Metric Delta

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `paired_bootstrap_delta(y_true, pred_baseline, pred_candidate, metric, rng, n_boot=1000, alpha=0.05)`. It estimates a percentile confidence interval for `metric(y, candidate) - metric(y, baseline)` by resampling examples jointly for both models. `metric(y, pred)` returns a float, and labels may be numeric or strings. Requirements: - Return a dict: `delta` (full-sample difference), `ci_low`, `ci_high`, `prob_improvement` (Python floats), and `deltas` (ndarray of shape `(n_boot,)`). - Draw `idx = rng.integers(0, n, size=(n_boot, n))` in one call. Row `b` indexes `y` and both predictions for resample `b`. - `ci_low, ci_high = np.quantile(deltas, [alpha/2, 1 - alpha/2])` (default linear method). - `prob_improvement` is the fraction of `deltas` strictly greater than 0. - Raise `ValueError` for mismatched lengths, empty inputs, `n_boot` that is not a positive integer, or `alpha` outside `(0, 1)`. Example, with a constant shift and a metric that ignores the labels: ```python y = np.array([0, 1, 1, 0]) pb = np.array([0.0, 0.25, 0.5, 0.75]) pc = pb + 1.0 metric = lambda y, p: float(np.mean(p)) out = paired_bootstrap_delta(y, pb, pc, metric, np.random.default_rng(5), n_boot=200) # out["delta"] == 1.0 # out["deltas"] == all 1.0, (ci_low, ci_high) == (1.0, 1.0), prob_improvement == 1.0 ``` Every prediction here is an exact binary fraction and `n` is 4, so the means carry no rounding error and every resampled delta is exactly 1.0. ```hint One row, three arrays Each row of the index matrix picks the examples for one resample. Think about which arrays that same row has to index, and keep the inputs as NumPy arrays so string labels index the same way numbers do. ``` ```hint Validation pitfalls Try your checks on `n_boot=None`, `n_boot=2.5` and `alpha=float("nan")`. Some common checks raise `TypeError` or let a bad value through. ``` ### What a Strong Answer Covers - Why one set of indices per resample is shared by the labels and both models, and what that does to the variance of the delta - The single `rng.integers` call with the required shape, made after validation, so a given generator state gives reproducible output - The percentile interval and `prob_improvement` computed exactly as specified, including how zero deltas count - Validation of lengths, empty input, `n_boot` and `alpha` - Time and memory cost, and what happens when the metric is undefined on a resample ### Follow-up Questions - Percentile intervals can be biased. What would you use instead? - How would you handle correlated examples, for example several samples per user? - How would you choose `n_boot`, and how would you check that the interval endpoints are stable?

Overview: Implement a paired bootstrap confidence interval for the difference in a metric between a candidate model and a baseline. It tests why both models must share each resample, reproducible index draws, percentile intervals, the share of positive deltas and careful input validation.

General logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement paired_bootstrap_delta(y_true, pred_baseline, pred_candidate, metric, rng, n_boot=1000, alpha=0.05). It estimates a percentile confidence interval for metric(y, candidate) - metric(y, baseline) by resampling examples jointly for both models. metric(y, pred) returns a float, and labels may be numeric or strings.

Requirements:

  • Return a dict: delta (full-sample difference), ci_low , ci_high , prob_improvement (Python floats), and deltas (ndarray of shape (n_boot,) ).
  • Draw idx = rng.integers(0, n, size=(n_boot, n)) in one call. Row b indexes y and both predictions for resample b .
  • ci_low, ci_high = np.quantile(deltas, [alpha/2, 1 - alpha/2]) (default linear method).
  • prob_improvement is the fraction of deltas strictly greater than 0.
  • Raise ValueError for mismatched lengths, empty inputs, n_boot that is not a positive integer, or alpha outside (0, 1) .

Example, with a constant shift and a metric that ignores the labels:

y = np.array([0, 1, 1, 0])
pb = np.array([0.0, 0.25, 0.5, 0.75])
pc = pb + 1.0
metric = lambda y, p: float(np.mean(p))
out = paired_bootstrap_delta(y, pb, pc, metric, np.random.default_rng(5), n_boot=200)
# out["delta"] == 1.0
# out["deltas"] == all 1.0, (ci_low, ci_high) == (1.0, 1.0), prob_improvement == 1.0

Every prediction here is an exact binary fraction and n is 4, so the means carry no rounding error and every resampled delta is exactly 1.0.

What a Strong Answer Covers Guidance

  • Why one set of indices per resample is shared by the labels and both models, and what that does to the variance of the delta
  • The single rng.integers call with the required shape, made after validation, so a given generator state gives reproducible output
  • The percentile interval and prob_improvement computed exactly as specified, including how zero deltas count
  • Validation of lengths, empty input, n_boot and alpha
  • Time and memory cost, and what happens when the metric is undefined on a resample

Follow-up Questions Guidance

  • Percentile intervals can be biased. What would you use instead?
  • How would you handle correlated examples, for example several samples per user?
  • How would you choose n_boot , and how would you check that the interval endpoints are stable?