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.
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:
update(estimates, counts, version, reward)keeps a running average for each version, in place.- Four strategies, each
choose(estimates, counts, rng)returning a version number:choose_even_split(the plain A/B test),choose_epsilon_greedy(withepsilon=0.1),choose_ucb(withc=1.0) andchoose_thompson. run_test(choose, rates, visitors, rng)shows one screen to each visitor and returns(estimates, counts, rewards).regret(counts, rates): the visitors a test is expected to have lost, against showing everyone the best version.chance_best(estimates, counts, rng, draws=10000): each version's chance of really being the best.- 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.
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
- Try things and keep score
- The running average
- Always the best so far
- The greedy test
- What a fair test costs
- A quarter each
- Your favourite café, or a new one
- One in five explores
- Explore where you're unsure
- The benefit of the doubt
- A belief, not a number
- What might be true
- How sure is the winner?
- Counting the winners
- How Paul would do it
- Fix Yahya's version
- Make the A/B test bandit
- What the winner hides
- Ship it