Bernoulli Bandit and Thompson Sampling Algorithm
Summary
The Beta-Bernoulli bandit is the tutorial’s running example: arms, each paying 1 (success) with unknown probability , learned via a conjugate Beta posterior over each . BernGreedy always plays the arm with highest posterior-mean success probability; BernTS instead draws one sample per arm and plays the arm with the largest sample. This note gives the exact algorithm boxes, the Beta-Bernoulli conjugate update, and the general (non-conjugate) Thompson-sampling and greedy algorithms that later notes specialize to shortest-path, contextual, and linear-bandit problems.
Overview
Every algorithm in the tutorial follows the same generic “online decision algorithm” loop (Fig. 2.1): fit/update a Bayesian model from history, use it to select an action, apply the action, observe an outcome, repeat. Greedy algorithms break this into (1) estimate and (2) act optimally for — with no accounting for estimation uncertainty. Thompson sampling changes only step (1): instead of a point estimate, it draws from the posterior distribution itself, so the randomness in correctly reflects the agent’s residual uncertainty.
Main Content
The Beta-Bernoulli model (Example 3.1)
Beta-Bernoulli Bandit
There are actions. Action produces reward with probability and reward with probability ; is fixed but unknown. Each has an independent prior,
gives the uniform prior on .
Beta-Bernoulli conjugate update
Beta priors are conjugate to Bernoulli likelihoods: after playing action and observing reward ,
are pseudo-counts: (resp. ) increments by one with each observed success (resp. failure) of arm . The mean is ; concentration grows with .
The two algorithms, side by side (Algorithms 3.1-3.2)
BernGreedy
For :
- Estimate model: for , set (the posterior mean).
- Select and apply action: ; apply , observe .
- Update distribution: .
BernTS
For :
- Sample model: for , draw (a posterior sample, not the mean).
- Select and apply action: ; apply , observe .
- Update distribution: .
The only difference between the two algorithms is step 1. This is the general pattern: TS = greedy with the point estimate replaced by a posterior draw.
A common misconception
in BernTS is not a sample of the binary outcome that would occur if arm were played. It is a sample of the success probability itself — a statistically plausible parameter value, not a statistically plausible observation.
The general (non-conjugate) algorithms (Algorithms 4.1-4.2)
TS extends far beyond Beta-Bernoulli. Let the agent apply actions (possibly infinite), observe outcomes , and earn reward for known function . A prior over is updated by Bayes’ rule.
Greedy — Algorithm 4.1
For :
- Estimate model: .
- Select and apply action: ; apply , observe .
- Update distribution: .
Thompson — Algorithm 4.2
For :
- Sample model: .
- Select and apply action: ; apply , observe .
- Update distribution: .
with the Bayes-rule update, for finite -support, . The Beta-Bernoulli algorithms above are the special case , , .
Examples
Three-armed Bernoulli bandit (Fig. 2.2, 3.1-3.2)
With posteriors concentrated near , for arms 1-2 (1000 plays each) and a near-uniform posterior for arm 3 (3 plays, 1 success), a greedy algorithm locks onto arm 1 forever and never learns whether arm 3 (true mean ) is actually better. TS instead samples arms 1/2/3 with probabilities — exactly the posterior probability each is optimal — so it continues to probe arm 3 without wasting effort on arm 2 (posterior probability of being optimal). Over 1000 periods and , TS’s per-period regret vanishes while greedy’s does not (Fig. 3.2).
Independent travel times / shortest path (Example 4.1)
Actions are paths through a graph; each edge has an independent log-Gaussian-distributed mean travel time with , updated in closed form after each traversal by a Gaussian conjugate rule analogous to the Beta update above. The action that maximizes expected reward (minimizes expected cost) is found via Dijkstra’s algorithm on the sampled/estimated edge weights — showing that Algorithms 4.1-4.2 apply even when is exponentially large, as long as the maximization step is tractable. See Contextual and Linear Bandits for the linear/generalized-linear versions of this same pattern.
Connections
- Both algorithms instantiate the generic online-decision loop of Multi-Armed Bandits and Thompson Sampling - Overview; only the model-estimation step (mean vs. sample) differs.
- The general Algorithm 4.2 is exactly what gets specialized to the linear and generalized linear reward models in Contextual and Linear Bandits, and to cascading bandits (CascadeTS) in UCB and Greedy Algorithms for Bandits.
- When exact conjugate posteriors are unavailable (e.g. binary feedback on travel times, logistic reward models), Algorithm 4.2’s sampling step is approximated — see Approximate Thompson Sampling and Practical Extensions.
- The resulting regret behavior of BernTS vs. BernGreedy is formalized in Regret Bounds for Thompson Sampling.
See Also
- Multi-Armed Bandits and Thompson Sampling - Overview — problem setup and why greedy fails
- UCB and Greedy Algorithms for Bandits — the optimistic (UCB) alternative to sampling
- Regret Bounds for Thompson Sampling — why probability matching controls regret
- Contextual and Linear Bandits — the general algorithm applied to linear/GLM reward models