Thompson Sampling in Reinforcement Learning
Thompson Sampling Mechanics
The Thompson Sampling Loop
You know how to update your beliefs about a single option using a Beta distribution and Bernoulli rewards. Now, let's put that knowledge into action to make decisions. Thompson Sampling provides a simple, elegant loop for choosing between multiple competing options, often called a multi-armed bandit problem.
The core idea is to let the data guide your choices in a probabilistic way. Instead of committing to the option that looks best right now, you sample from your belief about each option. The option that wins this random draw gets picked for the next trial. This naturally balances exploring uncertain options and exploiting ones that have proven successful.
This process is built on a principle called [{
}], where the chance of selecting an arm directly corresponds to the probability that it is the optimal arm. If your model believes Arm A has an 80% chance of being the best, you'll pick it about 80% of the time. This is a powerful, intuitive way to handle the exploration-exploitation trade-off. It avoids the rigid rules of other strategies, like epsilon-greedy, where you explore with a fixed, arbitrary probability. With Thompson Sampling, exploration is a natural consequence of uncertainty. The more uncertain you are about an arm's true value, the wider its posterior distribution will be, and the more chances it has to produce a high sample and get chosen.
The Algorithm Step-by-Step
Let's formalize this into an algorithm. Imagine you have arms, or options, to choose from. For each arm , you maintain a posterior distribution that represents your current belief about its success rate. Since we're dealing with win/loss (Bernoulli) outcomes, we use a Beta distribution for each arm, . Initially, you might start with a prior for all arms, representing complete uncertainty. The loop then proceeds as follows, for each round of decision-making:
| Step | Action | Description |
|---|---|---|
| 1 | Sample | For each arm , draw a random sample from its current posterior distribution: . |
| 2 | Select | Choose the arm with the highest sampled value. This is the 'winning' arm for this round: . |
| 3 | Observe | Pull the selected arm and observe the reward, . The reward will be either 1 (success) or 0 (failure). |
| 4 | Update | Update the posterior for the chosen arm only. If the reward was 1, increment . If the reward was 0, increment . The parameters for all other arms remain unchanged. |
This loop repeats, continuously refining your beliefs. As you gather more data, the Beta distributions for each arm become narrower and more representative of their true success rates. An arm that frequently gives rewards will see its parameter grow, shifting its distribution towards 1. An unsuccessful arm will see its parameter grow, shifting its distribution towards 0.
The selection step, , is where the magic happens. You aren't picking the arm with the highest expected value. Instead, you're giving each arm a chance to prove itself based on a random snapshot of its potential. An arm with high uncertainty (a wide Beta distribution) might occasionally produce a very high sample, giving it a chance to be explored. An arm with a proven track record (a narrow distribution peaked at a high value) will consistently produce high samples, leading to its exploitation.
From Beliefs to Action
Let's consider a simple two-arm bandit problem. Arm A has a posterior of , and Arm B has a posterior of .
- Arm A has seen 2 successes () and 1 failure (). Its expected value is .
- Arm B is unexplored. Its posterior is the uniform prior. Its expected value is .
A purely greedy approach would always pick Arm A. But what if Arm B is actually better? Its uniform distribution means it has a chance, albeit small, of producing a sample higher than Arm A's. Let's say we sample from both:
We choose Arm B, pull it, and observe a success (reward = 1). We then update Arm B's posterior to . Arm A's posterior remains . In the next round, both arms have stronger posteriors, and we repeat the sampling process. This simple mechanism, proposed by back in 1933, is remarkably effective and forms the basis for many modern reinforcement learning systems.
What is the fundamental principle behind Thompson Sampling for a multi-armed bandit problem?
You are using Thompson Sampling for a problem with Bernoulli (win/loss) rewards. You choose an arm, and it results in a 'win' (reward = 1). How do you update its posterior distribution, represented by ?