Statistics

Thompson Sampling Explained: Bandit Algorithm

Thompson sampling explained: how it draws from each arm's Beta posterior and why it converges faster than epsilon-greedy, with a worked example.

Abstract illustration of overlapping probability distribution curves flowing into branching light paths, in dark green and teal, representing adaptive traffic allocation

Thompson Sampling is the Bayesian algorithm behind most production multi-armed bandits: it is how a bandit decides, visitor by visitor, which variation gets the next click. Instead of fixing a traffic split up front and only analyzing results at the end, the way a classic A/B test does, it draws a random value from the current belief about each arm and sends the visitor to whichever arm drew the highest value, then updates that belief with the outcome. This guide walks through the mechanics behind the draw (the Beta distribution and its conjugate update), works through the math step by step with a three-variation example using numbers computed by the same Bayesian engine that runs the calculators on this blog, and states plainly when the technique earns its keep over a formal A/B test, and when it does not.

The problem Thompson Sampling solves: exploration vs. exploitation

Every multi-armed bandit has to balance two opposing forces. Exploring means spending traffic on arms you are still uncertain about, to learn more. Exploiting means concentrating traffic on the arm that looks best given what you know so far. An algorithm that only explores never converges and wastes conversions indefinitely; an algorithm that only exploits risks locking in on the wrong answer too early, because the first data points from any arm are noisy.

Three classic families answer this problem, each with a different philosophy:

The key difference between the three shows up in how exploration decays over the course of a test. In epsilon-greedy it stays constant unless someone manually lowers epsilon; in UCB and Thompson Sampling it shrinks on its own, purely as a consequence of more data reducing each arm’s uncertainty:

An arm’s belief distribution narrows as data accumulatesFor an arm with a true 5 percent conversion rate, the Beta belief distribution starts wide at 100 visitors, with a 2.32 percent standard deviation, narrows at 400 visitors to 1.11 percent, and narrows further at 1,600 visitors to 0.55 percent, which automatically reduces exploration.possible conversion rate for this arm100 visitorsstd dev 2.32%400 visitorsstd dev 1.11%1,600 visitorssame true rate (5%), more data= narrower curve = fewer drawslanding above average “by accident”
Real numbers from the Beta-Binomial model: with a uniform Beta(1,1) prior and a true rate of 5%, the posterior after 100, 400, and 1,600 visitors is Beta(6,96), Beta(21,381), and Beta(81,1521), with standard deviation falling from 2.32% to 1.11% and then 0.55%. Nobody has to program that shrinkage by hand, it falls out of the update math directly.

How Thompson Sampling decides, step by step

The mechanism is simpler than it looks at first, and it needs no manual parameter like epsilon. For every arm in the test, the algorithm keeps a probability distribution representing the current belief about that arm’s true conversion rate, given everything observed so far. When the metric is a binary conversion (converted or did not convert), that belief distribution is a Beta(alpha, beta), the natural conjugate pair of the binomial distribution.

The cycle repeats for every new visitor:

The Thompson Sampling decision loopFor every visitor, the algorithm draws a random value from the Beta distribution of each arm, picks the arm with the highest draw, observes whether that visitor converted or not, and uses the result to update that arm’s distribution before the next visitor.Drawone value fromeach Beta(a, b)Choosethe arm withthe highest drawObserveconverted ordid not convertUpdatethat arm’s Betaloops back for the next visitor
No manual parameter enters this loop: the balance of exploration and exploitation comes entirely from how wide each arm’s distribution still is, and it only narrows when real data arrives.

In plain terms, an arm with little data has a wide Beta (high uncertainty), so its draw sometimes lands on a high value even when the average looks middling, and it ends up receiving exploration traffic organically, with nobody reserving a fixed slice for it on purpose. An arm with lots of data and a clearly weak rate has a narrow, low Beta, so it almost never draws a value high enough to win, which reduces the traffic it gets without cutting it off completely.

The conjugate update: from Beta(1,1) to Beta(1 plus conversions, 1 plus non-conversions)

The part that makes Thompson Sampling cheap to compute, and therefore so common in production, is that for a binary conversion metric the belief update has a closed-form formula, with no heavy simulation required. Starting from a uniform Beta(1,1) prior, which says “before any data, every conversion rate between 0% and 100% is equally likely,” the update rule after observing a number of visitors and conversions is:

arm posterior = Beta(1 + conversions, 1 + visitors - conversions)

This is the same formula, with the same Beta(1,1) prior, that already runs underneath the Bayesian calculator on this blog. The reason it is this simple is conjugacy: when the prior is Beta and the observation is binomial (converted or not), the posterior stays Beta, so the update is just adding conversions to alpha and non-conversions to beta. There is no heavy numerical integration or simulation behind this particular step, just a sum.

A worked example: three variations, one round of traffic

To move past theory, let’s run the numbers. Picture a landing page running a test with three variations (A, B, and C) under Thompson Sampling. At the start, with no data yet, all three arms share the same uniform Beta(1,1) prior, a 33% share of traffic each. After a first round of 800 visitors per arm, the observed results were:

Arm Visitors Conversions Observed rate
A 800 40 5.00%
B 800 56 7.00%
C 800 32 4.00%

Applying the update formula, Beta(1+conversions, 1+visitors-conversions), to each arm:

The mean of a Beta(alpha, beta) distribution is alpha divided by (alpha plus beta), and the standard deviation is the square root of alpha times beta, divided by the square of (alpha plus beta) times (alpha plus beta plus 1). Working both out for each arm:

Arm Posterior Posterior mean Standard deviation
A Beta(41, 761) 5.11% 0.78%
B Beta(57, 745) 7.11% 0.91%
C Beta(33, 769) 4.11% 0.70%
The three posterior distributions after 800 visitors per armArm A has posterior Beta(41,761) with a mean of 5.11 percent, arm B has Beta(57,745) with a mean of 7.11 percent, and arm C has Beta(33,769) with a mean of 4.11 percent. Arm B’s curve sits shifted to the right of the other two, with small overlap.possible conversion rateC, 4.11%std dev 0.70%A, 5.11%std dev 0.78%B, 7.11%std dev 0.91%
B’s curve barely overlaps A’s and C’s. In the next round of traffic, most draws from B’s distribution will beat draws from A and C, so B receives the largest share of upcoming traffic without A and C being cut off entirely.

Notice the pattern: the further apart the means and the smaller the overlap between curves, the more predictable it becomes which arm wins the draw in the next round, and that mechanism, with no manual parameter anywhere, is exactly what pushes growing traffic toward B from here on. A and C still keep receiving some traffic, though, because their distributions, while lower, still have a tail that occasionally draws a competitive value. That residual exploration is what keeps the algorithm honest in case B’s round happened to be a lucky one.

Check the math on the Bayesian calculator

The posterior of each arm in Thompson Sampling is the same mathematical object a Bayesian A/B testing calculator uses to answer “what is the probability that B beats A.” To compare arm B against arm A from the example above, enter the numbers from the table (A: 800 visitors, 40 conversions; B: 800 visitors, 56 conversions) into the calculator below:

Bayesian A/B test calculator
A (control)
B (variation)
-probability that B beats A
Rate of A (posterior)-
Rate of B (posterior)-
Probability A wins-
Risk of choosing B (expected loss)-
Relative lift (means)-

Beta-Binomial model with a uniform Beta(1,1) prior and a 95% credible interval. Deterministic calculation, updates live.

Running those numbers through the same Bayesian engine that powers the calculator (conjugate Beta-Binomial, Beta(1,1) prior, 95% credible interval), the result is: A’s posterior rate at 5.11% (credible interval 3.70% to 6.74%), B’s posterior rate at 7.11% (credible interval 5.43% to 8.98%), a 95.34% probability that B beats A, a relative lift of the means of +39.0%, and an expected loss of just 0.02 percentage points from choosing B, against 2.02 percentage points from choosing A. Running B against C the same way (C: 800 visitors, 32 conversions), the probability that B beats C climbs to 99.57%, consistent with the wider gap between those two curves in the figure above.

That is the connection between the two techniques: a Bayesian A/B test runs this calculation once, at the end of the test, to report a verdict. Thompson Sampling runs the equivalent draw for every visitor, throughout the whole test, to decide traffic in real time. The underlying math is identical, what changes is the frequency and the purpose of the calculation.

Why Thompson Sampling tends to converge faster than epsilon-greedy

“Converges faster” here means the algorithm loses less possible conversion during its own learning process, the accumulated regret. The structural reason for that advantage lies in how each method decides how much to explore at any given moment:

Criterion Epsilon-greedy UCB Thompson Sampling
How exploration is decided Fixed slice (epsilon) of traffic, always random across arms Optimism bonus added to the estimated rate, larger when data is scarce Random draw from each arm’s belief distribution
Does exploration shrink on its own as data grows? No, epsilon stays constant unless someone lowers it manually Yes, the bonus shrinks as uncertainty falls Yes, the distribution narrows as uncertainty falls
Needs a manual parameter Yes, the value of epsilon (and, with decaying epsilon, the decay rate too) No, the bonus is computed straight from the data No, the prior is the only knob, and a uniform prior already works well in most cases
Where it wastes the most traffic On arms that are already clearly weak, after lots of data Less than epsilon-greedy, but the bonus is a deterministic approximation of uncertainty Tends to waste the least, because each arm’s real variance feeds directly into the draw

Epsilon-greedy treats uncertainty as a “how long should I explore” problem, solved with a fixed number chosen in advance. Thompson Sampling treats uncertainty as part of each arm’s own distribution, so the amount of exploration adjusts itself automatically: newly arrived arms explore a lot (wide distribution), well-established arms explore little (narrow distribution), with nobody deciding that by hand. That self-regulation, documented in empirical evaluations such as Chapelle and Li (2011), is what typically gives Thompson Sampling lower accumulated regret than epsilon-greedy in the same scenario.

When to use Thompson Sampling instead of a classic A/B test (and when not to)

The right question is not “which technique is more modern,” it is “what does this specific decision require.” Thompson Sampling tends to earn its keep when:

A classic A/B test remains the right tool when:

Many mature teams run both in sequence, as covered in our multi-armed bandits vs. A/B testing guide: an A/B test to formally validate that a change works, Thompson Sampling to continuously optimize among variations already validated, once the question has shifted from “does this work?” to “which one works best, always?” When the winner also depends on who is looking, a contextual bandit takes the same Thompson Sampling mechanics a step further, learning a different champion for different visitor segments instead of one overall winner.

Automate This in Donnu

You have just seen the full mechanics behind Thompson Sampling: each arm’s Beta posterior, the conjugate update that adds conversions and non-conversions onto the prior, the draw that decides the next visitor, and why that self-regulating exploration tends to beat a fixed epsilon. Donnu A/B already runs on that same native Bayesian engine today, the same Beta-Binomial math that declares an honest winner in a classic A/B test is the foundation a bandit like Thompson Sampling uses to optimize continuously. You do not need to guess which tool fits, start from the statistics both scenarios already require.

Start a free 14-day trial and run your own Bayesian comparison on real traffic.

Read the full guide to multi-armed bandits vs. A/B testing, the companion piece on contextual bandits explained for when the winner changes by visitor profile, and the Bayesian A/B testing guide for the broader frequentist-vs-Bayesian picture. Leia em português: Thompson Sampling explicado.

References

Frequently asked questions

What is Thompson Sampling, in plain terms?
Thompson Sampling is a multi-armed bandit algorithm that decides which variation to show each visitor by drawing a random value from the current belief distribution (posterior) of every arm, then routing the visitor to whichever arm drew the highest value. William R. Thompson introduced the idea in a 1933 paper, and it is now the most common Bayesian method used in production bandit systems.
Why does Thompson Sampling typically converge faster than epsilon-greedy?
Because the amount of exploration adjusts itself to the real uncertainty of each arm: arms with little data have wide posteriors and get sampled often, which is automatic exploration, while arms with lots of data and a clearly weak rate have narrow posteriors and almost never win the draw. Epsilon-greedy, by contrast, reserves the same fixed slice of traffic for random exploration at all times, even after uncertainty has largely disappeared.
Do I need to understand Bayesian statistics to use Thompson Sampling in practice?
Not to run the product day to day: the mechanics, a Beta distribution as belief, an update from observed conversions, a draw, and picking the highest value, are the same math that already powers a Bayesian A/B testing calculator. Understanding the logic helps you interpret why traffic shifts the way it does, but the calculation itself ships ready to use inside the tool.
When should I NOT use Thompson Sampling instead of a classic A/B test?
When the decision needs a formal, documentable verdict: a p-value, a confidence interval, or a traffic split stable enough to slice by segment afterward. Thompson Sampling keeps changing the allocation continuously, so it does not produce classic statistical significance and it does not preserve a comparable traffic split from the start of the test to the end.
Do Thompson Sampling and a Bayesian A/B test use the same math?
Yes. Both techniques start from the same conjugate Beta-Binomial model: the posterior of each arm, or variation, is Beta(1 plus conversions, 1 plus non-conversions), built from a uniform Beta(1,1) prior. The difference is what you do with that posterior. A Bayesian A/B test reports the final probability that B beats A, once, at the end. Thompson Sampling draws from that same posterior for every visitor to decide traffic in real time.