Module: L2-M01 · Duration: ~120 min (estimate, including the independent exercise) · Previous: L03 · Next: L05
1. Introduction
An evaluation produces one number: "0.897". How precise is that number? If you reran it with 40 different cases, would you get 0.85 or 0.95? If tutor v2 scores 0.833, is it really worse than v1 or is it just luck? Without statistics, these questions only have gut-feeling answers.
In L1-M06-L02 you used the Wilson interval to measure one case run repeatedly. This lesson changes the unit of sampling from "one run" to "one case", and asks the question of the person who designs an evaluation: how many cases are needed for a conclusion to be worth anything? It does not require a statistics course; every formula runs in the lab and is checked by tests.
2. Learning objectives
- Explain why a pass rate on a dataset is an estimate and why the unit of sampling is the case, not the run.
- Compute the Wilson interval for a rate and explain why the Wald interval is not trustworthy at small n.
- Compute the number of cases needed to reach a given margin of error, and the upper bound on the failure rate when every case passes.
- Compare the intervals of two versions and judge whether the dataset is large enough to tell them apart.
- Apply the SHIP / BLOCK / INCONCLUSIVE decision rule based on the interval bounds.
3. Prerequisites and tools
- L1-M06-L02: pass rate, Wilson interval, stability gate. This lesson reuses them and does not reteach them.
- L2-M01-L01 to L03:
run_eval,criterion_rates, graders. - The code lab
code-labs/l2-m01, which needs only the Python standard library and pytest.
4. Concepts
4.1 A pass rate is an estimate
The paper "Adding Error Bars to Evals" (Evan Miller, 2024) proposes treating the questions of an eval as a sample drawn from a larger but unseen "population" of questions, and recommends computing the standard error of the mean score, then reporting a
confidence interval [R1]. For a 0/1 score (pass/fail), the standard error is sqrt(p(1-p)/n) where p is the observed rate, and the 95% confidence interval is the mean plus or minus 1.96 times the standard error [R1].
In words: if your 40 cases are a sample from all the questions a student could ask, then 0.897 is an estimate of the true rate, not the true rate itself. A different set of 40 cases would give a different number. A confidence interval gives the range of plausible values of the true rate, assuming the sample was drawn reasonably.
4.2 Wald and Wilson
The formula above is the Wald interval: p ± 1.96 × sqrt(p(1-p)/n). It is simple, but behaves badly when n is small or p is close to 0 or 1, exactly the region evals often fall into. The Wilson interval (which you used in L1-M06) fixes that.
Instead of trusting the theory, measure. The coverage of a 95% interval is the probability that the interval contains the true rate. stats.exact_coverage computes it exactly: given a true rate p and n cases, it walks through every possible value k (the number of passing cases), computes the binomial probability of k, and adds up the probabilities of those k for which the interval contains p.
With n=20 and a true rate of 0.90, the coverage of Wald is 0.876 and of Wilson is 0.957 (§8). A Wald "95%" interval is actually right only 87.6% of the time, meaning it is more confident than it deserves to be. Wilson is close to 95%.
4.3 How many cases
The interval narrows as n grows, but slowly: the half-width of the Wilson interval when the observed rate is 0.90 is ±0.137 at n=20, ±0.095 at n=40, ±0.060 at n=100, ±0.030 at n=400, ±0.015 at n=1600 (§8). Making the number of cases 4 times larger only halves the width.
For planning, the standard formula n = z² × p(1-p) / m² (z = 1.96) gives the number of cases needed for the half-width to be at most m (normal approximation). When the rate is unknown, use p = 0.5 (the worst case): 385 cases for ±0.05. When the expected rate is 0.9: 139 cases for the same margin.
An important special case for QA: seeing no failures. 40 cases all passing does not mean the failure rate is 0. The 95% upper bound of the failure rate is 1 - 0.05^(1/n): for n=40, it is 0.072. To conclude "the failure rate is below 5%" just by looking at all cases passing, you need 59 cases all passing
(stats.zero_failure_required_n). This is a mathematical consequence of probability, not a number quoted from an outside source.
4.4 The unit of sampling is the case, not the run
Running each case 10 times gives 400 result rows, but 400 rows are not 400 independent observations: the 10 runs of the same case resemble each other in difficulty. Miller names exactly this problem: when questions belong to groups, independence is violated, and ignoring it "may lead an unsuspecting analyst to suppose that the measurement of the overall eval score is much more precise than it actually is" [R1]; he proposes clustered standard errors.
The lab has a simpler way to see the effect: bootstrapping by case (read this part for understanding; you do not need to implement it) (stats.cluster_bootstrap_ci): resample the 40 cases with replacement many times, compute the pooled rate each time, and take the middle 95%.
For tutor v1, treating the 400 runs as independent gives an interval of 0.864-0.924 (width 0.060), while the bootstrap by case gives 0.855-0.935 (width 0.080) (§8). The difference here is moderate because the lab's cases differ only slightly in difficulty; with a dataset that has very easy and very hard cases, the "falsely independent" interval would be far narrower than reality.
Repeated runs reduce the noise of each case (useful, L1-M06), but only new cases narrow the uncertainty about the population of questions.
4.5 Is the dataset large enough to tell two versions apart?
Tutor v1 scores 0.897 and v2 scores 0.833 on the same 40 cases x 10 runs. The question is: if the two versions really differ by that much, will a dataset of this size often detect it? That is the power of the experiment, a concept you met in L1-M06-L04 §4.3.
Miller's paper devotes a section to power analysis and sample-size formulas [R1]; module L2-M07 will teach paired comparison. Here we only use the simple (unpaired) version that the lab implements itself
(stats.required_n_two_rates, using the standard library's statistics.NormalDist.inv_cdf [R2]): to tell apart true rates of 0.90 and 0.80 at a 5% significance level and 80% power you need 197 cases per version.
Look at a simulation, and read it carefully. In this simulation v1 and v2 really differ, but the true difference is only about 0.065 (estimated from the 0.897 and 0.833 above), not 0.10, so the 197 cases of the 0.90 versus 0.80 example do not apply directly. The lab repeats 200 times with different seeds, each time a pass with one run per case, and counts two criteria: the two Wilson intervals do not overlap, and a two-proportion z-test at the 0.05 level. With 40 cases: 0.050 and 0.210; with 200 cases: 0.405 and 0.720; with 400 cases: 0.810 and 0.970 (§8). Three takeaways. (1) With 40 cases, the test detects the true difference only about once in five times: a conclusion "v2 is worse" on 40 cases often does not hold up, even when it is true. (2) The "no overlap" criterion is stricter than a 0.05-level test on the difference, so it gives clearly lower power at the same size. (3) The "200" and "400 cases" here are 5 and 10 copies of the 40 cases with new ids, simulating more cases from the same distribution; 400 real cases, with varying difficulty, would still have to account for the clustered nature from §4.4.
A note on "start small". Anthropic advises that "20-50 simple tasks drawn from real failures is a great start" [R3]. The two pieces of advice do not conflict: 20-50 cases are enough to find defects and build the habit, but not enough to estimate a rate precisely, or to tell two versions apart when the difference is small. Anthropic says 20-50 is enough early on because at the start the defects and differences are usually large; as you refine, the differences shrink and more cases are needed.
5. Technical deep dive
5.1 The functions in lab/stats.py
| Function | What it does |
|---|---|
wilson(k, n), wald(k, n) |
The two 95% intervals; raise an error if n <= 0 or k is outside [0, n] |
exact_coverage(method, n, p) |
The exact coverage of an interval, summing binomial probabilities |
margin_of_error(k, n) |
The half-width of the Wilson interval |
required_n_margin(p, margin) |
The number of cases for a margin of error |
zero_failure_upper_bound(n), zero_failure_required_n(rate) |
When every case passes |
required_n_two_rates(p1, p2) |
The number of cases per version to tell two rates apart |
cluster_bootstrap_ci(per_case) |
A bootstrap interval resampling by case; seeded |
decide(k, n, threshold) |
SHIP if the lower bound ≥ threshold, BLOCK if the upper bound < threshold, otherwise INCONCLUSIVE |
5.2 The decision rule by interval bounds
decide(k, n, threshold) is the stability gate of L1-M06 applied to a dataset. For example with a threshold of 0.90: 990/1000 → SHIP; 80/100 → BLOCK; 90/100 → INCONCLUSIVE, because the lower bound of the Wilson interval for 90/100 lies below 0.90 even though the observed rate equals the threshold.
With "INCONCLUSIVE", the right action is to collect more cases (not to rerun until it looks good). The test test_decide_gate pins these three cases.
6. The QA perspective
6.1 A correct report line
Every reported rate must come with: numerator/denominator, the unit of sampling (case), the number of cases and runs, the interval method, the seed, and the dataset fingerprint. For example: "pedagogy 128/140 (0.914), Wilson 0.856-0.950; 14 cases x 10 runs; the intervals treat runs as independent so they are optimistic (§4.4)". If you do not state the number of cases, the reader cannot judge the reliability.
6.2 Decide before looking at the results
- Choose the threshold and the margin of error from the risk (L02) before running, and only then compute the number of cases needed.
- If the minimum number of cases is larger than the dataset you have, that is your finding: "the current dataset is not enough to conclude". Say so plainly.
- Do not increase n bit by bit until the interval looks "nice" (stopping early when you see the desired result makes the interval wrong). Record the sample-size plan first.
- Do not see two overlapping intervals and conclude "equal". Overlap proves neither equality nor difference; the "no overlap" criterion is also stricter than a test at the 0.05 level (a test on the difference is more sensitive: L1-M06-L04 and L2-M07). For example: 359/400 versus 333/400 have slightly overlapping intervals (0.864 versus 0.866), but the z-test gives z ≈ 2.69.
7. Real-world scenarios
Scenario 1 (fictional): "40/40 pass, ship". The HocVui Tutor team expands the safety set to 40 cases, runs it once, all pass, and they decide to ship. QA computes the upper bound: with 40/40, the failure rate could still be as high as 7.2% (95%). For children's data,
7.2% is too high; they need at least 59 cases all passing just to conclude below 5%, and more for a tighter target.
Scenario 2 (fictional): "v2 wins by 3 points". A team compares v2 with v1 on 40 cases and sees a 3 percentage-point difference in favor of v2. The two versions' intervals overlap almost completely. They plan to ship v2. QA points out that with 40 cases, most differences of that size are noise; even if the two versions really differ by 10 points, about 197 cases per version are needed to detect it often. The right decision is to expand the dataset before deciding.
8. Hands-on tutorial
8.1 Run the demo
cd academy-content/code-labs/l2-m01
python -m lab.demo l04
Output (copied from a real run on Python 3.14.6 and 3.12.14, 2026-11-03; the two versions are identical; it runs in a few seconds):
A. exact coverage of a nominal 95% interval, n=20 cases, true rate 0.90
Wald 0.876
Wilson 0.957
B. half-width of the Wilson interval when the observed rate is 0.90
n= 20: +/-0.137
n= 40: +/-0.095
n= 100: +/-0.060
n= 400: +/-0.030
n=1600: +/-0.015
C. planning
cases for +/-0.05 at unknown rate: 385; at rate 0.9: 139
40 cases all pass: failure rate could still be 0.072
cases that all pass to show failure rate < 5%: 59
cases per variant to tell 0.90 from 0.80: 197
D. runs are not cases: tutor-v1, 40 cases x 10 runs
treating 400 runs as independent: 0.864-0.924 (width 0.060)
resampling the 40 cases: 0.855-0.935 (width 0.080)
E. can the dataset tell v1 from v2? 200 repeats with different seeds, one run per case
(non-overlap = the two Wilson intervals do not overlap; z-test = two-proportion z-test at alpha 0.05)
40 cases: non-overlap 0.050, z-test 0.210
200 cases: non-overlap 0.405, z-test 0.720
400 cases: non-overlap 0.810, z-test 0.970
Reading the output:
- A. Wald 0.876 and Wilson 0.957 are exact coverages, not random estimates.
- B. To halve the width, you need four times as many cases: 100 → 400 → 1600.
- C. Four numbers used when planning a sample size.
- D. The interval by case is one third wider than the interval that "treats runs as independent".
- E. For "200" and "400 cases", the lab multiplies the existing cases with new ids (
H01x1, ...) to simulate more cases from the same distribution; it is a simulation, not 400 different questions.
8.2 Run the tests
python -m pytest tests/test_l04_stats.py -v
Result: 16 passed. Read test_coverage_wald_is_below_nominal_and_wilson_is_close and test_forty_cases_cannot_separate_v1_from_v2_but_four_hundred_usually_can.
8.3 Compute an interval yourself
Save it as try_l04.py in the lab's root folder (next to lab/):
from lab import stats
k, n = 359, 400
low, high = stats.wilson(k, n)
print(f"{k}/{n} = {k / n:.3f}, Wilson {low:.3f}-{high:.3f}, half-width {stats.margin_of_error(k, n):.3f}")
print("gate at 0.90:", stats.decide(k, n, 0.90), "| gate at 0.95:", stats.decide(k, n, 0.95))
Output:
359/400 = 0.897, Wilson 0.864-0.924, half-width 0.030
gate at 0.90: INCONCLUSIVE | gate at 0.95: BLOCK
With a threshold of 0.90, the interval 0.864-0.924 contains the threshold so no conclusion yet; with a threshold of 0.95, the whole interval lies below it so BLOCK. Remember this interval is still optimistic because it treats runs as independent (§4.4).
8.4 Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
ValueError: k must be between 0 and n |
The pass count is wrong | Check that passes <= n |
| Part E runs slowly | The simulation is 200 repeats x 3 sizes | Normal, a few seconds |
| Numbers differ from the lesson | Different seed or dataset size | Use seed=1, 40 cases x 10 runs |
9. Independent exercise
Problem: Plan a sample size for the tutor and check the reliability of the two kinds of interval.
Requirements: fill in ANSWERS in exercises/test_l04_sample_size.py, computed with lab.stats, not typed by hand:
margin_cases: the number of cases to reach a margin of ±0.04 when the expected rate is about 0.92.variant_cases: the number of cases per version to tell a true rate of 0.90 from 0.85 (significance level 0.05, power 0.80).wald_coverandwilson_cover: the exact coverage of the two intervals with n=30 cases and a true rate of 0.95 (rounded to 3 digits).- In a comment, write two sentences: why
variant_casesis much larger than the 0.90 versus 0.80 example in the lesson, and what you conclude about Wald.
Test data: lab.stats; no data file needed.
Deliverable: the completed test file, with the two explanatory sentences.
Acceptance criteria: python -m pytest exercises/test_l04_sample_size.py passes (2 tests); the Wald coverage is below 0.90, the Wilson coverage is above 0.90.
Hint: round(x, 3); the smaller the gap between the two rates, the more cases are needed (inversely proportional to the square of the gap).
10. Knowledge check
0/18 done · Results are kept in this browser only.
P02 · True or false
P03 · Short answer
All 40 cases pass. Why can you not conclude the failure rate is 0, and at least how many all-passing cases do you need to conclude the failure rate is below 5%?
P04 · Select all that apply
P05 · Fill in the blank
P06 · Put in order
P08 · Spot the bug
The snippet below decides against a threshold of 0.90 for the result 90/100. It prints SHIP. Find the bug.
low, high = stats.wilson(90, 100)
print("SHIP" if 90 / 100 >= 0.90 else "BLOCK")P09 · Short answer
Why are the 400 rows of 40 cases x 10 runs not 400 independent observations, and how does that change the interval in the demo?
P10 · Essay
A team compares v2 with v1 on 40 cases and sees v2 ahead by 3 percentage points, with the two intervals overlapping almost completely. They plan to ship v2. Write 5-8 sentences saying what you tell them, using the numbers from the lesson.
P11 · Multiple choice
P12 · True or false
P13 · Select all that apply
P14 · True or false
P15 · Multiple choice
P16 · Recall
Lesson: L1-M06-L02P17 · Recall
Lesson: L2-M01-L0311. Summary
- Concepts: a pass rate is an estimate; the Wilson interval is better than Wald at small n; the width shrinks with the square root of n; seeing no failures still leaves an upper bound; the unit of sampling is the case; power tells whether a dataset can tell two versions apart; decisions rest on the interval bounds.
- Skills: compute intervals, sample sizes and the bound for 0 failures; bootstrap by case; run a power simulation; use
decide. - Traps: treating 400 runs as 400 observations; trusting the Wald interval; concluding from 40 cases; concluding "equal" when two intervals overlap (or "different" just because they do not overlap, which is a strict criterion); stopping collection when the result is pleasing.
- You can now: say how many cases a dataset needs for a specific conclusion, and report the current result with honest uncertainty.
12. References
| ID | Title | Publisher | URL | Accessed | Relevance |
|---|---|---|---|---|---|
| R1 | Adding Error Bars to Evals: A Statistical Approach to Language Model Evaluations (Evan Miller, 2024) | arXiv | https://arxiv.org/abs/2411.00640 (full-text PDF: https://arxiv.org/pdf/2411.00640) | 2026-11-03 | Standard error, 95% interval = mean ± 1.96 SE, clustered standard errors, power analysis |
| R2 | statistics: Mathematical statistics functions (NormalDist, inv_cdf) | Python Software Foundation | https://docs.python.org/3/library/statistics.html | 2026-11-03 | NormalDist.inv_cdf used in required_n_two_rates |
| R3 | Demystifying evals for AI agents | Anthropic | https://www.anthropic.com/engineering/demystifying-evals-for-ai-agents | 2026-11-03 | "20-50 simple tasks ... a great start" |
Key terms (English ↔ Vietnamese)
| English | Tiếng Việt |
|---|---|
| Confidence interval | Khoảng tin cậy |
| Standard error | Sai số chuẩn |
| Coverage | Độ phủ (xác suất khoảng chứa giá trị thật) |
| Margin of error | Biên sai số (nửa độ rộng của khoảng) |
| Sample size | Cỡ mẫu (số case) |
| Power | Độ mạnh (xác suất phát hiện một khác biệt có thật) |
| Bootstrap | Rút mẫu lại có hoàn lại để ước lượng độ bất định |
| Unit of sampling | Đơn vị lấy mẫu |