Sub topic 14
Sample size, variance, and confidence intervals
This chapter assumes you can read Python, know what a mean and a standard deviation are, and already have per-question scores such as faithfulness or nDCG from earlier chapters.
Why a single average misleads
An evaluation score is an average over a small set of questions. If you change the retriever and the mean faithfulness moves from 0.78 to 0.82, you can't yet say the change is real. A different sample of 40 questions might have produced the opposite result.
Two sources of noise cause this:
- Sampling noise. Your questions are a small draw from all questions users could ask.
- Judge and generator noise. An LLM judge or a sampled generator can give different outputs on the same input.
A confidence interval (CI) is a range that expresses this uncertainty. A 95% CI is built by a procedure that, over many repeated experiments, would contain the true mean about 95% of the time.
Bootstrapping a confidence interval
Scores like faithfulness are bounded and often skewed, with many 1.0 values and a few 0.0 values. A normal-approximation formula fits them poorly. The percentile bootstrap makes no shape assumption. It resamples your questions with replacement, computes the mean of each resample, and reads the interval from the sorted means.
import random
def bootstrap_ci(scores, n_resamples=2000, alpha=0.05, seed=0):
rng = random.Random(seed)
n = len(scores)
means = []
for _ in range(n_resamples):
sample = [scores[rng.randrange(n)] for _ in range(n)]
means.append(sum(sample) / n)
means.sort()
lo = means[int((alpha / 2) * n_resamples)]
hi = means[int((1 - alpha / 2) * n_resamples) - 1]
return sum(scores) / n, lo, hi
per_question_faithfulness = [1.0, 0.5, 1.0, 0.0, 1.0, 0.75, 1.0, 0.5]
print(bootstrap_ci(per_question_faithfulness))With only eight questions the interval is wide. That is the correct answer: eight questions can't pin the mean down.
How sample size affects width
The width of a CI for a mean shrinks roughly with the square root of the number of questions. To halve the width, you need about four times as many questions.
| Questions | Relative CI width |
|---|---|
| 25 | 1.0 |
| 100 | 0.5 |
| 400 | 0.25 |
Variance matters too. If per-question scores range widely, the interval is wider at the same size. Reporting the interval, not only the mean, shows both effects.
Comparing two systems
Comparing two systems by checking whether their separate intervals overlap is a common mistake. Both systems answer the same questions, so the scores are paired. Compute the per-question difference (new minus old), then bootstrap the CI of those differences.
old = [1.0, 0.5, 1.0, 0.0, 1.0, 0.75, 1.0, 0.5]
new = [1.0, 0.75, 1.0, 0.25, 1.0, 0.75, 1.0, 0.5]
diffs = [n - o for n, o in zip(new, old)]
mean_diff, lo, hi = bootstrap_ci(diffs)
print(mean_diff, lo, hi)
print("improvement is clear" if lo > 0 else "cannot rule out no change")If the interval for the difference includes zero, you haven't shown an improvement. The paired method removes question difficulty from the noise, so it is usually sharper than comparing two separate intervals.
Common errors
- Reporting a mean to three decimals from 20 questions. The extra digits imply precision you don't have.
- Tuning prompts against the evaluation set, then reporting the same set's score. Keep a held-out set.
- Ignoring repeat noise. If the generator or judge is non-deterministic, run it several times per question and average within each question first.
- Treating questions as independent when many come from one document. Correlated questions make the real interval wider than the bootstrap suggests.
Exercise
A change raises mean nDCG@5 from 0.61 to 0.64 on 30 questions. The bootstrap CI of the per-question differences is [-0.02, 0.08]. Can you claim an improvement, and what would you do next?
Show answer
No. The interval includes zero, so the data is consistent with no change or a small drop. Add more questions (roughly four times as many halves the interval width), check whether the questions are varied enough, and repeat the paired comparison.