Estimate pass@k without bias

Why it matters HumanEval and every code model leaderboard reports pass@1 and pass@10 this way.

Code models are scored by pass@k: the chance that at least one of k samples passes the tests. With n samples of which c pass, the unbiased estimate is 1 - C(n - c, k) / C(n, k).

Write pass_at_k(n, c, k). Return 1.0 if n - c < k. Compute it as 1 - prod over i from n - c + 1 to n of (1 - k / i) to avoid huge binomials.

Signaturedef pass_at_k(n, c, k)

Example

pass_at_k(10, 3, 1) is 0.3.

Understand the idea

For code generation you often sample many programs and ask: if I picked k of them, what is the chance at least one passes the tests? With n samples of which c pass, the probability that all k picks fail is C(n - c, k) / C(n, k), so pass@k = 1 minus that. Worked example: n = 5, c = 2, k = 2. C(3, 2) = 3 and C(5, 2) = 10, so pass@2 = 1 - 3/10 = 0.7. The product form gives the same: i runs from n - c + 1 = 4 to 5, and (1 - 2/4) * (1 - 2/5) = 0.5 * 0.6 = 0.3, so 1 - 0.3 = 0.7. Rule: if n - c < k, any k picks must include a pass, so return 1.0. Otherwise return 1 - product over i from n - c + 1 to n of (1 - k / i). The product avoids huge binomial numbers. Watch the range bounds, an off-by-one gives a wrong answer. Used in: HumanEval and MBPP style coding benchmarks for LLMs.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks all 5.

  • Sample 1: examplepass_at_k(10, 3, 1)
  • Sample 2: k 5pass_at_k(20, 2, 5)
  • Test 3: all failpass_at_k(10, 0, 3)
  • Test 4: guaranteedpass_at_k(5, 4, 2)
  • Test 5: big npass_at_k(200, 13, 10)