A/B Testing for Machine Learning
While A/B testing is a general experimentation technique, it plays a specific and critical role in machine learning: validating that a new model actually performs better than the current one in the real world, not just on a static test set.
Why Offline Metrics Aren't Enough
A model can score well on offline metrics like accuracy or RMSE but still fail to improve real business outcomes once deployed, due to shifts in live data or downstream effects not captured in historical data. A/B testing closes that gap.
Typical ML A/B Test Setup
| Group | What They See |
|---|---|
| Control | Predictions from the current production model |
| Treatment | Predictions from the new candidate model |
Traffic is randomly split between the two, and a business metric (click-through rate, conversion, revenue) is compared between groups using a statistical significance test.
Best Practices for ML A/B Testing
- Pick One Primary Metric: Avoid overloading the test with too many success criteria.
- Run Long Enough: Capture enough traffic and time to reach statistical power.
- Monitor Guardrail Metrics: Ensure the new model doesn't hurt other important metrics.
- Shadow Testing First: Run the new model silently alongside production before a live A/B test.
Common Pitfalls
- Comparing models trained on different time windows, introducing bias.
- Stopping the test too early once results look favorable.
- Ignoring interaction effects between the new model and other running experiments.
Planning the Experiment Before Launch
A model A/B test should be designed on paper before any traffic moves. Writing the plan first stops people from changing the goal after they see the numbers.
- State the hypothesis: "The new ranking model raises click-through rate on the home feed."
- Pick the randomisation unit: usually the user, so one person does not bounce between models and get a mixed experience.
- Set the minimum detectable effect: the smallest lift that would be worth shipping.
- Estimate sample size from the baseline rate, that effect, the significance level and the desired power.
- Fix the duration to cover full weekly cycles, since behaviour on weekends often differs.
from statsmodels.stats.power import NormalIndPower
from statsmodels.stats.proportion import proportion_effectsize
baseline = 0.040 # current click-through rate
target = 0.042 # smallest rate worth shipping
effect = proportion_effectsize(target, baseline)
n_per_group = NormalIndPower().solve_power(effect, alpha=0.05, power=0.8)
print(round(n_per_group)) # users needed in each group
Reading the Results Correctly
Suppose control shows 4,000 clicks from 100,000 users and treatment shows 4,300 clicks from 100,000 users. A two-proportion z-test tells you whether that gap is likely to be noise.
from statsmodels.stats.proportion import proportions_ztest
clicks = [4300, 4000]
users = [100000, 100000]
stat, p_value = proportions_ztest(clicks, users)
print(f"z = {stat:.2f}, p = {p_value:.4f}")
A small p-value suggests a real difference, but also report the confidence interval of the lift. "Between a small and a moderate gain" helps decision makers more than a single number.
Before trusting any result, run a sample ratio mismatch check. If you planned a 50/50 split but the groups came out very uneven, something in assignment or logging is broken, and the outcome cannot be trusted.
Alternatives When a Classic Split Does Not Fit
| Method | Good for | Trade-off |
|---|---|---|
| Interleaving | Comparing two ranking or search models quickly | Measures preference, not business metrics directly |
| Multi-armed bandit | Shifting traffic towards the winner while testing | Harder to get clean effect estimates |
| Switchback test | Marketplaces where users affect each other, such as pricing or dispatch | Needs careful time-slot design |
| Canary release | Checking stability and latency on a small slice | Usually too small to judge business impact |
Launch Checklist
- Both models log predictions, model version and user ID so results can be joined later.
- Latency of the new model is measured, since a slower model can lower engagement on its own.
- Novelty effects are watched: an early jump that fades after a week is common.
- Segments such as new versus returning users are checked, but only those planned in advance.
- A rollback switch exists and has been tested.
Ready to master Machine Learning?
Build real-world machine learning skills with hands-on projects, mentor-led sessions, and industry-relevant tools.