Paul's Python CoursePPC

Project 1 of 12, for Caitlin

A/B Test Bandit

Four attract screens for Caitlin's new cabinet, five thousand visitors walking past, and a test that shows the winner more often as it learns: epsilon-greedy, UCB and Thompson sampling.

About 2 hours, in 20 steps.

Sign up free to open itLog in

Act 8 needs a free account, so your work is saved as you go. Act 1 is open to everyone.

The brief

  • CaitlinI've got a new cabinet, and Riya drew four attract screens for it, the loop it plays when nobody's playing.
  • RiyaThere's a high-score table, a demo race, a flashing coin and a crab that waves at you.
  • CaitlinThe fair way is a quarter of the visitors each for a whole fortnight, and I'll be bored by Tuesday.
  • AaeshaPeople make this kind of choice every day, whether to stick with the best so far or try something new.
  • PaulThat's fair to the screens, but it's the visitors who pay for it. There's a cheaper way to be fair.

Every visitor who walks past Caitlin's new cabinet sees one of four attract screens. If they stop and play, that screen scored (reward 1); if they walk on, it didn't (reward 0). Each version has its own chance of bringing a visitor in, and nobody knows what it is.

A plain A/B test shows the versions in turn and compares them at the end. A bandit test learns as it goes, and shows whichever is winning more often.

Write abtest.py:

  1. update(estimates, counts, version, reward) keeps a running average for each version, in place.
  2. Four strategies, each choose(estimates, counts, rng) returning a version number: choose_even_split (the plain A/B test), choose_epsilon_greedy (with epsilon=0.1), choose_ucb (with c=1.0) and choose_thompson.
  3. run_test(choose, rates, visitors, rng) shows one screen to each visitor and returns (estimates, counts, rewards).
  4. regret(counts, rates): the visitors a test is expected to have lost, against showing everyone the best version.
  5. chance_best(estimates, counts, rng, draws=10000): each version's chance of really being the best.
  6. Run as a program, it tries the even split, plain greedy, epsilon-greedy, UCB and Thompson sampling on 5000 visitors each, prints a line for each, recommends a screen, and draws a chart.
What it prints
strategy            played  lost  its pick
even split             584   238  waving crab
greedy                 485   350  high-score table
epsilon-greedy         749    69  waving crab
UCB                    690   124  waving crab
Thompson sampling      827    26  waving crab

Recommendation: waving crab, 96% likely to be the best

What you learn

What's in it

  1. Try things and keep score
  2. The running average
  3. Always the best so far
  4. The greedy test
  5. What a fair test costs
  6. A quarter each
  7. Your favourite café, or a new one
  8. One in five explores
  9. Explore where you're unsure
  10. The benefit of the doubt
  11. A belief, not a number
  12. What might be true
  13. How sure is the winner?
  14. Counting the winners
  15. How Paul would do it
  16. Fix Yahya's version
  17. Make the A/B test bandit
  18. What the winner hides
  19. Ship it