Thompson Sampling and Bayesian Bandits
A way to decide which of several unproven options to try next by keeping a posterior belief over each option's true payoff and randomly picking according to how likely each one is to be the best — automatically balancing exploring uncertain options against exploiting known-good ones.
Prerequisites: Bayes' Theorem, Conjugate Priors
A trading desk has five variants of an execution algorithm, each with an unknown true fill-rate advantage, and only limited order flow to test them with live. Route everything to the current best-looking variant, and you might lock into a false leader based on early noise, never discovering another variant is actually better. Split traffic evenly forever, and you waste flow testing variants already clearly worse. Thompson sampling navigates this tradeoff automatically, using nothing more exotic than a posterior distribution per option and a random draw.
An analogy: choosing a restaurant among several you're unsure about
Suppose you've tried five restaurants a handful of times each, and you're not fully sure which is genuinely best — some you've visited more (so you're more confident), some only once (so your belief is wide and uncertain). A sensible policy for where to eat tonight isn't "always the best average so far" (overcommits to noisy early impressions) and isn't "rotate evenly" (wastes visits on ones you're already fairly sure are worse). Instead: imagine a plausible "true quality" for each restaurant, drawn according to how confident and where-centered your current belief is, then go wherever looks best in that one imagined draw. An uncertain restaurant occasionally gets a lucky high draw and gets picked, giving it a fair chance to prove itself, while one you're confident is mediocre rarely draws high enough to win. This is Thompson sampling.
The maths, one symbol at a time
For each option (arm) , maintain a posterior distribution over its true payoff , based on data observed from that arm so far. At each decision point:
In words: draw one random sample from each arm's current posterior belief about its own true payoff, then pick whichever sampled value is highest this round. An arm with a wide, uncertain posterior occasionally produces a high sample purely from that uncertainty, giving it a chance to be explored; an arm with a narrow posterior confidently centered on a mediocre value almost never produces a high enough sample to win. As data accumulates on each arm, its posterior narrows around its true payoff, and the sampled draws increasingly reflect the true ranking rather than noise — exploration tapers off naturally, with no separate schedule to hand-tune.
Worked example 1: two execution variants after limited data
Variant A has been tested 40 times with 28 successes, giving a Beta posterior (mean , fairly tight). Variant B has been tested only 5 times with 4 successes, giving a Beta posterior (mean , but very wide). On a given round, sample Beta and Beta. Even though B's mean is higher, its wide spread means could easily land as low as or as high as ; A's tighter posterior usually samples around –. Some rounds B "wins" and gets tried again (appropriately, since it's underexplored); other rounds A wins, since its solid track record still frequently outdraws B's noisier one. Traffic splits in a data-justified way, with no fixed exploration percentage set by hand.
Worked example 2: convergence as data accumulates
Suppose after 500 more rounds, Variant A's posterior has tightened to Beta (mean ) and Variant B's to Beta (mean ) — B's early 4/5 success rate turned out to be mostly luck. Now sampling from each posterior almost always gives A a higher draw, since the two barely overlap anymore; B is essentially never chosen going forward. Through the same sampling rule used from round one, the algorithm has transitioned from heavy exploration to heavy exploitation, with the transition speed set automatically by how quickly each posterior narrows, not a separate schedule.
Each arm in Thompson sampling carries its own posterior like the one shown in the explorer above — a tight, confident posterior mostly loses the "who gets tried next" draw to a wide, uncertain one until enough data narrows it too.
What this means in practice
Thompson sampling and Bayesian bandits are used for A/B testing execution algorithms, market-making quote strategies, or product variants under live traffic, where continuously re-splitting traffic based on updated posteriors captures far more value than a fixed-length A/B test committed to one allocation until a predetermined end date. It's also the standard baseline behind many production recommendation and pricing systems — provably good long-run performance from a rule simple enough to implement around any conjugate posterior update.
Thompson sampling picks the next option to try by drawing one random sample from each option's current posterior belief and choosing the arm with the highest sampled value — this automatically balances exploring uncertain options (which can score a lucky high sample) against exploiting confidently good ones (which reliably score high), with the exploration-exploitation tradeoff resolving itself as each posterior narrows, no manual schedule required.
Thompson sampling assumes each arm's payoff distribution is genuinely stationary — that Variant A's true fill rate today is the same distribution it'll have next month. In a live market, execution quality drifts with liquidity conditions, and an arm that looked clearly worse under old conditions may quietly become better as conditions shift, while a narrowed, confident posterior from stale data keeps it from ever being resampled. Non-stationary environments need an explicit mechanism (discounting old observations, periodically widening posteriors) to keep exploration alive — plain Thompson sampling on stale data can lock in a wrong answer indefinitely.
Related concepts
Practice in interviews
Further reading
- Russo et al., 'A Tutorial on Thompson Sampling' (2018)
- Thompson, 'On the Likelihood that One Unknown Probability Exceeds Another' (1933)