Skip to content
StackDecode
AI & ML Atlas
Reinforcement LearningalgorithmInteractive simulation

Thompson Sampling

Pick each arm with the probability that it is the best, by sampling from beliefs.

The idea

Keep a posterior distribution over each arm's success rate (for coin-like rewards, a Beta distribution). Each round draw one sample from every arm's posterior and play the arm with the highest sample. Uncertain arms produce widely varying samples, so they get explored, and good arms soon dominate.

θᵢ ~ Beta(αᵢ, βᵢ), play argmax θᵢ

Watch it run

This is a real computation, not a drawing. Pause, step, change the settings, or try new random data.

total regret (reward lost by not always playing the best arm)pulled 0×arm 1pulled 1×arm 2pulled 0×arm 3pulled 0×arm 4pulled 0×arm 5
  • estimated value
  • true value (dashed)
  • arm just pulled

Pull 1: Draw one plausible value for each arm from its belief and play the best draw. Uncertain arms get drawn high often enough to be tested. Got reward 1.29.

pulls:
1
regret:
0.58
1/40

Bars are what the agent believes; dashed lines are the truth it is trying to discover.

Step by step

  1. 1Start every arm with a Beta(1, 1) prior.
  2. 2Sample a value from each arm's distribution.
  3. 3Pull the arm with the largest sample.
  4. 4Update that arm's Beta with the success or failure.

Where it is used

Online experiments, recommendation and ad placement.

Watch out for

Needs a sensible prior and a conjugate or approximate posterior.

Related