In a recent post, a company selling A/B testing services made the claim that A/B testing is superior to bandit algorithms. They do make a compelling case that A/B testing is superior to one particular not very good bandit algorithm, because that particular algorithm does not take into account statistical significance.

However, there are bandit algorithms that account for statistical significance.

UPDATE: Several years after writing this post, I've learned that it's a bit misunderstood. After reading this post, please go read this followup post to avoid any misconceptions.

Epsilon-greedy algorithms

inputs:machines::[GamblingMachine]num_plays::Map(GamblingMachine->Int)total_reward::Map(GamblingMachine->Float)selecte// In the original post, e = 0.1whileTrue:x<-UNIFORM([0,1])// A uniformly distributed random variableifx<e://exploration phaseletm=random_choice(machines)reward<-play_machine(m)num_plays[m]+=1total_reward[m]+=rewardelse://exploitation phaseaverage_rewards=Map((m,total_reward[m]/num_plays[m])forminmachines)best_machine=argmax(average_rewards)//Find the machine with the highest rewardreward<-play_machine(best_machine)num_plays[best_machine]+=1total_reward[best_machine]+=reward

Basically, exploration with probability e and explotation with probability 1-e.

This is not a good algorithm. Consider the scenario of two machines - machine A has reward 1, machine B has reward 0. Even after the epsilon-greedy algorithm has converged, 10% of the time a random machine will be chosen. So given "convergence", the odds of choosing machine B are still:

P( exploration) * P(machine B | exploration) = e * (1/2)

So your total reward can be at most:

num_plays * (1 - e/2)

I.e., if e=0.1 (as advocated in the original blog post), you are leaving $5 on the table for every $100 you could potentially earn.

A/B testing or Epsilon-first algorithms

The approach that Visual Website Optimizer advocates is A/B testing, which is called epsilon-first in the formal literature. The way it works is that you explore for a finite number of plays (the A/B test) and exploit forever after:

I won't get into the specifics of how Visual Website Optimizer set up their A/B test, since they did it wrong (see their code). In particular, what they did is A/B testing with repeated peeks - they measured statistical significance repeatedly throughout the A/B test. Rather than explaining why this is wrong, I'll just refer to the excellent post How Not to Run an A/B Test, by Evan Miller. Seriously, go read that article.

The key point in understanding epsilon-first algorithms is recognizing that statistical_test has an error rate (which is dependent on N as well as the prior distribution of machine rewards). Rather than getting into the details of statistical testing, I'll just make up a symbol for it, Err.

Let us now suppose the epsilon-first algorithm has finished the exploration phase. In that case:

P( best_machine_estimate == true_best_machine ) = 1 - Err

Suppose again that true_best_machine has reward 1, and the other worse machines all have reward 0. Then in the exploitation phase, your reward is:

If you run your A/B tests with 5% confidence levels (as most people do), you are again leaving $5 on the table for every $100 you could earn.

In the epsilon-greedy case, the most likely scenario is that you collect $95/$100. In the epsilon-first case, the most likely scenario is that you collect $100, and there is a 5% chance you collect $0. But in both cases, you are leaving money on the table.

The algorithm is called UCB1, and it's based on the following idea. Figure out which machine could be the best possible one (based on your statistical level of confidence), and play that machine as much as possible. Note that I didn't say which machine is the best, but which one could be the best.

There are two ways a machine could be the best:

You have measured it's mean payout to be higher than all the others, and you are certain of this with a high degree of statistical significance.

You have not measured it's mean payout very well, and the confidence interval is large.

The algorithm is based on the Chernoff-Hoeffding bound. Suppose we have a random generator rand. Repeated calls to rand are assumed to be independent of each other, and it's range is bounded:

x <- rand
assert(0<= x && x <=1)

Now suppose we run N trials and compute the average:

avg = 0
for i in 1..N:
avg += rand
avg /= N

Define m to be the theoretical statistical mean of rand (i.e., if rand were a fair coin flip, m=0.5). Then avg is an approximation to m.

Chernoff-Hoeffding says that for any a > 0, we have the following bound:

P( avg > m + a) <= 1 / exp(2 * a^2 N)

I.e., the probability that avg overestimates m by at least a decreases exponentially with N. This is just a way of quantifying something we already know intuitively - the more trials we run, the better our estimate of the mean of a random variable.

If we reverse Chernoff-Hoeffding and do some arithmetic (which is already done in the paper, so I won't repeat it here), we can get the following stylized fact (i.e., it isn't true, but is intuitively useful):

true_mean(m) <= avg[m] + sqrt( 2 ln(N) / num_plays[m] )

Here m is a machine, avg[m] the average reward observed so far from machine m, num_plays[m] the number of times machine m has been played, and N the total number of plays across all machines. From here on out, I'll refer to sqrt( 2 ln(N) / num_plays[m] ) as the confidence bound.

This algorithm starts off in a pure exploration phase, since avg[m] will be small compared to the confidence bound. For example, with N=10 and num_plays[m]=5, we find that the confidence bound is 0.9597. For comparison, avg[m] is some number bounded by 1.

However, for larger values of N, ln(N) grows very slowly, and eventually ln(N) / num_plays[m] will become small. With N=400 and num_plays[m]=200, we find the confidence bound has dropped to 0.2447. If avg[m_best] = 0.50 and avg[m_worst] = 0.20, the algorithm will start playing m_best most of the time.

At this point, N and num_plays[m_best] are increasing, but num_plays[m_worst] is remaining constant. This means the confidence bound for m_worst is slowly increasing. At N=8000, the confidence bound on the worst machine is 0.2997, while the confidence bound on the best is only 0.04800. We will continue playing machine m_best for a while. For simplicity, I'll assume avg[m_best]=0.5 and avg[m_worst]=0.2 on every play. (In reality this won't be true, I'm just trying to illustrate a point here.)

The key point here is that ln(N) grows very slowly relative to num_plays[m_worst]. So while the exploration process continues forever, the fraction of time we spend exploring decreases exponentially.

Another important fact is that the exploration time is adaptive - if avg[m_best] and avg[m_worst] are very close together (i.e., 0.5 and 0.51), then we will spend more time exploring until we are finally confident of which machine is the best one.

There is a theorem proven in the paper which says that the regret of this algorithm grows only logarithmically. This means that your reward will be proportional to:

num_plays - log(num_plays)

This value will always be larger than either num_plays * (1-e/2) or num_plays * (1 - Err) provided num_plays is sufficiently large.

This means that UCB1 will always beat both A/B testing and "20 lines of code...". (Though actually, UCB1 will take only marginally more than 20 lines of code.)

Conclusion

If you don't do any of this stuff (A/B, epsilon-greedy, MAB), pick whichever one is easiest and do it. Any of these fine optimization methods are vastly better than nothing. If you really want to be optimal, try UCB1 or any of the more modern adaptive bandit methods. They are marginally better than A/B tests.

I've also written quite a bit more on bandit algorithms since this post was published. I've described my favorite bandit algorithm, the Bayesian Bandit, which achieves similar accuracy to UCB1. I wrote this blog post about Bayesian A/B testing. And I wrote a post about measuring a changing conversion rate, which (when combined with the Bayesian Bandit) can handle the case of time varying conversion rates.

UPDATE: Several years after writing this post, I've learned that it's a bit misunderstood. After reading this post, please go read this followup post to avoid any misconceptions.