Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

164. Bootstrap confidence intervals

medium
GeneralGeneral
senior

Bootstrap confidence intervals help you quantify uncertainty around an ML evaluation metric, like accuracy or MAE, by repeatedly resampling your dataset and recomputing the metric. In this problem, you’ll implement a simple bootstrap procedure to return a two-sided confidence interval for a metric computed on y_true and y_pred.

Requirements

Implement the function

python

The bootstrap CI is based on the percentile method:

CI1−α=[quantileα/2({m(b)}), quantile1−α/2({m(b)})]\text{CI}_{1-\alpha} = \left[ \text{quantile}_{\alpha/2}(\{m^{(b)}\}),\ \text{quantile}_{1-\alpha/2}(\{m^{(b)}\}) \right]

where (m^{(b)}) is the metric computed on bootstrap resample (b).

Rules:

  • For each bootstrap iteration, sample indices with replacement from 0..n-1, compute the metric on the resampled pairs, and store it.
  • Support metric="accuracy" (treat values >= 0.5 in y_pred as class 1, else 0) and metric="mae" (mean absolute error).
  • Use np.random.default_rng(seed) for reproducible sampling.
  • Return the two bounds as a Python tuple (lower, upper).
  • Use only NumPy and Python built-in libraries (no sklearn/scipy).

Example

python

Output:

python
Input Signature
ArgumentType
seedint
alphafloat
metricstr
y_prednp.ndarray
y_truenp.ndarray
n_bootstrapint
Output Signature
Return NameType
valuetuple

Constraints

  • Use only NumPy; no sklearn/scipy.

  • Use np.random.default_rng(seed) for sampling.

  • Return Python list [lower, upper].

Hint 1

Convert y_true/y_pred to NumPy arrays and create an RNG with np.random.default_rng(seed) so resampling is reproducible.

Hint 2

In each bootstrap iteration, sample n indices with replacement via rng.integers(0, n, size=n), then compute the metric on y_true[idx] and y_pred[idx].

Hint 3

Store all bootstrap metric values, then return [np.quantile(vals, alpha/2), np.quantile(vals, 1-alpha/2)] as Python floats. For accuracy, binarize with (y_pred >= 0.5); for mae, use mean(abs(y_true-y_pred)).

Roles
ML Engineer
AI Engineer
Data Scientist
Quantitative Analyst
Companies
GeneralGeneral
Levels
senior
entry
Tags
bootstrap
confidence-interval
numpy
evaluation-metrics
23 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit