The DataHour Synopsis: Hands-on with A/B Testing for AI & Machine Learning
A/B testing has long been a core tool for data-driven decision making, but it‘s becoming increasingly critical for AI and machine learning practitioners to master as well. As more products and experiences are powered by machine learning models, A/B tests are a key way to reliably measure the impact of new models and ensure they are having the desired effect.
In this hands-on overview, we‘ll walk through what A/B testing is, break down the key concepts, and share an example to illustrate an end-to-end A/B testing experiment. By the end, you‘ll understand the foundations of A/B testing and how to start applying it to your own AI/ML work. Let‘s dive in!
What is A/B Testing?
At its core, A/B testing is a way to compare two versions of something to determine which performs better. In a digital product context, this typically means comparing a control (A) and treatment (B) experience to see which moves the needle on a key metric.
For example, you may want to test two different recommendation algorithms on an e-commerce site to see which leads to a higher revenue per user. Or you may want to compare a chatbot powered by a new natural language model vs. the existing chatbot to see if it increases customer satisfaction scores.
The key is that users are randomly bucketed into either the A or B experience, and only one key metric is compared between the two groups. Any difference in the metric between the two groups can then be attributed to the version, rather than other factors.
Key Statistical Concepts
To really understand how A/B testing works under the hood, there are a few key statistical concepts to grasp:
Hypothesis Testing
An A/B test is fundamentally a type of hypothesis test. You start by stating a null hypothesis and an alternative hypothesis:
- Null hypothesis: There is no difference in the key metric between groups A and B
- Alternative hypothesis: There is a difference in the key metric between groups A and B
The goal is to determine whether you can reject the null hypothesis based on the data. If you can reject it, that means there is a statistically significant difference between the two groups.
P-Value
The p-value represents the probability of observing the data you did if the null hypothesis were true. A lower p-value means there is stronger evidence against the null hypothesis.
The standard threshold for "statistical significance" is a p-value < 0.05. This means if you ran the experiment 100 times, you‘d expect to see the observed difference only 5 times if there was truly no difference between the groups.
Confidence Interval
A confidence interval is a range that you expect the true population value to fall within a certain percentage of the time. A 95% confidence interval is most commonly used.
So if your experiment found a 5% lift in the treatment group with a 95% confidence interval of 3%-7%, that means if you ran the experiment many times, the true difference would fall between 3% and 7% in 95% of the experiments.
End-to-End A/B Test Example
Let‘s walk through an example to make this all concrete. Say you work for a video streaming service and want to test a new machine learning model that personalizes the homepage for each user. You believe this will lead to increased engagement, which you measure through total watch time per user.
Experiment Setup
First, you need to set up your A/B test:
- Define control & treatment
- Control (A): Existing non-personalized homepage
- Treatment (B): New homepage with ML-driven personalization
- State hypothesis
- Null hypothesis: No difference in average watch time per user between control and treatment
- Alternative hypothesis: Treatment group will have higher average watch time per user
-
Choose significance level: Let‘s use the standard p < 0.05
-
Determine sample size: Based on past data, you know you need 500,000 users in each group to detect a 2% increase in watch time with 95% confidence
Running the Experiment
With the setup in place, you can launch the A/B test. The key steps:
- Randomly split traffic 50/50 between control and treatment
- Run experiment for predetermined period of time, let‘s say 2 weeks
- Collect watch time data for each user in both groups
Analyzing the Results
Once you have the data, it‘s time for analysis:
- Calculate key metrics:
- Control group average watch time: 3.2 hours/day
- Treatment group average watch time: 3.3 hours/day
- Run significance test:
- Test statistic: (3.3 – 3.2) / sqrt((1.2^2/500000) + (1.3^2/500000)) = 6.01
- P-value: p < 0.00001
- The difference is statistically significant!
- Calculate confidence interval:
- Standard error: sqrt((1.2^2/500000) + (1.3^2/500000)) = 0.0163
- 95% confidence interval: 3.3 – 3.2 ± 1.96*0.0163 = (0.068, 0.132)
- You‘re 95% confident the true increase in watch time is between 0.068 and 0.132 hours (4-8 minutes) per user
- Draw conclusions:
- The new ML-powered personalized homepage led to a statistically significant 3.125% lift in watch time
- You reject the null hypothesis and launch the new homepage to all users!
Best Practices & Pitfalls
As you conduct more A/B tests, keep these tips and pitfalls in mind:
- Let experiments run for full planned duration. It can be tempting to stop early when you spot a significant difference, but this increases the chance of a false positive
- Be wary of launching experiments with multiple changes in the treatment. If you see a lift, you won‘t know which change was responsible! Try to isolate variables as much as possible
- Make sure your A/B testing platform is truly randomizing users and isn‘t introducing bias through improper hashing or sticky bucketing
- QA treatment experiences thoroughly! It‘s a frustrating waste of time and data to realize after the fact that the treatment was broken
- Think deeply about metrics. Avoid vanity metrics in favor of metrics that truly reflect the health of your product and business
A/B Testing & Machine Learning
While the example above focused on testing a full ML model, you can also use A/B tests to test model components like:
- Different model architectures
- New input features
- Adjustments to model hyperparameters
- Different training data sets
For example, you could A/B test two versions of an image tagging model, one trained on 1 million images and one trained on 10 million images, to quantify the impact of increased training data.
You can also A/B test non-model components like latency and see if users react differently when an ML-powered experience is slower.
The key is to approach every change with the rigor of an A/B test, even if it‘s "just" a model change. Measure the impact and ensure there are no unexpected side effects before rolling out to everyone.
Conclusion & Further Reading
We‘ve covered a lot of ground in this overview of A/B testing for AI and ML! To recap:
- A/B testing measures the impact of a treatment on a key metric vs. a control group
- Hypothesis testing, p-values, and confidence intervals are the key statistical concepts that underlie A/B tests
- A/B tests are a crucial tool for evaluating the real-world impact of ML models and interventions
If you‘re eager to learn more, here are some great resources to dive deeper:
- Udacity A/B Testing Course: https://www.udacity.com/course/ab-testing–ud257
- Experimentation in a Data-Driven World (Coursera): https://www.coursera.org/learn/experimentation-in-a-data-driven-world
- Trustworthy Online Controlled Experiments (Book): https://www.amazon.com/Trustworthy-Online-Controlled-Experiments-Practical/dp/1108724264/
I hope this has been a valuable intro to the what, why, and how of A/B testing in AI/ML! Happy experimenting!