Lab 4 of 4about 45 minutes
An A/B test of two prompts
It uses what you learned in Confidence intervals and the bootstrap and A/B tests and p-values.
Start here
Run itA chatbot tried two prompts for 20 days. Each user was shown prompt A or prompt B at random, and gave its answer a thumbs-up (1) or not (0). a and b hold each prompt's users, one result each.
1The test
Run itFrom Confidence intervals and the bootstrap
Run the cell. It prints the first users, then each prompt's users, thumbs-up and rate.
B is ahead, 12.9% against 9.8%: about three points. Real, or luck?
2Each prompt's error bar
Your turnFrom Confidence intervals and the bootstrap
Write rate_draws: draw the prompt's users again with repeats, 10,000 times, each draw as many as there are users, and give back each draw's rate. The cell then cuts the middle 95% of each prompt's rates, and SciPy's stats.bootstrap checks A's.
The two bars overlap, from about 10.8% to 11.6%. Read that as "no difference", and you'd be wrong: the gap needs its own bar.
Hint
rng.choice(results, size=(10_000, len(results))) makes the draws, a row each. Then each row's average.
One way to do it
def rate_draws(results, rng):
draws = rng.choice(results, size=(10_000, len(results)))
return draws.mean(axis=1)
rng = np.random.default_rng(0)
draws_a, draws_b = rate_draws(a, rng), rate_draws(b, rng)
bar_a, bar_b = np.percentile(draws_a, [2.5, 97.5]), np.percentile(draws_b, [2.5, 97.5])
print(bar_a.round(3), bar_b.round(3))
by_scipy = stats.bootstrap((a,), np.mean, rng=rng).confidence_interval
print(round(by_scipy.low, 3), round(by_scipy.high, 3))
for row, (rate, bar) in enumerate([(a.mean(), bar_a), (b.mean(), bar_b)]):
plt.plot(bar, [row, row])
plt.plot(rate, row, "o")
plt.yticks([0, 1], ["A", "B"])3The gap's own bar
Your turnFrom Confidence intervals and the bootstrap and A/B tests and p-values
In prob-confidence-intervals, both models took the same questions, so one draw of questions scored both. Here each user saw one prompt, so each group was drawn on its own. Each draw's gap is B's rate minus A's. Keep the gaps, then their middle 95%.
Hint
Take one array of rates from the other. The middle 95% is the same np.percentile as in step 2.
One way to do it
gaps = draws_b - draws_a
gap_bar = np.percentile(gaps, [2.5, 97.5])
print(round(b.mean() - a.mean(), 3), gap_bar.round(3))4The shuffle test
Your turnSuppose the two prompts are equally good: then which user saw which is only luck. Deal the users out again 10,000 times, as many to A as before, and keep each gap. The p-value is the share of shuffles with a gap as big as the real one, either way.
SciPy's shuffle agrees: p is about 0.03. That's not the chance B is no better. It's how often luck alone, with the prompts equal, makes a gap this big.
Hint
rng.permutation(pooled) deals them out again. The first len(a) play A, the rest play B.
One way to do it
def gap(x, y):
return y.mean() - x.mean()
rng = np.random.default_rng(0)
pooled = np.concatenate([a, b])
real_gap = gap(a, b)
shuffled = np.empty(10_000)
for i in range(10_000):
dealt = rng.permutation(pooled)
shuffled[i] = dealt[len(a):].mean() - dealt[:len(a)].mean()
p_value = (np.abs(shuffled) >= abs(real_gap)).mean()
res = stats.permutation_test((a, b), gap, rng=rng)
print(round(p_value, 3), round(res.pvalue, 3))5The test, day by day
Run itWith a thousand users, the shuffled gaps pile into a bell, so the share past the real gap can be read off the bell, with no shuffling. bell_p does that, from each prompt's thumbs-up and users. Run the cell: it adds each day's users to the days before, and draws the p, day by day.
On day 20 the bell gives 0.03, as the shuffles did. And see how far p wandered on the way, where B really is better.
6A copy, checked every day
On your ownOn your own now. Run 1,000 tests where B is a copy of A: both at a 10% thumbs-up rate, 100 users a prompt a day, for 20 days. rng.binomial(100, 0.1, (1_000, 20)) gives each test's thumbs-up, day by day. Keep in every_day the share of tests whose p falls under 0.05 on some day, and in once the share under 0.05 on day 20 alone.
Hint
.cumsum(axis=1) adds each day to the days before, as in step 5. .any(axis=1) asks whether any day in a test's row is True.
One way to do it
rng = np.random.default_rng(0)
thumbs_a = rng.binomial(100, 0.1, (1_000, 20)).cumsum(axis=1)
thumbs_b = rng.binomial(100, 0.1, (1_000, 20)).cumsum(axis=1)
seen = 100 * np.arange(1, 21)
copy_p = bell_p(thumbs_a, seen, thumbs_b, seen)
every_day = (copy_p < 0.05).any(axis=1).mean()
once = (copy_p[:, -1] < 0.05).mean()
print(every_day, once)7A second test
On your ownFrom Confidence intervals and the bootstrap and A/B tests and p-values
On your own. second_test.csv holds two other prompts, tried on 600 users. Read it: keep its p-value from SciPy's shuffle in second_p, the gap's bar in second_bar, and in settled whether the test can tell the two apart, True or False.
Hint
Steps 2 to 4 on the new file: the gap's bar with rate_draws, then SciPy's shuffle. Not significant means the test can't tell yet, not that the two are the same.
One way to do it
second = pd.read_csv("second_test.csv")
a2 = second[second.prompt == "A"].thumbs_up.to_numpy()
b2 = second[second.prompt == "B"].thumbs_up.to_numpy()
rng = np.random.default_rng(0)
second_p = stats.permutation_test((a2, b2), gap, rng=rng).pvalue
second_bar = np.percentile(rate_draws(b2, rng) - rate_draws(a2, rng), [2.5, 97.5])
settled = bool(second_p < 0.05 and second_bar[0] > 0)
print(round(b2.mean() - a2.mean(), 3), round(second_p, 2), second_bar.round(3), settled)Run on Pyodide 314, Python 3.14, NumPy 2.4, pandas 3.0, matplotlib 3.10 and SciPy 1.18. Checked on .
