Lesson 10 of 117 min
Confidence intervals and the bootstrap
To see how far a score could move, draw its own answers again with repeats, many times: the middle 95% of those scores is its error bar.
After this lesson you can put error bars on a measured number.
Loading the video…
Refreshers
An idea from an earlier lesson shows up here when the video leans on it.
In real code
What the video left out, as you will meet it in your own code.
Work out the bar from the score
Run the cells in order. For 160 right answers in 200 independent questions, put 1.96 standard errors on either side of 80% for a quick 95% interval. This normal approximation is easiest to use when there are plenty of both right and wrong answers.
import numpy as np
p, n = 0.8, 200
half = 1.96 * np.sqrt(p * (1 - p) / n)
print(f"{p - half:.1%} to {p + half:.1%}")74.5% to 85.5%
Ask SciPy for the middle 95 percent
The percentile method takes the middle 95% of the bootstrap scores straight from the draws. SciPy defaults to BCa, which adjusts the cutoffs using information about bias and skew. Here the two bars are close.
from scipy import stats
correct = np.r_[np.ones(160), np.zeros(40)]
for options in ({"method": "percentile"}, {}):
result = stats.bootstrap((correct,), np.mean,
rng=np.random.default_rng(0), **options)
lo, hi = result.confidence_interval
print(f"{options.get('method', 'default BCa')}: {lo:.1%} to {hi:.1%}")percentile: 74.5% to 85.5% default BCa: 74.0% to 85.0%
Draw the same questions for both models
Each model gets its own bar. To compare their scores, draw the same question IDs for both and make a bar for B minus A. The paired draw keeps the fact that 150 questions were right for both and 22 wrong for both. See Miller's paired analysis.
a = np.repeat([1, 1, 0, 0], [150, 10, 18, 22])
b = np.repeat([1, 0, 1, 0], [150, 10, 18, 22])
for label, values in (("A", a), ("B", b)):
lo, hi = stats.binom.ppf([0.025, 0.975], n, values.mean()) / n
print(f"{label}'s score: {lo:.1%} to {hi:.1%}")rng = np.random.default_rng(0)
first = rng.integers(0, n, (10_000, n))
second = rng.integers(0, n, (10_000, n))
paired = (b[first] - a[first]).mean(axis=1)
separate = b[second].mean(axis=1) - a[first].mean(axis=1)
for label, gaps in (("same picks", paired), ("separate picks", separate)):
lo, hi = 100 * np.percentile(gaps, [2.5, 97.5])
print(f"{label}: {lo:+.1f} to {hi:+.1f} points")same picks: -1.0 to +9.5 points separate picks: -3.5 to +11.5 points
Check what 95 percent covers
In this simulation the true score is 80%. Each new test gives its own interval, which either holds 80% or misses it. The 95% describes how often the method should catch the true score over repeated tests, with some variation in small runs.
rng = np.random.default_rng(3)
right = rng.binomial(n, 0.8, size=100)
lo = stats.binom.ppf(0.025, n, right / n)
hi = stats.binom.ppf(0.975, n, right / n)
hits = (lo <= 160) & (hi >= 160)
print(f"{hits.sum()} of {len(hits)} bars caught 80%")95 of 100 bars caught 80%
Keep each passage together
If four questions share a passage, their scores can rise or fall together. Here each row is one passage with four answers, and all four share a score. Drawing whole rows keeps the group together. The resulting bar is wider for this test.
passages = np.repeat(correct.reshape(50, 4)[:, :1], 4, axis=1)
rng = np.random.default_rng(0)
by_question = rng.choice(passages.ravel(), (10_000, 200)).mean(axis=1)
rows = rng.integers(0, len(passages), (10_000, len(passages)))
by_passage = passages[rows].mean(axis=(1, 2))
for label, scores in (("questions", by_question), ("passages", by_passage)):
lo, hi = np.percentile(scores, [2.5, 97.5])
print(f"{label}: {lo:.1%} to {hi:.1%}")questions: 74.5% to 85.5% passages: 68.0% to 90.0%
Run on Python 3.13.3, NumPy 2.5.5 and SciPy 1.18.1. Checked on .
Quiz
Two questions on this lesson, and one from earlier. Answer as often as you like.
1 of 3
From earlier: Samples, averages and the normal distribution
A model gets 9 questions in 10 right, so one question's standard deviation is 0.3. On a test of 100 questions, what is the standard error of its score, in points?
2 of 3
You bootstrap a model's 200 answers to put an error bar on its score. But each draw takes every answer once, with no repeats. What happens to the error bar?
3 of 3
You build a model whose true score you know. You give it 200 tests, and put a 95% error bar on each score with the bootstrap. About how many of the 200 bars miss the true score?
The ideas in this lesson
- Bootstrap
- Draw your own data again, at random and with repeats, many times, to see how much a number you measured could move.
- Confidence interval
- The middle 95% of the bootstrap's scores: an error bar that, made this way, catches the true value 95 times in 100.
- Error bar on a gap
- Bootstrap the difference itself, on the same questions: if its error bar crosses zero, the test can't tell which is better.
It leans on
Transcript
What the video says, part by part. Press a time to watch from there.
How sure is one score?
Here's a puzzle. A model takes one test, and it gets a score. But you can't keep giving it new tests. So how sure can you be of that one score?
Your guess
Say the model got a hundred and sixty questions right, out of two hundred. That's eighty percent. Its true score is what it would get on all the questions like these. How far from eighty percent could that be? About one point? About five? Or about fifteen?
One test
One test gave this model eighty percent. Now picture a model we built ourselves, one that gets each question right eight times in ten. We could give it a thousand tests, and watch the scores pile into a bell. The bell's spread would say how far one score moves. But a real model gets one test, and you don't know its true score. New questions take time and money to write. Like a poll on the news, we want a "plus or minus". So what can you do with just these two hundred answers?
A draw with repeats
Here's the trick. Treat the two hundred answers as a small copy of all the questions there could be. Pick one answer at random, write it down, and put it back. That's a sample of one. Do that two hundred times, and you have a new test, made from the old one. Some answers come up twice, some three times, and some never. Score the new test: seventy-six percent. Drawing a test again from its own answers, like this... is called the bootstrap.
Why the repeats
But why put each answer back? Try it without repeats.
Now each answer comes up once. That's the same two hundred answers, in a different order. Eighty percent. Again, eighty. Every time. No spread at all. But we know a new test could score differently. The repeats make each pick a fresh one, like a new question.
The middle ninety-five percent
Now make ten thousand of these new tests: ten thousand bootstrap draws. Where do their scores land?
They pile up around eighty percent, in a bell. Cut off the lowest two and a half percent of the scores, and the highest two and a half percent. Keep the middle ninety-five percent. It runs from seventy-four and a half percent to eighty-five and a half. That range is called a ninety-five percent confidence interval.
On a chart, it's drawn as an error bar.
Plus or minus
So the score is eighty percent, plus or minus about five and a half points.
If you said one point, here's why it felt right.
A score feels like a fact about the model, and rankings print it to a tenth of a point. But two hundred questions are a small sample.
What ninety-five percent means
But can a trick on one test really find the truth?
Let's check it where we know the truth: a model we make, one that gets each question right eight times in ten. Give it a hundred tests, each with two hundred fresh questions.
Bootstrap each one, and draw its error bar. How many of the hundred bars will cross eighty percent?
Ninety-five of them. Five miss. That's what ninety-five percent means. Make error bars this way, and ninety-five times in a hundred, the bar catches the true score.
Bars on a real test
How big are the error bars on real tests?
Here's a well-known test of hard science questions. It has a hundred and ninety-eight of them. On one ranking of AI models, the top model scores about ninety-six percent. Its error bar is plus or minus about two and a half points. That's wide. But the ranking prints the score to a tenth of a point. And the next model is a fifth of a point behind.
To tell those two apart, look at the gap between them, with an error bar of its own.
Two models
Here's a gap of our own. Model A is our model, at eighty percent. Model B took the same two hundred questions, and got eighty-four percent: four points more. On most questions, the two models agree. They differ on twenty-eight: B is right on eighteen of those, and A on ten.
Is B really better?
The gap's error bar
Bootstrap the gap. Draw two hundred questions with repeats, score both models on that same draw, and keep the gap. Do it ten thousand times. The gaps pile up around four points. The middle ninety-five percent runs from minus one to plus nine and a half. Zero means no gap, and this bar crosses zero. So B might be better by up to nine and a half points... or worse by one. These two hundred questions can't tell us.
In code
Now in real code. Here are the model's two hundred answers, as ones and zeros. NumPy draws ten thousand tests of two hundred, with repeats, in one line. Then we take each draw's score, and the middle ninety-five percent. What will it print?
Zero point seven four five, and zero point eight five five: our error bar.
SciPy does all of it in one line, with a slightly more careful way to cut the ends.
Its answer is nearly the same: zero point seven four to zero point eight five.
One research paper, from the AI company Anthropic, asks for exactly this.
Report the error bar with every score, and compare two models on the same questions.
A score and its error bar
So draw a test's own answers again, with repeats, many times.
That's the bootstrap. The middle ninety-five percent of the scores is the error bar, the confidence interval. And when a gap's error bar crosses zero, you can't tell which model is better.
Every score needs its error bar
One test's score can move, and its error bar shows how far. Now try it on this lesson's page, and take the quiz. Find out how many questions keep the bar within three points.
You use this lesson in Lab 4: An A/B test of two prompts, after A/B tests and p-values.
Facts last checked on .
