Sub topic 10
Handling nondeterminism and reporting uncertainty
This chapter assumes you have the harness from the earlier chapter, where run_eval returns a pass rate and per-case results. It also assumes you know what a mean, a proportion, and a variance are. By the end you should be able to say whether version B of your application is really better than version A, or whether the gap is noise.
Where the randomness comes from
A single pass rate looks precise, such as 0.72. It is one draw from a noisy process. The noise has several sources:
- Sampling. With a temperature above 0, the model picks tokens randomly, so the same input can produce different outputs.
- Provider-side variation. Even at temperature 0, hosted models can return different outputs for the same request. Batching, floating-point ordering, and silent model updates all contribute. Treat a seed parameter as best effort, not a guarantee.
- Judge variation. An LLM judge is also a model call, so the same answer can get PASS on one run and FAIL on the next.
- System variation. Retrieval indexes change, tools return live data, and agents take different paths.
- Case selection. Your test set is a small sample of all the inputs users could send. A different sample would give a different score.
The last source is separate from the others. Even a perfectly deterministic application has an uncertain score, because you measured it on a finite set of cases. Both kinds of noise matter, and they are handled differently.
Reduce the noise you control
Remove the variation you can before you measure the rest.
- Set the temperature to 0 for the judge and for any application step where you want stable output. Keep the production temperature for the application when you want to measure production behavior.
- Pin the model version by its dated identifier, not an alias like "latest". Record it with every result.
- Freeze the retrieval index and mock or record tool responses during evaluation.
- Store every raw output, judge verdict, and configuration value so a surprising score can be traced.
This lowers the noise but does not remove it. You still need to measure what is left.
Repeat runs, then average per case
If the application is stochastic, run each case K times (3 to 5 is common) and average the results per case. The per-case mean is a fraction between 0 and 1, so a case that passes 2 of 3 times scores 0.67. This also tells you which cases are unstable, which is useful in error analysis.
def run_repeated(app, cases, scorer, k=3):
"""Return one mean score per case, averaged over k runs."""
per_case = []
for case in cases:
runs = [scorer(app(case["input"]), case["expected"]) for _ in range(k)]
per_case.append(sum(runs) / k)
return per_caseRepeating runs shrinks only the run-to-run noise. Ten runs of the same 20 cases do not tell you much more about how the app does on cases you did not write. For that you need more cases. Do not treat the 20 × 10 = 200 results as 200 independent observations.
How wide is the uncertainty?
For a pass rate p over n independent cases, the standard error is sqrt(p * (1 - p) / n). With p = 0.7 and n = 100, that is about 0.046. A 95% interval is roughly the pass rate plus or minus 1.96 standard errors, here about ±0.09. A reported 70% could plausibly be anywhere from 61% to 79%.
The same formula tells you how many cases you need. For a margin of ±0.05 at p = 0.7, solve n = 1.96² × p × (1 - p) / 0.05². That gives about 323 cases. Halving the margin requires four times as many cases.
The normal approximation works poorly for small n or for pass rates near 0 or 1. The bootstrap is a simpler tool that needs fewer assumptions.
Bootstrap confidence intervals
A percentile bootstrap resamples your per-case scores with replacement, computes the mean each time, and reads the interval from the sorted means.
import random
def bootstrap_ci(scores, n_resamples=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_resamples)
)
lo = means[int((alpha / 2) * n_resamples)]
hi = means[int((1 - alpha / 2) * n_resamples) - 1]
return lo, hi
scores = [1, 0, 1, 1, 0, 1, 1, 1, 0, 1]
print(bootstrap_ci(scores))These ten scores have a mean of 0.7, and the interval is wide, roughly 0.4 to 0.9. Ten cases cannot support a precise claim. The function also works on the per-case means from run_repeated, since it only needs one number per case. Resampling cases, not individual runs, is what keeps the case-selection noise in the interval.
Compare two versions with a paired test
To compare version A and version B, run both on the same cases. Then bootstrap the per-case differences instead of computing two separate intervals. A case that is hard for both versions drops out of the difference, so the pairing removes much of the noise.
import random
def paired_bootstrap_diff(scores_a, scores_b, n_resamples=1000, alpha=0.05, seed=0):
"""Confidence interval for mean(B) - mean(A) over the same cases."""
if len(scores_a) != len(scores_b):
raise ValueError("scores must cover the same cases in the same order")
rng = random.Random(seed)
diffs = [b - a for a, b in zip(scores_a, scores_b)]
n = len(diffs)
means = sorted(
sum(rng.choices(diffs, k=n)) / n for _ in range(n_resamples)
)
lo = means[int((alpha / 2) * n_resamples)]
hi = means[int((1 - alpha / 2) * n_resamples) - 1]
return sum(diffs) / n, (lo, hi)
a = [1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1]
b = [1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1]
mean_diff, (lo, hi) = paired_bootstrap_diff(a, b)
print(f"B - A = {mean_diff:+.2f}, 95% CI [{lo:+.2f}, {hi:+.2f}]")Read the result by looking at where the interval sits relative to zero:
| Interval for B − A | Reading |
|---|---|
| Entirely above 0 | B is better, beyond the noise you measured |
| Contains 0 | The data cannot separate A from B |
| Entirely below 0 | B is worse |
"Contains 0" does not mean "the same". It means you do not have enough evidence. A wide interval is a prompt to add cases or repeats, not a verdict.
Decision rules and common mistakes
Decide the rule before you look at the numbers. Examples are "ship B if the lower bound of B − A is above 0" for an improvement, or "ship if the lower bound is above −0.02" for a change that should not hurt quality, such as a cheaper model. A rule fixed in advance keeps you from reading the result however you want.
Common errors:
- Reading small gaps as real. A jump from 0.70 to 0.73 on 50 cases is well inside the noise.
- Comparing runs on different cases. If the test set changed between versions, the pairing is gone and the comparison is not valid.
- Trying many variants and keeping the best. If you test 20 prompt variants, one will look good by chance. Hold out a second set of cases and confirm the winner there.
- Treating repeated runs as independent cases. This makes the interval too narrow, as noted above.
- Ignoring judge noise. If the judge disagrees with itself on repeated calls, that variance is part of your measurement. Measure it by re-judging the same outputs a few times.
- Reporting only the mean. Also report
n,k, the model version, and the interval, so someone else can judge the claim.
Reporting template
A useful summary line looks like this:
Version B: 0.78 pass rate (95% CI 0.70 to 0.85), n=120 cases, k=3 runs, model-2026-05-01, judge temp 0
Versus A: +0.06 (95% CI +0.01 to +0.11), paired on the same 120 casesIt states the estimate, the uncertainty, the sample size, and the configuration. A reader can see both the gain and how much to trust it.
Exercise
Version A passes 60 of 100 cases. Version B passes 66 of the same 100 cases. A colleague says B is better because 66 > 60. Using the standard error formula, explain why that conclusion is not yet supported, and name two ways to strengthen it.
Show answer
The standard error of B's pass rate is sqrt(0.66 × 0.34 / 100) ≈ 0.047, so its 95% interval is about ±0.09 (0.57 to 0.75). A's interval is similar (about 0.50 to 0.70). The two intervals overlap heavily, and a 6-point gap is smaller than the noise at n = 100. To strengthen the claim, you could (1) run a paired bootstrap on the per-case differences and check whether the interval for B − A excludes 0, and (2) add more cases, since reaching about ±0.05 at this pass rate takes roughly 350 cases, or repeat each case several times to reduce run-to-run noise. Both help most when the cases are the same for the two versions.