llm-judge-stats
Statistics for turning LLM eval results into conclusions you can defend, or into
a clear statement that the data cannot support one. Small API, NumPy + SciPy, no
LLM frameworks.
The usual practice: compare two prompts on 40 examples, see a few points of
difference, declare a winner. No confidence interval, no power calculation, no
correction for having tried six prompts. This package answers four questions:
- Before running: how many items do I need to detect the difference I care about?
- After running: is the difference I see distinguishable from noise?
- About the judge: how much does the judge agree with humans, and how much
uncertainty does its disagreement add to my metric?
- For the report: what can I honestly say in one paragraph?
pip install llm-judge-stats
The example that hurts
Two prompt variants with the same true pass rate, run on the same 40 items
(examples/small_n.py; the seed is the first one where B leads by 3 items):
A passes 28/40 (70.0%), B passes 31/40 (77.5%)
gap: +7.5 points
It looks like B wins. The paired analysis says:
from llm_judge_stats import compare
print(compare(passed_a, passed_b, min_relevant=0.05))
B vs A: +7.5 points (70.0% -> 77.5%), 95% CI -7.5 to +21.7, 40 paired items.
Mid-p McNemar test p = 0.344; Cohen's h = 0.17.
Not distinguishable from noise at alpha = 0.05.
To detect a 5-point difference with 80% power you would need about 705 paired items.
In 10,000 runs of the same setup, with no real difference between the variants,
49.4% show a gap of 7.5 points or more in one direction or the other.
How many items do you need?
Base pass rate 70%, the two variants disagree on 20% of items, two-sided
alpha = 0.05 (examples/planning_table.py). Paired = both variants run on the
same items, which is the normal case and roughly halves the items needed.
| Difference to detect |
paired, power 0.80 |
paired, 0.90 |
paired, 0.95 |
unpaired, 0.80 (per variant) |
| 2 points |
3923 |
5250 |
6492 |
8080 |
| 5 points |
626 |
837 |
1034 |
1251 |
| 10 points |
155 |
206 |
254 |
294 |
| 15 points |
68 |
90 |
110 |
121 |
The discordance (share of items where exactly one variant passes) drives the
paired numbers more than the pass rates do. Measure it on a pilot. The table
holds it at 20% for every row.
The reverse question, given the items you already have:
from llm_judge_stats import smallest_detectable_difference
smallest_detectable_difference([50, 100, 200, 500, 1000, 2000], p_base=0.70, discordance=0.2)
| Items |
50 |
100 |
200 |
500 |
1000 |
2000 |
| Smallest detectable difference (power 0.80) |
17.3 pts |
12.4 |
8.8 |
5.6 |
4.0 |
2.8 |
API
1. Planning (run before the eval)
| Function |
What it does |
n_for_proportions(p_a, p_b, power=0.8, alpha=0.05, discordance=None) |
Items needed to detect a pass-rate difference. Paired (McNemar, Connor 1987) when discordance is given, unpaired (items per variant) otherwise |
n_for_noninferiority(margin, discordance, true_diff=0.0, power=0.8, alpha=0.025) |
Paired items needed for noninferiority to succeed on pass rates. Assuming the cheap variant is even 1 point worse raises n fast |
n_for_means(delta, sd, power=0.8, alpha=0.05, paired=True) |
Same for continuous judge scores, exact via the noncentral t. For paired data sd is the SD of per-item differences |
smallest_detectable_difference(n, ...) |
The inverse: the smallest difference a given N detects, in either direction. Accepts an array of N for a detectability curve |
n_for_kappa(kappa, prevalence, half_width) |
Items to label so a binary kappa's CI has a target width (Bloch & Kraemer 1989) |
n_for_precision(stat, *pilot, half_width=...) |
Same for any statistic (F1, MCC, ICC, ...), extrapolated from a pilot |
2. Analysing an eval
| Function |
What it does |
compare(a, b, paired=True, alpha=0.05, min_relevant=None) |
Difference, CI, p-value and effect size for two variants. Paired: mid-p McNemar + Agresti-Min CI for 0/1 data, paired t otherwise. Unpaired: two-proportion z + Agresti-Caffo CI, or Welch t. Returns a Comparison; print() gives the verdict |
compare_many(scores, baseline, correction="holm") |
k variants against a baseline, p-values adjusted by Holm (FWER) or Benjamini-Hochberg (FDR) |
noninferiority(a, b, margin, paired=True, alpha=0.025) |
Is the cheaper variant b at most margin worse than a? Non-inferior only if the lower limit of the 95% CI of b - a is above -margin. Verdicts: non-inferior, not shown, inferior |
equivalence(a, b, margin, paired=True, alpha=0.025) |
Is b within +-margin of a? Two one-sided tests (TOST) on the same CI |
adjust_pvalues(p, method="holm") |
Holm or Benjamini-Hochberg adjustment on its own |
bootstrap_ci(stat, *data, method="percentile", strata=None) |
Bootstrap CI over items for any metric; method="bca" for skewed statistics |
Effect size is always reported next to the p-value: Cohen's d_z (paired) or d
(unpaired) for scores, Cohen's h for pass rates.
When the difference is not distinguishable, the verdict says how many items
would detect min_relevant (the smallest difference that matters to you). Without
it, the verdict gives the smallest difference the current N can detect.
Planning on the observed difference ("post-hoc power") is circular, so the
package does not do it.
from llm_judge_stats import compare_many
results = compare_many({"base": base, "v1": v1, "v2": v2, "v3": v3}, baseline="base")
print(*results, sep="\n\n")
"No significant difference" is not evidence that a cheaper model is as good: a
small eval finds no difference between anything. noninferiority reverses the
burden of proof, as clinical trials do (ICH E9, E10). The margin is fixed
before the eval runs:
from llm_judge_stats import noninferiority
print(noninferiority(large, small, margin=0.04, names=("large", "small")))
# small vs large: -4.0 points, 95% CI -9.5 to +1.6, margin 4 points.
# Non-inferiority not shown (one-sided p = 0.494, alpha = 0.025).
# The CI rules out a loss larger than 9.51 points, which is beyond the margin: the eval is too small to decide.
3. Judge quality
| Function |
What it does |
cohen_kappa(a, b) |
Agreement between two raters (any categories) |
fleiss_kappa(ratings) |
Agreement among m raters, ratings is (items, raters) |
krippendorff_alpha(ratings, level="nominal") |
Agreement with missing ratings allowed (None/NaN); level nominal, ordinal or interval |
icc(scores, kind="2,1") |
Shrout & Fleiss ICC for numeric scores |
spearman(a, b) |
Rank correlation between judge and human scores |
f1, mcc |
Binary F1 / Matthews correlation vs. gold |
judge_adjusted_rate(judge_labels, calib_judge, calib_human) |
Pass rate corrected for the judge's sensitivity and specificity, measured on a human-labelled calibration set (Rogan & Gladen 1978). The CI bootstraps both sets, and the summary shows how much each source widens it |
shuffled_judge(judge, options, rng) |
Shows options in random order, maps the pick back (counters position bias) |
majority_vote(votes, tie=None) |
Aggregates repeated judge samples or several annotators |
Any agreement statistic gets a CI through bootstrap_ci:
bootstrap_ci(cohen_kappa, judge_labels, human_labels) # (est, lo, hi)
bootstrap_ci(f1, gold, pred, strata=gold) # keeps class balance fixed
4. Report
print() on a compare or compare_many result gives the verdict: estimated
difference, CI, test, effect size, whether it is distinguishable from noise, and
what more data would buy. print() on a judge_adjusted_rate result gives the
corrected rate and where its uncertainty comes from (example below).
Example: GPT-4 vs. human experts on MT-Bench
uv run --extra example python examples/mt_bench.py
Uses the public lmsys/mt_bench_human_judgments dataset (CC-BY-4.0):
GPT-4 position-inconsistent verdicts: 15.8%
items judged by both: 1814
raw agreement GPT-4 vs human majority: 67.2%
Cohen's kappa GPT-4 vs human majority: 0.505 [95% CI 0.472, 0.537]
same items, pairs in the order GPT-4 saw them: 0.380 [0.341, 0.419]
Fleiss' kappa human-human (961 items): 0.503 [0.458, 0.549]
F1 (model_a wins, 1078 decisive items): 0.891 [0.872, 0.909]
MCC (model_a wins, 1078 decisive items): 0.767 [0.725, 0.804]
items for kappa +-0.05, from this pilot: 753
items for binary kappa=0.6, prevalence=0.3, +-0.05 (planning formula): 1181
Krippendorff's alpha human-human, all votes (961 items, up to 7 raters): 0.485 [0.444, 0.524]
GPT-4 agrees with the human majority about as much as two humans agree with
each other (kappa 0.505 against 0.503).
Kappa depends on how the labels are laid out. The verdicts are model_a,
model_b or tie, and which model is called model_a is arbitrary. With pairs
in alphabetical order the labels are balanced. In the order GPT-4 saw them,
model_b is usually the stronger model, the labels are unbalanced, and kappa
falls from 0.505 to 0.380 on the same items with the same 67.2% raw agreement.
Compare two kappas only when both use the same layout.
Versions up to 0.2.0 of this example reported kappa = 0.410 on 1,232 items. They
matched human and GPT-4 rows without putting the pairs in one order first, which
dropped a third of the items and left the rest in GPT-4's order. That figure was
then set against a human-human kappa computed on balanced labels, which is not a
like-for-like comparison.
Correcting a judge-measured rate. This part uses the order GPT-4 saw the pairs,
where model_a rarely wins and GPT-4 misses many of those wins. On the 1,078
decisive items, a random half plays the human-labelled calibration set, and on
the other half only GPT-4's verdict is used. The human answer for that half is
known, so we can check whether the correction lands on it:
Judge pass rate: 9.1% (95% CI 6.7% to 11.4%), 563 items.
Judge vs. human on 515 calibration items: sensitivity 0.47, specificity 0.98.
Adjusted pass rate: 15.8% (95% CI 9.3% to 23.9%).
CI width: 4.6 points if the judge were perfect, 10.1 after correcting for its error rates, 14.6 also counting the uncertainty in those rates (3.2x).
human rate on the GPT-4-only half (the target): 16.2%
GPT-4 misses more than half of the human "model_a wins" verdicts, so its raw
rate (9.1%) is far from the human one (16.2%), and its 95% CI excludes it.
The corrected rate covers the human value, but the honest interval is 3.2 times
wider than the naive one. That is what this judge costs. This is one split of
one dataset. The simulations below check that the CI coverage holds in general.
With only 20 to 50 calibration labels this correction is unreliable. See
bayesian-judge-calibration.
Does it do what it promises?
examples/simulations.py (about 35 s) checks each method on simulated data
where the truth is known. Each rate comes with its Monte Carlo SE. The test
suite checks the same properties with fewer runs.
compare() on null data (no real difference), alpha = 0.05, 4000 runs each: