Policy gradient on a four-armed bandit: softmax preferences, baselines and gradient noise
Policy gradient methods, from REINFORCE to the PPO and GRPO used to train language models, all start from one idea: raise the probability of actions that beat a baseline. The gradient bandit is that idea at its smallest, with four arms, four numbers to learn and an exact gradient you can check by hand.
Interactive simulation. It needs JavaScript; the text below explains what it shows.
What you are looking at
A multi-armed bandit is the simplest reinforcement learning problem: there is no state, only a choice of arm and a noisy reward. The table holds twenty logged rewards from each of four arms, drawn as N(q*, 1) around 3.2, 4.5, 5.5 and 5.7. Those four values come from a 10-armed testbed shifted up by +4, the setting Sutton and Barto use in their Figure 2.5 to show why a baseline matters. You can paste rewards from your own A/B test or bandit logs, one column per arm.
The policy is a softmax over four preferences H1 to H4, all starting at 0, so each arm is picked 25% of the time. Press run and the preferences climb the gradient of the expected reward. The chart draws each arm's reward distribution scaled by how often the policy picks it: as learning goes on, the best arm's curve grows and the others shrink. The vertical line is the expected reward J, and the dots are arm 4's logged rewards. The bars show the push each logged reward of arm 4 would give its preference: positive above the baseline, negative below it.
The algorithm
- Estimate each arm's value q(a) by its mean logged reward: 3.2125, 4.5145, 5.508 and 5.805 here.
- Turn preferences into probabilities with a softmax: π(a) = e^H(a) / Σ e^H(b).
- The expected reward is J = Σ π(a) q(a). At the start it is 4.76, and the regret against always pulling arm 4 is 5.805 − 4.76 = 1.045.
- The gradient of J with respect to each preference is π(a)(q(a) − J). Arms better than average are pushed up, worse ones pushed down. At the start, arm 1 gets 0.25 × (3.2125 − 4.76) = −0.3869 and arm 4 gets 0.25 × (5.805 − 4.76) = 0.2613.
- Each preference moves by α times its gradient, and the loop repeats.
The sampled version in Sutton and Barto's equation (2.12) pulls one arm A_t, sees one reward R_t and updates every preference by α(R_t − R̄_t)(1[a = A_t] − π(a)), where R̄_t is the running average reward. Its expected value equals the exact gradient above, which is why the algorithm is stochastic gradient ascent. The simulation runs the exact expected update, so the curve is the average path the sampled algorithm follows.
The baseline does not change that average, because Σ π(a)(1[a = A] − π(a)) sums to zero, but it changes the noise. The variance of a one-sample gradient is Σ π(a) E[(R − b)² | a] ‖e_a − π‖² − ‖∇J‖². With the default data and uniform policy that is 18.188 with no baseline (b = 0) and 1.1948 with b = J: 15.22 times less noise.
Worked example with α = 0.1: after one step arm 4's probability rises from 25.00% to 25.65%. After 100 steps it is 77.40% and J = 5.644; after 300 steps it is 94.62%, J = 5.769 and the regret is down to 0.036. Arm 3, worth 5.508, is the close competitor that slows the last part of the climb.
Things to try
- Set the reward shift to +4. The learning curve does not change at all (arm 4 still reaches 94.62% after 300 steps) but the no-baseline noise jumps from 18.188 to 58.748 while the baseline noise stays at 1.1948, 49.17 times less.
- Set the shift to −4: rewards now sit near zero and the two noises are close (1.628 against 1.195). A baseline matters most when rewards are far from zero, which is exactly the +4 testbed.
- Raise α to 0.4: arm 4 reaches 98.94% after 300 steps. With exact gradients a bigger step is safe here; with sampled rewards it amplifies the noise.
- Watch the bars at the start: with b = J some of arm 4's rewards push its preference down, because they were unlucky draws. Without a baseline every one of them would push up.
- Edit arm 3's rewards upward until its mean passes arm 4's and watch the policy switch winners.
How it works in real systems
REINFORCE applies the same estimator to full trajectories: the gradient is (return − baseline) times the gradient of log π(action). Actor-critic methods learn the baseline as a value function, and PPO adds a clipped probability ratio on top, see the PPO clipped objective. GRPO, used for reasoning models, drops the value network and uses the mean reward of a group of sampled answers to the same prompt as the baseline, see GRPO. In language-model training the arms become whole answers and the probabilities come from the model, but the core move is the one here: compare a reward with a baseline and nudge the log-probability up or down.
For a value-based contrast, try Q-learning on a grid world, and for the wider picture read reinforcement learning from MDPs to Q-learning and PPO or the REINFORCE page.