The problem
How do you measure how good a code model is? Run the code it writes against unit tests: a program counts as correct only if it passes. But a model can write a different program each time, so one sample says little. pass@k is the probability that at least one of generated programs passes the tests. You will implement the estimator of Chen et al. (2021).
Estimating pass@k from exactly samples is noisy. So draw samples, count the that pass, and compute
where (" choose ") is the number of ways to pick items out of . The fraction is the probability that a random group of samples is all wrong: groups of among the wrong samples, out of groups in all. One minus it is the probability that at least one is right. This estimator has no bias.
Example. samples, pass.
- : : the share of correct samples.
- : .
- , : only wrong samples, so no group of 5 can be all wrong: pass@5 .
- Over two problems, one with and one with (pass@5 ), the mean pass@5 is .
The stable form
The binomials overflow for in the hundreds ( is about , and for larger it exceeds what a float can hold). The same number, as a product:
Every factor is between 0 and 1, so nothing overflows. (If the answer is 1, as in the example; the product form would hit a negative factor.)
The biased shortcut
It is tempting to estimate and compute . It is biased: it treats the samples as independent draws with replacement.
Example. , , : drawing both samples certainly includes the correct one, so pass@2 . The shortcut gives .
What to implement
pass_at_k(n, c, k): the estimator for one problem, as a float. If return 1.0; otherwise the product form.mean_pass_at_k(ns, cs, k): the mean over problems (arrays of and , one entry per problem).naive_pass_at_k(n, c, k): the biased shortcut, so that the tests can compare.
Requirements
- Raise
ValueErrorif .