Code Lab / Challenge 12

Eval Metrics: pass@k, QA F1, Model-Graded Harness

Mediumpass@k unbiased estimator, QA F1/EM, judge harness skeleton

The problem, as an interviewer would set it

Implement three evaluation building blocks:

1. pass_at_k(n, c, k) -> float
   Unbiased estimator of pass@k for code generation (Chen et al., 2021,
   "Evaluating Large Language Models Trained on Code"): given n samples of
   which c passed, estimate P(at least one of k random samples passes):
       pass@k = 1 - C(n-c, k) / C(n, k)
   Compute it in the numerically stable product form
       1 - prod_{i=n-c+1..n} (1 - k/i)
   never materializing huge binomial coefficients.

2. SQuAD-style QA metrics:
       normalize_answer(s) - lowercase, strip punctuation, drop articles
                              (a/an/the), collapse whitespace
       exact_match(pred, gold) -> float   (0.0 or 1.0, on normalized text)
       token_f1(pred, gold) -> float      (multiset token overlap F1;
                              if either normalized side is empty, EM instead)

3. A minimal model-graded (LLM-as-judge) pairwise harness:
       pairwise_eval(judge, examples, rubric) -> EvalReport
   - judge is an injected callable: judge(prompt: str) -> str
   - for each example, build a judge prompt from the rubric and BOTH answer
     orders (position swap), parse the two verdicts, and score:
     consistent winner -> win; both TIE -> tie; disagreement -> inconsistent
   - report wins_a, wins_b, ties, inconsistent, and win_rate_a
     (ties and inconsistent count 0.5).
What interviewers look for

Why the naive pass@k estimator is biased, token-level F1 details, judge calibration hooks

A strong solution demonstrates:
- WHY the alternatives are worse. Drawing exactly k samples and checking for
  a pass is unbiased but high-variance and wastes the other n - k samples.
  The plug-in 1 - (1 - c/n)**k uses all n but is biased: it systematically
  UNDERESTIMATES pass@k (Chen et al., appendix A). The combinatorial form is
  unbiased and uses every sample.
- Numerical maturity: C(200, 100) ~ 9e58 still fits in float64, but
  C(2000, 1000) ~ 2e600 overflows it (float64 max ~1.8e308), so a
  numpy/scipy.special.comb version returns inf / inf = nan. Python's exact
  ints hide the problem in tests; the product form never leaves [0, 1].
- Knowing the official SQuAD normalization (lowercase, strip punctuation,
  drop articles, collapse whitespace) by heart. A homegrown F1 that skips a
  step scores differently from the official script, so its numbers cannot be
  compared with published baselines.
- Position bias is the best-documented failure mode of pairwise LLM judges;
  swapping answer order and requiring agreement is the standard mitigation.
  Verbosity bias and self-preference (a judge favouring its own model
  family) need separate controls.
Common mistakes: pass@k returning >1 or negative for edge cases (c=0, c=n,
n-c<k); F1 using set intersection instead of multiset (double-counted tokens);
judge harness trusting a single ordering; parsing free-text judge output
with strict equality (tolerant matching helps, a schema-constrained verdict
field removes the problem); silently scoring an unparseable verdict as a tie
or a loss instead of surfacing it.
Follow-ups: confidence intervals on pass@k via bootstrap over tasks;
length-bias controls for judges; Bradley-Terry (rather than online Elo)
aggregation across many model pairs, which is order-independent; agreement
metrics (Cohen's kappa) between judge and humans.

Write your own version first. The reference solution and its tests are one click away when you are ready.

Next challenge