pass@k Unbiased Estimator
Background
For verifiable tasks (code synthesis, math), the standard LLM eval metric is pass@k: the probability that at least one of independent samples from the model is correct. Naively estimating it as "I sampled , did any pass?" is high-variance and wasteful. The HumanEval paper (Chen et al., 2021) introduced the unbiased estimator: sample candidates, count the number correct , and apply a single combinatorial formula.
Problem statement
Implement pass_at_k(n, c, k) returning the unbiased estimate:
This is the probability that at least one correct candidate is in a random size- subsample drawn without replacement from the candidates - the standard interview-grade derivation.
Input
n-int >= 1, total candidates sampled.c-int, with , number of correct candidates among the .k-int, with , the target for .
Output
floatin - the unbiased estimate.
Examples
Example 1 - hand-computed
Input: n=10, c=5, k=1
Output: 0.5
Explanation: .
Example 2 - all candidates wrong -> 0
Input: n=10, c=0, k=5
Output: 0.0
Example 3 - all candidates correct -> 1
Input: n=10, c=10, k=5
Output: 1.0
Example 4 - mostly wrong, large k makes it nearly certain to hit a correct one
Input: n=10, c=2, k=8
Output: 1 - C(8, 8) / C(10, 8) = 1 - 1/45 ~= 0.9778
Constraints
- ; raise
ValueErrorotherwise. - ; raise
ValueErrorotherwise. - When (you've subsampled more than the wrong-count), every size- subset must contain at least one correct - return exactly .
- Return a plain Python
float.
Notes
- Why the unbiased form. The naive on a single trial is unbiased only for . Drawing samples and computing the closed-form for many is far more sample-efficient than running fresh trials per .
- Numerical stability. For large (), the ratio of binomials underflows in naive arithmetic - compute it iteratively as to stay in .
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •reference n=10 c=3 k=2
- •example n=10 c=2 k=8
- •sample n=12 c=4 k=3