Gradient LabOpen in Gradient Lab

Lessons › Projects

Human evaluation toolkit

The calculations behind Chapter 15: win rates with confidence intervals, a position-bias check, Cohen’s kappa and rater normalization.

No setup needed. Run every step in your browser on Gradient Lab, or download it.

Open the notebook

Level: Intermediate · Time: 30 min · You’ll practise: win rate, bootstrap intervals, position bias, Cohen’s kappa, z-scores

Steps

1. 300 side-by-side comparisons of chatbot A (old) vs B

Simulate 300 side-by-side comparisons of an old and a new chatbot, with sides randomized, and save them as pairwise.csv.

n = 300
b_on_left = rng.random(n) < 0.5                    # we randomized which side B was shown on
true_pref_b = rng.random(n) < 0.62                 # B is genuinely better most of the time
left_bias = rng.random(n) < 0.08                   # some raters just pick the left answer
picked_left = np.where(left_bias, True, np.where(b_on_left, true_pref_b, ~true_pref_b))
tie = rng.random(n) < 0.07
winner = np.where(tie, "tie", np.where(picked_left == b_on_left, "B", "A"))
pairs = pd.DataFrame({"b_on_left": b_on_left, "winner": winner})
pairs.to_csv("pairwise.csv", index=False)

2. Win rate with a 95% bootstrap confidence interval

Compute the new version’s win rate (ties count half) and a 95% bootstrap confidence interval.

def win_rate(w):
    return (w == "B").mean() + 0.5 * (w == "tie").mean()      # ties count half
boot = [win_rate(pairs.winner.sample(n, replace=True, random_state=i).to_numpy()) for i in range(1000)]
low, high = np.percentile(boot, [2.5, 97.5])
print(f"B win rate: {win_rate(pairs.winner.to_numpy()):.0%}  (95% interval {low:.0%} to {high:.0%})")
print("If the whole interval is above 50%, B is better beyond reasonable doubt.")

3. Position bias: how often does the LEFT answer win?

Check position bias: how often does whichever answer was on the left win?

non_tie = pairs[pairs.winner != "tie"]
left_wins = ((non_tie.winner == "B") == non_tie.b_on_left).mean()
print(f"Left-side answer wins {left_wins:.0%} of the time (50% would be no position bias)")

4. Two raters label 200 answers safe (1) or unsafe

Two raters label 200 answers safe or unsafe. Compare raw agreement with Cohen’s kappa.

truth = (rng.random(200) < 0.8).astype(int)
rater1 = np.where(rng.random(200) < 0.9, truth, 1 - truth)
rater2 = np.where(rng.random(200) < 0.85, truth, 1 - truth)
agree = (rater1 == rater2).mean()
print(f"\nRaw agreement: {agree:.0%}   Cohen's kappa: {cohen_kappa_score(rater1, rater2):.2f}")
print("Kappa removes the agreement you'd get by luck; above ~0.6 is usually seen as decent.")

5. Strict vs generous raters on a 1-5 scale, then normalize each rater

Fix strict and generous raters by normalizing each rater’s scores.

quality = rng.integers(1, 6, 50)
ratings = pd.DataFrame({
    "priya (strict)": (quality - 1).clip(1, 5),
    "sam (generous)": (quality + 1).clip(1, 5),
    "lee": quality,
})
print("\nAverage score per rater:", ratings.mean().round(2).to_dict())
z = (ratings - ratings.mean()) / ratings.std()
print("After normalizing each rater (z-scores), averages:", (z.mean().round(2) + 0.0).to_dict())
print("Correlation between raters after normalizing:", round(z.corr().values[np.triu_indices(3, 1)].mean(), 2))

Expected output

$ python human_eval.py
B win rate: 62%  (95% interval 57% to 67%)
If the whole interval is above 50%, B is better beyond reasonable doubt.
Left-side answer wins 56% of the time (50% would be no position bias)

Raw agreement: 77%   Cohen's kappa: 0.45
Kappa removes the agreement you'd get by luck; above ~0.6 is usually seen as decent.

Average score per rater: {'priya (strict)': 2.2, 'sam (generous)': 3.84, 'lee': 3.02}
After normalizing each rater (z-scores), averages: {'priya (strict)': 0.0, 'sam (generous)': 0.0, 'lee': 0.0}
Correlation between raters after normalizing: 0.95

Stretch goals

Download p11-human-eval.zip