A bandit algorithm must decide whether to use the option that currently looks best or gather information about another. Thompson sampling makes that choice by sampling plausible reward probabilities from its current posterior beliefs.
Spotted in the wild
- “parameter prior”Belief before observing the current data.
- “parameter posterior”Updated distribution conditional on data.
- “Beta distribution”A density over Bernoulli success probabilities.
- “maximum a posteriori estimate”A mode of the posterior density.
- “posterior predictive”Future-data distribution averaged over the posterior.
- “precision”Reciprocal variance for a scalar Gaussian.
| Symbol | Say it | Meaning | LaTeX |
|---|---|---|---|
| “parameter prior” | Belief before observing the current data. | ||
| “parameter posterior” | Updated distribution conditional on data. | ||
| “Beta distribution” | A density over Bernoulli success probabilities. | ||
| “maximum a posteriori estimate” | A mode of the posterior density. | ||
| “posterior predictive” | Future-data distribution averaged over the posterior. | ||
| “precision” | Reciprocal variance for a scalar Gaussian. |
Parameters can have distributions
Bayesian inference combines a prior and likelihood :
The posterior describes parameter uncertainty conditional on the model and data. A prior is part of that model. More data can reduce its influence, but only if the likelihood is informative about the parameter in question.
Beta plus Bernoulli
The Beta density on (0,1) has kernel for positive shape parameters. With s successes and f failures, multiply by the Bernoulli likelihood :
The posterior stays in the same family, which is called conjugacy. Its mean is . For posterior shape parameters both greater than one, the mode is . Boundary modes require separate treatment.
From a uniform Beta(1,1) prior, eight successes and two failures give Beta(9,3). The MLE is 0.8, the posterior mean is 0.75, and the interior MAP is 0.8. These answer different estimation questions.
A Beta(1,1) prior sees eight successes and two failures. What is the posterior mean?
Predict by averaging over uncertainty
The posterior predictive integrates parameters out:
For one new Bernoulli trial, its success probability equals the posterior mean. For several future trials, using a fixed plug-in probability can miss dependence induced by the shared uncertain θ. The Beta-binomial predictive accounts for that uncertainty.
Gaussian conjugacy
Suppose and observations are conditionally independent with known σ². Completing the square gives posterior variance and mean
Precision, the reciprocal variance, adds. The next observation has predictive variance , combining observation noise with remaining mean uncertainty.
A Gaussian posterior for a mean has variance 0.2. Observation noise variance is 1. What is the predictive variance of one new observation?
MAP and regularisation
The maximum a posteriori estimate maximises log likelihood plus log prior. An isotropic Gaussian prior on weights has negative log density equal to a constant plus . Thus MAP minimises negative log-likelihood plus an L2 penalty. The penalty coefficient depends on whether the loss is summed or averaged.
MAP supplies one parameter value. The posterior mean and posterior predictive retain different aspects of uncertainty. A credible interval assigns posterior probability to a parameter region under the specified model; it is not automatically a frequentist coverage guarantee.
Thompson sampling
Maintain a Beta posterior for each machine, initially Beta(1,1). Draw one θ from each posterior, choose the largest, observe the reward, and increment the selected arm’s success or failure count. Broad posteriors sometimes produce optimistic draws, encouraging exploration.
No finite budget guarantees discovery of the best arm with a specified confidence for every possible reward configuration. Very similar machines can require many trials. A simulated posterior probability of being best is itself a numerical estimate, conditional on the model.
What makes Thompson sampling explore?
Read beyond
Book · free online · ~20 min
Mathematics for Machine LearningDeisenroth, Faisal & Ong · Sections 8.4 and 9.3: Bayesian inference
Compare a parameter point estimate with a posterior distribution.
Book · free online · ~20 min
Introduction to Probability, Statistics, and Random ProcessesHossein Pishro-Nik · Chapter 9: Bayesian inference
Write the likelihood and prior kernels before identifying a conjugate posterior.
Book · free online · ~20 min
Introduction to ProbabilityBlitzstein & Hwang · Beta distributions and conjugacy
Connect Beta parameters with Bernoulli counts.
Read the equation in context
A Tutorial on Thompson SamplingDaniel Russo, Benjamin Van Roy, Abbas Kazerouni, Ian Osband & Zheng Wen · 2018The tutorial presents Thompson sampling as choosing actions from sampled plausible models. In the Bernoulli example, successes and failures update Beta posteriors. The sampled winner is an exploration strategy, not proof that its true reward probability is highest.
Decode the paper · Beta-Bernoulli bandit example, written as a single action-selection step
A Tutorial on Thompson SamplingDaniel Russo, Benjamin Van Roy, Abbas Kazerouni, Ian Osband & Zheng Wen · 2018
The tutorial presents Thompson sampling as choosing actions from sampled plausible models. In the Bernoulli example, successes and failures update Beta posteriors. The sampled winner is an exploration strategy, not proof that its true reward probability is highest.
Options
Your turn
Pull three machines and update their Beta posteriors. Compare choosing the largest posterior mean with sampling a plausible success probability for each arm. Observe both uncertainty and accumulated reward.
Interactive lab
Three slot machines
Machine A
0 rewards / 0 pulls
Posterior: Beta(1, 1)
Posterior mean
0.500
Probability of being best
33.33%
Machine B
0 rewards / 0 pulls
Posterior: Beta(1, 1)
Posterior mean
0.500
Probability of being best
33.33%
Machine C
0 rewards / 0 pulls
Posterior: Beta(1, 1)
Posterior mean
0.500
Probability of being best
33.33%
Pulls used: 0/60 · Total rewards: 0. Best-arm probabilities are computed by numerical integration of the posteriors.
Match · Expression ↔ Meaning
Bayesian estimates
Options
Match · Expression ↔ Meaning
Beta updates
Options
Proof puzzle
Beta-Bernoulli conjugacy
Claim
Update a Beta(alpha,beta) prior after s successes and f failures.
Tap lines in the order they should appear. Tap a line in your proof to send it back.
Your proof
- Pick the first line below.
Available lines
Prove it yourself
Gaussian prior becomes L2
Claim
Show MAP with minimises NLL plus .
Your typeset proof appears here.
Coding problems
Problem 22·Warm-up
Update a prior
A Beta(2,2) prior sees 18 successes and 12 failures. Compute the posterior predictive probability of one more success as a reduced fraction.
Problem 23·Standard
Combine Gaussian evidence
The prior on μ is normal with mean 0 and variance 4. Four observations have known noise variance 1 and sample mean 3. Compute the posterior mean as a reduced fraction.
Problem 24·Challenge
Predict a run of successes
After observing data, θ has posterior Beta(3,2). Compute the probability that the next four conditionally independent Bernoulli trials all succeed, integrating over θ. Submit a reduced fraction.
Key takeaways
- A posterior combines a stated prior and likelihood.
- MLE, MAP and posterior mean answer different questions.
- Posterior prediction integrates parameter uncertainty.
- Thompson sampling uses posterior uncertainty to guide exploration.
Checkpoint
Prove it to the labyrinth
Answer every question to clear this chamber. First-try answers earn the most XP.
A Beta(2,3) prior sees three successes and one failure. What is the new alpha parameter?
What is the mean of Beta(5,4)?
A Gaussian prior on weights corresponds to what MAP penalty?
What does the posterior predictive do?
Beta(3,5) has an interior mode. What is it?
Can sixty pulls guarantee identifying the best of any three Bernoulli arms with 95% posterior probability?
End of the chamber
Clear this chamber
- Questions in this chamber (0/9 solved)Next unsolved
- Bonus: Thompson's trial (+50 XP)
- Bonus: Problem 22: Update a prior (+20 XP)
- Bonus: Problem 23: Combine Gaussian evidence (+35 XP)
- Bonus: Problem 24: Predict a run of successes (+50 XP)
- Bonus: Proof: Beta-Bernoulli conjugacy (+25 XP)
- Bonus: Proof: Gaussian prior becomes L2 (+35 XP)
- Bonus: Decode the paper (+25 XP)
- Bonus: Match: Bayesian estimates (+20 XP)
- Bonus: Match: Beta updates (+20 XP)