Concepts / A/B Testing for Machine Learning Systems

A/B Testing for Machine Learning Systems

Offline evaluation assesses algorithmic predictions on a test set collected beforehand and is the first step for user-facing systems.

  • Programming

A Good Test Score Is Not the Finish Line

Imagine you have built a model that predicts which items a user will want to see next on a shopping website. You have trained it, you have checked it, and it looks promising. The natural question is what kind of checking comes first, what kind comes next, and why the second kind cannot be skipped when real people are involved.

A model that scores well on past data may still change what real people do once it is placed in front of them. Changing the recommendations or search results a website shows can drastically change user behavior, and a change that looks like a clear improvement on a test set can lead to unexpected consequences once people start reacting to it. This article follows the path from the first kind of checking, offline evaluation, toward testing with live users, and looks at one of the most common tools for doing that: A/B testing.

Offline Evaluation on a Saved Test Set

Definition

Offline evaluation assesses an algorithm's predictions on a test set that was collected beforehand. It is the first step in evaluating a user-facing machine learning system.

Offline evaluation is a necessary starting point. It is cheap to repeat, it does not expose real users to an unfinished system, and it lets you compare candidate models on identical inputs. Because the test set is fixed in advance, every candidate model is measured against exactly the same examples, which makes the comparison fair.

The limitation is built into the design. The predictions offline evaluation measures are fixed in advance, while the system's behavior in production depends on how people respond to what they are shown. Offline evaluation can tell you how well a model matches the past, but it cannot observe a future in which users react to the model's output.

Moving From Saved Data to Live Users

Once an algorithm is part of a live website, the question changes. It is no longer enough to ask whether the predictions match a test set. The team must ask what happens to the whole system when users encounter the new behavior. This is called online testing, also known as live testing, in which the consequences of employing the algorithm in the overall system are evaluated.

For systems that users see directly, online testing is usually the next step after offline evaluation. Offline evaluation tells you whether a candidate looks promising on saved data; online testing tells you how the candidate behaves inside the product. The diagram below shows the order of these stages.

Offline evaluationpredictions on a saved testsetOnline testingconsequences with liveusersDecisionchoose between approachesthenmetrics compared
What happens after offline evaluation on a saved test set, and how online testing with real users follows it.

How an A/B Test Splits Users

Definition

A/B testing is a form of blind user study. A selected portion of users is served a website or service using algorithm A, while the remaining users are served algorithm B. The users do not know which group they belong to.

The procedure has three stages. First, users are divided into two groups, and each group receives one algorithm. Second, relevant success metrics are recorded for both groups over a set period of time. Third, the metrics of algorithm A and algorithm B are compared, and a choice between the two approaches is made according to those metrics. Because users never need to know that a comparison is happening, the study is described as blind.

A common arrangement is for algorithm A to be a new model and algorithm B to be the established system already running. The new model is tested against the current one on live traffic, so the decision to adopt it rests on how users actually behave rather than on offline scores alone. The current system keeps serving everyone outside the tested portion.

Incoming uservisits the live siteSplit usersselected portion vsremainderGroup Aserved algorithm AMetrics for Arecorded over a set periodCompare A and Bchoose the approachGroup Bserved algorithm BMetrics for Brecorded over a set periodentersselected portionremaining usersrecordrecordcomparecompare
When a user arrives, how are they assigned to group A or group B, and where does each group's traffic go?

Why Live Testing Catches Surprises

The main benefit of A/B testing is that it lets you evaluate algorithms in the wild. Because users are interacting with the model in its real setting, the test can surface effects that no offline measurement would have predicted. This is the protection most user-facing services rely on: a small portion of traffic absorbs the risk of a surprising change before it reaches everyone.

A useful way to hold this idea is to picture the algorithm's output feeding into the rest of the product. Its recommendations are displayed, users click or ignore them, and those reactions shape the metrics the team sees. The effect of an algorithm therefore includes changes in what people do downstream, not only the accuracy of its predictions.

Comparing Click Rates Between Two Groups

Once the metrics have been recorded, the comparison can be as simple as computing a rate for each group. Suppose the metric is the fraction of visitors who clicked on a recommended item. For each group, the click rate is the number of clicks divided by the number of visitors: $\text{click rate} = \frac{\text{clicks}}{\text{visitors}}$.

Worked example

Click rates for two recommendation algorithms

Group A is served the new model and group B is served the established system. Group A had 1,200 visitors and 144 clicks. Group B had 1,000 visitors and 110 clicks. Which group has the higher click rate, and what does the comparison show?

  1. 1
    Compute the rate for group A

    144 clicks divided by 1,200 visitors gives 0.120.

  2. 2
    Compute the rate for group B

    110 clicks divided by 1,000 visitors gives 0.110.

  3. 3
    Place the two rates side by side

    Group A is 0.010 higher than group B. This difference is the starting point of the comparison, not the conclusion.

Result

Group A shows a click rate of 0.120 and group B shows 0.110. Group A is ahead on this metric, but the decision should rest on the metrics that were chosen before the test began, not on whichever numbers look best.

python
Output (expected)
Group A click rate: 0.120
Group B click rate: 0.110
Notes on the code
  • The visitor and click counts are invented numbers for illustration.
  • Each group's rate is computed the same way so the two can be placed side by side.
1,200 visitors1,000 visitors144 clicks110 clicks0.120 click rate0.110 click rate
How do the metrics from group A and group B line up side by side, and what difference between them is the starting point for the decision?

Where Bandit Algorithms Fit

A/B testing is not the only way to run online tests. There are more elaborate mechanisms that go beyond A/B testing, and bandit algorithms are one example. They belong to the same online testing family, so they also evaluate an algorithm's consequences with live use, but they are more involved than a simple split between two fixed groups.

The source does not describe how bandit algorithms work internally, and it points to a dedicated book on the subject for a proper introduction. For now, the key point is that A/B testing is a well-established starting approach within a larger set of options. You do not need to master bandit algorithms to understand live evaluation, but it helps to know they exist for cases where a simple two-group comparison is not the right fit.

Mistakes When Reading and Running A/B Tests

  • Common mistake

    Treating offline scores as proof that a model will work for users

    What it looks like

    A new model beats the old one on the saved test set, so the team ships it to everyone without a live test.

    Why it fails

    Offline predictions are fixed in advance, while user behavior depends on how people respond to what they are shown.

    Do this instead

    Use offline evaluation as the first step, then run online testing to measure consequences inside the overall system.

  • Common mistake

    Choosing the success metric after seeing which numbers look best

    Why it fails

    The test is designed around metrics chosen in advance. Picking whichever number favors a result undermines the comparison.

    Do this instead

    Decide which metrics matter before the test begins, and record those same metrics for both groups.

  • Common mistake

    Reading a raw difference in rates as a final answer

    What it looks like

    Group A shows 0.120 and group B shows 0.110, so A is declared the winner immediately.

    Why it fails

    A difference in rates is only the starting point of the comparison.

    Do this instead

    Treat the gap as the input to a careful decision, and judge it against the pre-chosen metrics and the set period over which they were recorded.

  • Common mistake

    Comparing groups that were measured over different periods or under different conditions

Practice Reading an A/B Test

MEDIUM

A team runs an A/B test in which group A is served a new recommendation model and group B is served the current system. Group A has 800 visitors and 96 clicks. Group B has 900 visitors and 99 clicks. Compute each group's click rate, then explain in your own words why the difference alone does not settle which algorithm to adopt.

Hints
  • Divide clicks by visitors for each group separately.
  • Think about what the source says about metrics chosen in advance and about the difference being only a starting point.
  • Consider what would change your confidence: how the metrics were chosen, and whether both groups were recorded over the same period.

Putting the Evaluation Path Together

  1. 1
    Offline evaluation measures predictions on a test set collected beforehand and is the first step for user-facing systems.
  2. 2
    Online testing measures the consequences of an algorithm inside the overall system and usually follows offline evaluation, because user behavior can change in unexpected ways.
  3. 3
    A/B testing blindly splits users between algorithm A and algorithm B, records success metrics for both groups over a set period, and compares them to make a selection.
  4. 4
    Bandit algorithms are a more elaborate family of online testing methods that go beyond A/B testing.

Key Takeaways

  • Offline evaluation assesses predictions on a test set collected beforehand and is necessary but not sufficient for user-facing systems.
  • Online testing evaluates the consequences of an algorithm inside the overall system and usually comes after offline evaluation.
  • A/B testing splits users between algorithm A and algorithm B without their knowledge, records metrics for both groups, and compares them.
  • Metrics should be chosen in advance, and a raw difference between groups is the starting point of a decision rather than its conclusion.
  • Bandit algorithms are a more elaborate family of online testing methods that go beyond a simple two-group split.

Related concepts

More concepts starting with A