Generating confidence interval for precision recall curve
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
There is no single built-in “confidence interval for the whole precision-recall curve” in the way many people expect. In practice, a useful answer is to bootstrap the evaluation set, recompute the precision-recall curve many times, and then form percentile bands on a shared recall grid.
Why Precision-Recall Curves Need Extra Care
A precision-recall curve is built by sweeping decision thresholds over model scores. Each threshold gives a precision and recall pair, so the curve reflects model ranking quality rather than a single hard prediction rule.
Uncertainty is harder here than for a single scalar metric because the curve is a function, not one number. That means you need to decide what you want confidence intervals for:
- average precision as a single summary metric
- precision at chosen recall levels
- an approximate confidence band for the whole curve
For most applied workflows, bootstrap resampling is the practical approach.
Bootstrap Strategy
The idea is straightforward:
- sample the test set with replacement
- compute the precision-recall curve on that bootstrap sample
- interpolate precision values onto a common recall grid
- repeat many times
- take percentiles across runs
That produces an empirical uncertainty band.
A Runnable Python Example
This example uses scikit-learn and NumPy. It assumes you already have the true labels and the model scores for the positive class.
This gives an approximate 95% confidence band over recall values from 0 to 1.
Why Interpolation Is Needed
Each bootstrap run produces threshold points at different recall locations. You cannot take percentiles point-by-point unless all curves are aligned to the same x-axis values. Interpolating each curve onto a shared recall grid solves that alignment problem.
This is an approximation, but it is usually good enough for model comparison and reporting.
Confidence Interval for Average Precision
If you only need uncertainty for one summary number, bootstrap average precision instead of the full curve. That is often easier to explain in reports.
That gives a confidence interval for a scalar summary rather than a band for the curve.
Common Pitfalls
One common mistake is bootstrapping the training set instead of the held-out evaluation set. The confidence interval should reflect uncertainty in evaluation, not retraining noise, unless you are explicitly studying full pipeline variability.
Another issue is forgetting class balance. Small or highly imbalanced datasets can produce bootstrap samples with only one class, which makes the precision-recall curve undefined or uninformative. Those samples should usually be skipped.
It is also important to remember that the resulting band is an empirical bootstrap approximation, not an exact analytical confidence region.
Summary
- A practical confidence interval for a precision-recall curve is usually built with bootstrap resampling.
- Recompute the curve on each bootstrap sample and interpolate onto a shared recall grid.
- Use percentile bands, such as
2.5%and97.5%, to form an approximate95%interval. - For simpler reporting, bootstrap average precision instead of the full curve.
- Be careful with imbalanced data and bootstrap samples that contain only one class.

