Sub topic 10
Handling nondeterminism and reporting uncertainty
This chapter assumes you can write basic Python, have a working eval harness like the one from the earlier chapter, and know what a mean, a proportion, and variance are.
Why one run is not enough
An LLM application can give different outputs for the same input. Sampling temperature, provider-side changes, and retrieval ties all add variation. Your test set is also only a sample of the inputs you care about.
So a pass rate is an estimate, not a fact. Suppose version A scores 0.80 and version B scores 0.84 on 50 cases. That gap is two cases. It may be noise.
Two sources of noise
- Sampling noise in the model. The same case can pass on one run and fail on the next.
- Sampling noise in the dataset. A different set of 50 cases would give a different score.
You can reduce the first by lowering temperature or repeating runs. The second only shrinks when you add cases, or when you account for it with an interval.
Control what you can
Set temperature to 0 where the task allows it, and pin the model version. Fix random seeds for any code you control. Even so, some providers do not guarantee identical outputs at temperature 0, so do not assume determinism. Treat it as reduced variance.
Where variation matters, run each case several times and average the results per case. Then treat each case's averaged score as one data point.
Report an interval
A percentile bootstrap resamples the per-case scores with replacement. It computes the mean of each resample and takes the middle 95% of those 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))The mean here is 0.7, but with only 10 cases the interval is wide. That width is the useful information. It tells you how much to trust the number.
Compare two versions
Do not compare two separate intervals by eye. Both versions run on the same cases, so use a paired comparison. Compute the per-case difference, then bootstrap those differences.
diffs = [b - a for a, b in zip([1, 0, 1, 1, 0, 1, 1, 1, 0, 1],
[1, 1, 1, 1, 0, 1, 1, 1, 1, 1])]
print(bootstrap_ci(diffs))If the interval contains 0, you cannot claim B is better. Pairing removes the variation from case difficulty, so it detects real changes more easily than two independent intervals.
Common errors
- Reading small gaps as progress. With a few dozen cases, differences of several points are often noise.
- Treating repeated runs as more cases. Running the same 20 cases 10 times does not give 200 independent cases. Average within a case first.
- Changing the dataset between versions. Always compare on identical cases.
- Forgetting the seed. Fix the bootstrap seed so reports are reproducible.
Exercise
Version A and version B both run on the same 40 cases. B's mean score is 0.05 higher, and the bootstrap interval of the per-case differences is (-0.03, 0.13). What should you report, and what would you do next?
Show answer
Report that B is 0.05 higher on average but the interval includes 0, so the data does not show a reliable improvement. Next, add more cases (or repeat runs and average per case) to narrow the interval, then compare again.