Agentic Pattern Libraryby Vikas Goyal
Patterns / Math models / Confidence intervals
No. M06 Evaluation Established Verification

Confidence intervals for evals

Wilson intervals, sample sizes and paired comparisons

Takeaway

An eval of test cases scoring % only tells you the true rate is somewhere between 83.8% and 97.9%.

Change the blue numbers. Every figure on this page updates with them.

Why care

Every eval score is an estimate, and small eval sets give wide ones.

Teams change a prompt, rerun 50 test cases, see the score move from 88% to 94% and ship. On 50 cases, a six-point move is well within what chance alone produces. A confidence interval puts honest error bars on a score, a sample-size calculation tells you how many cases you need before a difference can show up, and a paired comparison, which scores both versions on the same cases, gets there with far fewer cases than comparing two separate scores.

The intuition

An eval set is a sample from all the requests your agent will ever see, like a poll is a sample of voters. A poll of 50 people saying 94% is far less certain than a poll of 1,000 saying the same. The uncertainty shrinks with the square root of the sample: four times the cases halves the error bar.

50 cases at 94%
±7
200 cases at 94%
±3.4
1,000 cases at 94%
±1.5

Bars show the 95% interval on a 0–100% scale.

Comparing two versions on the same cases is even better than doubling the sample, because most cases are passed or failed by both versions alike. Only the cases where they disagree carry information about which is better, and the comparison can focus on exactly those.

The math

For \(x\) successes out of \(n\) cases, observed rate \(\hat p = x/n\), the Wilson score interval is

$$\frac{\hat p + \frac{z^2}{2n}}{1+\frac{z^2}{n}} \;\pm\; \frac{z}{1+\frac{z^2}{n}}\sqrt{\frac{\hat p(1-\hat p)}{n} + \frac{z^2}{4n^2}}$$

To compare versions A and B on the same \(n\) cases, count only where they disagree: \(b\) cases A passes and B fails, \(c\) cases B passes and A fails. Then

$$\hat d = \frac{b-c}{n}, \qquad \text{SE}(\hat d) = \frac{1}{n}\sqrt{b+c-\frac{(b-c)^2}{n}}, \qquad p\text{-value} = \Pr\big(\text{Bin}(b+c, \tfrac12) \text{ as extreme as } b\big)$$
\(n,\ x,\ \hat p\)
number of test cases, successes, and observed rate
\(z\)
1.96 for 95% confidence (1.28 for 80%, 1.64 for 90%, 2.58 for 99%)
\(b,\ c\)
discordant cases: only A passes, only B passes
\(\hat d\)
observed difference in score, A minus B
\(p_{\text{disc}}\)
share of cases where the versions disagree, \((b+c)/n\)

Two sample-size rules cover most planning:

$$n_{\pm m} \approx \frac{z^2\,\hat p(1-\hat p)}{m^2} \qquad\qquad n_{\text{paired}} \approx \frac{\big(z_{\alpha}\sqrt{p_{\text{disc}}} + z_{\beta}\sqrt{p_{\text{disc}}-d^2}\big)^2}{d^2}$$
  • Margin: to pin a score near 90% to within ±3 points takes about 385 cases; ±2 points takes about 865.
  • Detecting a gap: the second formula gives the number of paired cases needed to detect a difference \(d\) with 80% power (\(z_\beta = 0.84\)) at 95% confidence (\(z_\alpha = 1.96\)).
  • Why Wilson: the textbook \(\hat p \pm z\sqrt{\hat p(1-\hat p)/n}\) collapses to zero width at 0% or 100% and is too narrow near the edges, which is exactly where good agents score.

Where each of these comes from is worked through in Derivations at the end of the page.

Interactive calculator

Put error bars on one score, or compare two versions scored on the same test set.

–
95% interval for the true score
–
Margin of error
–
Cases for ±3 points
–
Cases for ±2 points
Upper and lower bound at this score Your test set size

Worked example: a 6-point "win"

A team tuned the system prompt of an IT-helpdesk agent and reran their 50-case eval. Version A, the new prompt, scored 47/50 (94%); version B, the old one, 44/50 (88%). The Wilson intervals were 83.8–97.9% and 76.2–94.4%, which overlap heavily.

Because both versions ran on the same 50 cases, the better test is paired. They disagreed on 7 cases: A alone passed 5, B alone passed 2.

50 cases300 cases, same rates
Disagreements (only A passes / only B passes)5 / 230 / 12
Difference, A minus B+6.0 pts+6.0 pts
95% interval for the difference−4.2 to +16.2+1.8 to +10.2
McNemar exact p-value0.450.008

On 50 cases, a 7-case disagreement split 5 to 2 happens by chance almost half the time, so the result says nothing yet. The paired sample-size formula, with 14% disagreement and a 6-point gap, called for about 300 cases. The team expanded the eval to 300 cases, and the same rates now gave an interval that excludes zero. One caveat: had they tried ten prompt variants and kept the best, even \(p = 0.008\) would need adjusting for the extra chances (see below).

Assumptions and where it breaks

  • Test cases are assumed independent. Eval sets often contain clusters, such as ten variations of one scenario. Clustered cases carry less information than their count suggests. Use a cluster bootstrap that resamples whole scenarios.
  • Trying many variants inflates false wins. Test 20 prompts at 95% confidence and one will likely "win" by chance. Hold out a fresh test set for the final comparison, or tighten the threshold (for example, divide 0.05 by the number of variants).
  • The eval set is not the world. The interval covers sampling noise in the test set, not the gap between the test set and production traffic. A narrow interval on an unrepresentative set is precisely wrong.
  • Each run is itself random. With a nonzero temperature, the same case can pass on one run and fail on the next. Run each case several times (M05) or include run-to-run variance in a bootstrap.
  • Scores are not always pass or fail. For graded scores, use a bootstrap or a paired t-interval on per-case score differences instead.

How to apply it

  1. Report every score with its interval. "94% (84–98%, n=50)" invites the right level of trust.
  2. Size the eval before running it. Decide the smallest gap worth detecting and compute the paired \(n\). If it is larger than your set, grow the set or accept that small gains will be invisible.
  3. Always compare on the same cases. Paired comparisons need far fewer cases than comparing two separate scores.
  4. Look at the disagreements. The \(b\) and \(c\) cases are both the statistical evidence and the most useful cases to read.
  5. Keep a held-out set for decisions after many rounds of tuning on the main eval.

How to explain it to a stakeholder

"An eval is a poll of our future traffic. Fifty cases is a small poll: a six-point lead is inside the margin of error. We'll run the same 300 cases on both versions, and then the lead either holds or it doesn't."

For coding agents

A machine-readable summary and a reference implementation. The same spec is embedded in this page as <script type="application/json" id="model-spec">, and the whole series is listed in models.json.

Model spec (YAML)
id: confidence-intervals
number: M06
name: Confidence intervals for eval scores
family: evaluation
answers: "How precise is this eval score, and is version A really better than B?"
formula:
  wilson_interval: "(p + z^2/2n)/(1 + z^2/n) +- z/(1 + z^2/n) * sqrt(p(1-p)/n + z^2/4n^2)"
  paired_difference: "d = (b - c)/n,  se = sqrt(b + c - (b - c)^2/n)/n"
  mcnemar_exact: "two-sided binomial test of b against Bin(b + c, 0.5)"
  n_for_margin: "z^2 p (1-p) / m^2"
  n_paired_for_gap: "(z_a*sqrt(pd) + z_b*sqrt(pd - d^2))^2 / d^2"
inputs:
  n: {type: int, meaning: test cases}
  x: {type: int, meaning: successes}
  b: {type: int, meaning: cases only A passes}
  c: {type: int, meaning: cases only B passes}
assumptions:
  - test cases independent (use a cluster bootstrap otherwise)
  - binary scores (bootstrap for graded scores)
  - one planned comparison (adjust for many)
use_when:
  - reporting any eval score
  - deciding whether a prompt or model change is a real improvement
related_patterns: [llm-as-judge, learning-loop, pr-review-gate]
Reference implementation (Python)
import math
from math import comb

def wilson(x, n, z=1.96):
    p = x / n
    d = 1 + z * z / n
    centre = (p + z * z / (2 * n)) / d
    half = z / d * math.sqrt(p * (1 - p) / n + z * z / (4 * n * n))
    return centre - half, centre + half

def paired_difference(b, c, n, z=1.96):
    """b: only A passes, c: only B passes, n: total cases. Returns (diff, lo, hi, p)."""
    d = (b - c) / n
    se = math.sqrt(b + c - (b - c) ** 2 / n) / n
    m = b + c
    p = min(1.0, 2 * sum(comb(m, i) for i in range(min(b, c) + 1)) / 2 ** m) if m else 1.0
    return d, d - z * se, d + z * se, p

def n_paired(d, p_disc, z_a=1.96, z_b=0.8416):
    return math.ceil((z_a * math.sqrt(p_disc) + z_b * math.sqrt(p_disc - d * d)) ** 2 / d ** 2)

if __name__ == "__main__":
    print(wilson(47, 50))                    # (0.838, 0.979)
    print(paired_difference(5, 2, 50))       # (0.06, -0.042, 0.162, 0.453)
    print(paired_difference(30, 12, 300))    # (0.06, 0.018, 0.102, 0.008)
    print(n_paired(0.06, 0.14))              # 303

Derivations

For the math-curious: where each result in The math comes from. Nothing above depends on reading this.

The Wilson interval, by inverting a test

The number of successes is binomial, with mean \(np\) and variance \(np(1-p)\). A value \(p\) is plausible if the observed \(\hat p\) lies within \(z\) standard errors of it, using the standard error at \(p\) itself:

$$|\hat p - p| \le z\sqrt{p(1-p)/n}.$$

Square both sides and collect terms in \(p\): \((1+z^2/n)\,p^2 - (2\hat p + z^2/n)\,p + \hat p^2 \le 0\). The roots of this quadratic are the Wilson bounds. The textbook Wald interval instead plugs \(\hat p\) into the standard error, which gives zero width when \(\hat p\) is 0 or 1 and covers the truth less often than advertised near the edges.

Why paired comparisons need fewer cases

Score each case as \(D_i = A_i - B_i\), which is \(+1\) (only A passes), \(-1\) (only B passes) or 0. The difference in scores is the mean of the \(D_i\). Its variance is \(\operatorname{Var}(A) + \operatorname{Var}(B) - 2\operatorname{Cov}(A,B)\). Cases that are easy or hard for both versions make \(A\) and \(B\) strongly correlated, so the covariance term removes most of the noise. Computing the sample variance of \(D_i\) directly gives \(\big(b+c-(b-c)^2/n\big)/n^2\), the formula above.

McNemar's test

If A and B are equally good, then each disagreement is equally likely to favor either one. So, given \(m = b+c\) disagreements, \(b\) follows a binomial with \(m\) trials and probability \(\tfrac12\). The exact two-sided p-value is twice the probability of a split at least as lopsided as the one observed: \(2\sum_{i\le\min(b,c)}\binom{m}{i}/2^m\). With 5 to 2, that is \(2(1+7+21)/128 = 0.45\).

Sample-size formulas

Margin: set the half-width \(z\sqrt{p(1-p)/n}\) equal to \(m\) and solve for \(n\). The largest \(n\) is needed at \(p = 0.5\); scores near 90% need about a third as many cases.

Paired gap: under the null hypothesis, \(\hat d\) has standard deviation \(\sqrt{p_{\text{disc}}/n}\); under the alternative, \(\sqrt{(p_{\text{disc}}-d^2)/n}\). To reject with 80% power, the true gap \(d\) must exceed \(z_\alpha\) null standard deviations plus \(z_\beta\) alternative ones: \(d\sqrt n = z_\alpha\sqrt{p_{\text{disc}}} + z_\beta\sqrt{p_{\text{disc}}-d^2}\). Squaring gives the formula.

References

  1. Miller, E. (2024). Adding Error Bars to Evals: A Statistical Approach to Language Model Evaluations. Clustered standard errors, paired differences and power analysis for LLM evals.
  2. Agresti, A. and Coull, B. A. (1998). Approximate is better than "exact" for interval estimation of binomial proportions. The American Statistician 52(2), 119–126. Why Wilson-style intervals beat the textbook interval.
  3. Wilson, E. B. (1927). Probable inference, the law of succession, and statistical inference. Journal of the American Statistical Association 22(158), 209–212.