Sub topic 12
How Many Cases Are Enough
This chapter assumes you can write basic Python, read JSONL, and know what a proportion and a sample size are. It also assumes you have a per-case grader that returns pass or fail, as built in the earlier chapters. Here you will learn how much a score moves from sampling noise alone, and how many cases you need before a number or a difference between two numbers means something.
A score is an estimate
Your dataset is a sample of all the inputs the app will see. If 80 of 100 cases pass, the measured accuracy is 0.80. The true accuracy on the whole input population could be 0.72 or 0.88. A different 100 cases drawn from the same source would give a different number.
The size of this noise depends mostly on the number of cases, not on how large the app's traffic is. For a pass/fail metric, the standard error is:
se = sqrt(p * (1 - p) / n)A 95% confidence interval is roughly the score plus or minus 1.96 times that value. This is the normal approximation. It is fine when p is not near 0 or 1 and n is at least a few dozen.
| Cases (n) | Margin at p = 0.8 (95%) |
|---|---|
| 50 | about ±11.1 points |
| 100 | about ±7.8 points |
| 200 | about ±5.5 points |
| 400 | about ±3.9 points |
| 1000 | about ±2.5 points |
The margin shrinks with the square root of n. Halving the margin takes four times as many cases. That is why going from 50 to 100 cases helps a lot, and going from 1000 to 1100 barely helps.
Measure the uncertainty directly
You don't need to trust the formula. The bootstrap resamples your per-case scores with replacement and recomputes the mean many times. The spread of those means is your interval.
import random
def bootstrap_ci(scores, n_boot=1000, alpha=0.05, seed=0):
rng = random.Random(seed)
n = len(scores)
means = sorted(sum(rng.choices(scores, k=n)) / n for _ in range(n_boot))
lo = means[int(n_boot * alpha / 2)]
hi = means[int(n_boot * (1 - alpha / 2)) - 1]
return lo, hi
scores = [1] * 80 + [0] * 20
print(bootstrap_ci(scores))For 80 passes out of 100, the interval comes out near 0.72 to 0.88, matching the table. Report the interval next to every headline score. A line such as "0.80 (0.72 to 0.88, n=100)" tells a reader how much to trust the number.
The bootstrap works on any per-case numeric score, such as rubric points from 1 to 5, not only pass/fail.
Plan the size before you label
You can invert the margin formula to get a target size. For a desired margin m and an expected accuracy p:
n = 1.96^2 * p * (1 - p) / m^2import math
def cases_needed(p, margin, z=1.96):
return math.ceil(z ** 2 * p * (1 - p) / margin ** 2)
print(cases_needed(0.8, 0.05))
print(cases_needed(0.5, 0.05))This prints 246 and 385. If you don't know p, use 0.5. That is the worst case and gives the largest n. A target of ±5 points needs roughly 250 to 400 cases for the whole dataset. Treat this as a floor for a single overall score. Slices and version comparisons need more, as the next sections show.
Comparing two versions needs more cases
Most eval runs ask a different question: is prompt B better than prompt A? The difference between two scores has more noise than either score alone.
If you test each version on separate random samples, the standard error of the difference is sqrt(2 * p * (1 - p) / n). With n = 200 and p = 0.8, that is about ±7.8 points. To detect a true 3-point improvement at p = 0.8 you need roughly 2 * 1.96^2 * 0.16 / 0.03^2, or about 1,370 cases per version.
You can do much better by running both versions on the same cases. This is a paired comparison. Hard cases are hard for both versions, so that variation cancels out. What remains is the per-case difference: +1 where only B passes, -1 where only A passes, and 0 elsewhere.
import random
def paired_bootstrap_diff(scores_a, scores_b, n_boot=1000, alpha=0.05, seed=0):
assert len(scores_a) == len(scores_b), "scores must be aligned by case"
diffs = [b - a for a, b in zip(scores_a, scores_b)]
rng = random.Random(seed)
n = len(diffs)
means = sorted(sum(rng.choices(diffs, k=n)) / n for _ in range(n_boot))
lo = means[int(n_boot * alpha / 2)]
hi = means[int(n_boot * (1 - alpha / 2)) - 1]
return lo, hiIf the interval excludes 0, the difference is unlikely to be sampling noise. If it includes 0, you can't tell the versions apart with this dataset. Both versions must be scored on the same cases, in the same order, which is another reason to keep stable case ids.
Slices need their own sample size
An overall score can be precise while a slice score is not. Suppose your dataset has 400 cases, but only 30 are tagged adversarial. At p = 0.8 the margin on that slice is about ±14 points. A drop from 0.80 to 0.70 on 30 cases is well within noise.
Practical rules for slices:
- Print n next to every slice score. A table of percentages without counts invites over-reading.
- Set a minimum n for a slice, for example 30, and mark smaller slices as "indicative only".
- Add cases to the slices you make decisions on, not evenly across all slices. If refunds are the costly failure, 100 refund cases are worth more than 100 extra greeting cases.
- Don't hunt through many slices for the one that moved. With 20 slices, one will likely look different by chance. Decide which slices matter before you run the eval.
Edge cases and gotchas
Scores near 0 or 1. The normal approximation breaks down. At 100 out of 100 passes, the formula gives a margin of zero, which is wrong. Use the bootstrap with care, or a Wilson interval. A useful rule of thumb is that if you see 0 failures in n independent cases, the true failure rate could still be as high as about 3/n at 95% confidence. With 100 clean cases, you can only say the failure rate is below about 3%.
Cases that aren't independent. The formulas assume each case is an independent draw. Fifty questions generated from one document, or paraphrases of one question, behave like fewer than 50 cases. Your real effective n is lower, and the intervals above are too narrow. Deduplicate near-copies, and consider resampling by group (document or user) instead of by case.
Non-deterministic apps. If the app or the grader gives different answers on repeated runs, part of the noise comes from the run, not the cases. Running each case 3 to 5 times and averaging reduces run noise. It does not reduce case-sampling noise. For that you still need more distinct cases. When the two sources of noise are of similar size, more distinct cases usually give more value per dollar than more repeats.
Noisy labels and judges. Wrong references or an unreliable LLM judge add error that more cases cannot remove. A larger dataset with 10% bad labels gives a precise but biased score. Fix label quality first, using the agreement checks from the earlier chapters.
Dev and test splits. The interval applies to each split on its own. A 20% test split of 300 cases is only 60 cases, with a margin near ±10 points. If the test set is the final gate, size it for the margin you need, and don't take it as a fixed fraction of whatever you have.
Practical starting points
| Goal | Rough size |
|---|---|
| Smoke test that catches large regressions | 30 to 50 cases |
| Overall score within about ±5 points | 250 to 400 cases |
| Reliable slice scores | 30 or more cases per slice that matters |
| Detecting a 3-point change, unpaired | over 1,000 per version |
| Detecting a small change, paired | depends on how often versions disagree, so measure it |
These are starting points. The right size is the one that makes your intervals narrow enough to support the decisions you make. If a decision hinges on a 2-point difference and your interval is ±6, you don't have enough cases. Better to know that than to ship on noise.
A reasonable workflow is to start small, run the bootstrap, and see how wide the intervals are. Then add cases where the intervals are too wide to act on. Cases found through production failures, covered in a later chapter, are the best use of that growth.
Exercise
Your dataset has 150 cases and the app passes 90% of them. What is the approximate 95% margin on that score? How many cases would you need for a margin of ±3 points at the same pass rate?
Show answer
Standard error = sqrt(0.9 0.1 / 150) = sqrt(0.0006), about 0.0245. Multiplied by 1.96, the margin is about ±4.8 points. For ±3 points, n = 1.96^2 0.9 0.1 / 0.03^2 = 3.8416 0.09 / 0.0009, about 384.2, so you need 385 cases.
Exercise
Version B scores 0.84 and version A scores 0.80 on the same 200 cases. The paired bootstrap interval for B minus A is (-0.01, 0.09). What can you conclude, and what would you do next?
Show answer
The interval includes 0, so the 4-point gain could be sampling noise. You can't claim B is better. It also doesn't show B is worse or the same, because the interval allows anything from a 1-point loss to a 9-point gain. Next, add cases, especially on slices where the two versions disagree, and rerun the paired comparison. Or accept that the dataset can't resolve a difference this small.
Exercise
A slice tagged adversarial has 25 cases and drops from 0.84 to 0.72 between runs. Why is it risky to act on this drop alone?
Show answer
With n = 25 the margin on each score is about ±14 to ±18 points, so the two intervals overlap heavily and the 12-point drop is within sampling noise. If you checked many slices, one of them would likely show a drop like this by chance. Add cases to the slice, or run a paired comparison on the same cases, before acting.