# 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.

<div data-sim="specx" data-spec="{&#34;head&#34;:&#34;Policy gradient on a four-armed bandit: softmax preferences, baselines and gradient noise&#34;,&#34;sub&#34;:&#34;Twenty logged rewards from each of four arms. Run gradient ascent on the softmax preferences and watch probability flow to the best arm; shift all rewards and see why a baseline matters.&#34;,&#34;params&#34;:[{&#34;id&#34;:&#34;alpha&#34;,&#34;label&#34;:&#34;Step size α&#34;,&#34;min&#34;:0.01,&#34;max&#34;:1,&#34;step&#34;:0.01,&#34;value&#34;:0.1,&#34;note&#34;:&#34;how far preferences move per update&#34;},{&#34;id&#34;:&#34;shift&#34;,&#34;label&#34;:&#34;Constant added to every reward&#34;,&#34;min&#34;:-6,&#34;max&#34;:6,&#34;step&#34;:0.5,&#34;value&#34;:0,&#34;note&#34;:&#34;a baseline makes learning blind to this&#34;}],&#34;data&#34;:{&#34;columns&#34;:[&#34;t&#34;,&#34;a1&#34;,&#34;a2&#34;,&#34;a3&#34;,&#34;a4&#34;],&#34;rows&#34;:[[1,2.23,5.48,4.77,5.09],[2,3,3.37,4.48,4.82],[3,4.45,5.63,4.47,4.5],[4,4.95,4.75,6.39,6.39],[5,2.68,4.16,7.36,5.47],[6,4.5,5.32,5.49,7.32],[7,2.63,6.1,5.85,5.73],[8,3.25,5.88,5.27,6.2],[9,4.27,4.84,6.03,6.19],[10,2.87,4.17,4.32,4.38],[11,5.57,3.92,5.64,6.41],[12,0.67,5.26,6.94,5.95],[13,2.9,4.59,4.48,4.96],[14,2.39,3.95,5.22,5.55],[15,2.97,3.8,5.22,6.15],[16,3.27,3.51,4.55,6.11],[17,1.59,3.73,6.19,6.25],[18,4.33,4.88,5.17,6.17],[19,1.96,3.59,6.89,6.92],[20,3.77,3.36,5.43,5.54]],&#34;note&#34;:&#34;Twenty rewards per arm drawn as N(q*, 1) around four arm values (3.2, 4.5, 5.5, 5.7) of a +4-shifted 10-armed testbed, the setting of Sutton and Barto&#39;s Figure 2.5. Paste rewards from your own A/B or bandit logs, one column per arm.&#34;},&#34;pre&#34;:[{&#34;id&#34;:&#34;m1&#34;,&#34;label&#34;:&#34;Mean reward, arm 1&#34;,&#34;expr&#34;:&#34;mean(a1) + shift&#34;,&#34;tex&#34;:&#34;q(a) \\approx \\bar R_a = \\frac{1}{n}\\sum_t R_{t,a}&#34;},{&#34;id&#34;:&#34;v1&#34;,&#34;label&#34;:&#34;Reward variance, arm 1&#34;,&#34;expr&#34;:&#34;vari(a1)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;m2&#34;,&#34;label&#34;:&#34;Mean reward, arm 2&#34;,&#34;expr&#34;:&#34;mean(a2) + shift&#34;},{&#34;id&#34;:&#34;v2&#34;,&#34;label&#34;:&#34;Reward variance, arm 2&#34;,&#34;expr&#34;:&#34;vari(a2)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;m3&#34;,&#34;label&#34;:&#34;Mean reward, arm 3&#34;,&#34;expr&#34;:&#34;mean(a3) + shift&#34;},{&#34;id&#34;:&#34;v3&#34;,&#34;label&#34;:&#34;Reward variance, arm 3&#34;,&#34;expr&#34;:&#34;vari(a3)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;m4&#34;,&#34;label&#34;:&#34;Mean reward, arm 4&#34;,&#34;expr&#34;:&#34;mean(a4) + shift&#34;},{&#34;id&#34;:&#34;v4&#34;,&#34;label&#34;:&#34;Reward variance, arm 4&#34;,&#34;expr&#34;:&#34;vari(a4)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;mr1&#34;,&#34;label&#34;:&#34;Logged mean, arm 1&#34;,&#34;expr&#34;:&#34;mean(a1)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;mr2&#34;,&#34;label&#34;:&#34;Logged mean, arm 2&#34;,&#34;expr&#34;:&#34;mean(a2)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;mr3&#34;,&#34;label&#34;:&#34;Logged mean, arm 3&#34;,&#34;expr&#34;:&#34;mean(a3)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;mr4&#34;,&#34;label&#34;:&#34;Logged mean, arm 4&#34;,&#34;expr&#34;:&#34;mean(a4)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;sd4&#34;,&#34;label&#34;:&#34;Reward spread, arm 4&#34;,&#34;expr&#34;:&#34;sqrt(v4)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;qmax&#34;,&#34;label&#34;:&#34;Best mean reward&#34;,&#34;expr&#34;:&#34;max(max(m1, m2), max(m3, m4))&#34;,&#34;hide&#34;:true}],&#34;row&#34;:[{&#34;id&#34;:&#34;r4&#34;,&#34;label&#34;:&#34;arm 4 reward after shift&#34;,&#34;expr&#34;:&#34;a4 + shift&#34;}],&#34;derived&#34;:[{&#34;id&#34;:&#34;e1&#34;,&#34;label&#34;:&#34;exp H1&#34;,&#34;expr&#34;:&#34;exp(h1 - max(max(h1, h2), max(h3, h4)))&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;e2&#34;,&#34;label&#34;:&#34;exp H2&#34;,&#34;expr&#34;:&#34;exp(h2 - max(max(h1, h2), max(h3, h4)))&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;e3&#34;,&#34;label&#34;:&#34;exp H3&#34;,&#34;expr&#34;:&#34;exp(h3 - max(max(h1, h2), max(h3, h4)))&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;e4&#34;,&#34;label&#34;:&#34;exp H4&#34;,&#34;expr&#34;:&#34;exp(h4 - max(max(h1, h2), max(h3, h4)))&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;zs&#34;,&#34;label&#34;:&#34;Softmax total&#34;,&#34;expr&#34;:&#34;e1 + e2 + e3 + e4&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;p1&#34;,&#34;label&#34;:&#34;π(arm 1)&#34;,&#34;expr&#34;:&#34;e1/zs&#34;,&#34;fmt&#34;:&#34;p&#34;,&#34;tex&#34;:&#34;\\pi(a) = \\frac{e^{H(a)}}{\\sum_b e^{H(b)}}&#34;},{&#34;id&#34;:&#34;p2&#34;,&#34;label&#34;:&#34;π(arm 2)&#34;,&#34;expr&#34;:&#34;e2/zs&#34;,&#34;fmt&#34;:&#34;p&#34;},{&#34;id&#34;:&#34;p3&#34;,&#34;label&#34;:&#34;π(arm 3)&#34;,&#34;expr&#34;:&#34;e3/zs&#34;,&#34;fmt&#34;:&#34;p&#34;},{&#34;id&#34;:&#34;p4&#34;,&#34;label&#34;:&#34;π(arm 4)&#34;,&#34;expr&#34;:&#34;e4/zs&#34;,&#34;fmt&#34;:&#34;p&#34;},{&#34;id&#34;:&#34;jr&#34;,&#34;label&#34;:&#34;Expected reward J&#34;,&#34;expr&#34;:&#34;p1*m1 + p2*m2 + p3*m3 + p4*m4&#34;,&#34;tex&#34;:&#34;J(H) = \\sum_a \\pi(a)\\, q(a)&#34;},{&#34;id&#34;:&#34;g1&#34;,&#34;label&#34;:&#34;Gradient for H1&#34;,&#34;expr&#34;:&#34;p1*(m1 - jr)&#34;,&#34;tex&#34;:&#34;\\frac{\\partial J}{\\partial H(a)} = \\pi(a)\\,\\big(q(a) - J\\big)&#34;},{&#34;id&#34;:&#34;g2&#34;,&#34;label&#34;:&#34;Gradient for H2&#34;,&#34;expr&#34;:&#34;p2*(m2 - jr)&#34;},{&#34;id&#34;:&#34;g3&#34;,&#34;label&#34;:&#34;Gradient for H3&#34;,&#34;expr&#34;:&#34;p3*(m3 - jr)&#34;},{&#34;id&#34;:&#34;g4&#34;,&#34;label&#34;:&#34;Gradient for H4&#34;,&#34;expr&#34;:&#34;p4*(m4 - jr)&#34;},{&#34;id&#34;:&#34;regret&#34;,&#34;label&#34;:&#34;Regret per step&#34;,&#34;expr&#34;:&#34;qmax - jr&#34;,&#34;tex&#34;:&#34;\\text{regret} = \\max_a q(a) - J&#34;},{&#34;id&#34;:&#34;s2&#34;,&#34;label&#34;:&#34;Sum of squared probabilities&#34;,&#34;expr&#34;:&#34;p1^2 + p2^2 + p3^2 + p4^2&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;gn2&#34;,&#34;label&#34;:&#34;Squared length of the true gradient&#34;,&#34;expr&#34;:&#34;g1^2 + g2^2 + g3^2 + g4^2&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;var0&#34;,&#34;label&#34;:&#34;Gradient noise, no baseline&#34;,&#34;expr&#34;:&#34;p1*(v1 + (m1 - 0)^2)*(1 - 2*p1 + s2) + p2*(v2 + (m2 - 0)^2)*(1 - 2*p2 + s2) + p3*(v3 + (m3 - 0)^2)*(1 - 2*p3 + s2) + p4*(v4 + (m4 - 0)^2)*(1 - 2*p4 + s2) - gn2&#34;,&#34;tex&#34;:&#34;\\mathbb{V}[\\hat g] = \\sum_a \\pi(a)\\,\\mathbb{E}\\big[(R-b)^2\\mid a\\big]\\,\\lVert e_a-\\pi\\rVert^2 - \\lVert\\nabla J\\rVert^2&#34;},{&#34;id&#34;:&#34;varb&#34;,&#34;label&#34;:&#34;Gradient noise, baseline b = J&#34;,&#34;expr&#34;:&#34;p1*(v1 + (m1 - jr)^2)*(1 - 2*p1 + s2) + p2*(v2 + (m2 - jr)^2)*(1 - 2*p2 + s2) + p3*(v3 + (m3 - jr)^2)*(1 - 2*p3 + s2) + p4*(v4 + (m4 - jr)^2)*(1 - 2*p4 + s2) - gn2&#34;},{&#34;id&#34;:&#34;vratio&#34;,&#34;label&#34;:&#34;Noise ratio (no baseline / baseline)&#34;,&#34;expr&#34;:&#34;var0/varb&#34;,&#34;note&#34;:&#34;times more noise without the baseline&#34;},{&#34;id&#34;:&#34;jraw&#34;,&#34;label&#34;:&#34;J on the logged reward scale&#34;,&#34;expr&#34;:&#34;jr - shift&#34;,&#34;hide&#34;:true}],&#34;row2&#34;:[{&#34;id&#34;:&#34;den4&#34;,&#34;label&#34;:&#34;π-weighted density, arm 4&#34;,&#34;expr&#34;:&#34;p4*phi((a4 - mr4)/sd4)/sd4&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;push&#34;,&#34;label&#34;:&#34;push on H4 if arm 4 drew this&#34;,&#34;expr&#34;:&#34;(r4 - jr)*(1 - p4)&#34;,&#34;tex&#34;:&#34;\\Delta H(A_t) = \\alpha\\,(R_t - \\bar R)\\,\\big(1 - \\pi(A_t)\\big)&#34;},{&#34;id&#34;:&#34;push0&#34;,&#34;label&#34;:&#34;same push with no baseline&#34;,&#34;expr&#34;:&#34;r4*(1 - p4)&#34;}],&#34;iterate&#34;:{&#34;state&#34;:[{&#34;id&#34;:&#34;h1&#34;,&#34;label&#34;:&#34;Preference H1&#34;,&#34;init&#34;:&#34;0&#34;},{&#34;id&#34;:&#34;h2&#34;,&#34;label&#34;:&#34;Preference H2&#34;,&#34;init&#34;:&#34;0&#34;},{&#34;id&#34;:&#34;h3&#34;,&#34;label&#34;:&#34;Preference H3&#34;,&#34;init&#34;:&#34;0&#34;},{&#34;id&#34;:&#34;h4&#34;,&#34;label&#34;:&#34;Preference H4&#34;,&#34;init&#34;:&#34;0&#34;}],&#34;update&#34;:[{&#34;id&#34;:&#34;h1&#34;,&#34;expr&#34;:&#34;h1 + alpha*g1&#34;},{&#34;id&#34;:&#34;h2&#34;,&#34;expr&#34;:&#34;h2 + alpha*g2&#34;},{&#34;id&#34;:&#34;h3&#34;,&#34;expr&#34;:&#34;h3 + alpha*g3&#34;},{&#34;id&#34;:&#34;h4&#34;,&#34;expr&#34;:&#34;h4 + alpha*g4&#34;}],&#34;max&#34;:1000,&#34;per&#34;:5,&#34;track&#34;:&#34;jr&#34;},&#34;chart&#34;:{&#34;title&#34;:&#34;Reward distribution of each arm, weighted by how often the policy picks it&#34;,&#34;x&#34;:{&#34;var&#34;:&#34;rw&#34;,&#34;label&#34;:&#34;Reward&#34;,&#34;from&#34;:0,&#34;to&#34;:8.5},&#34;series&#34;:[{&#34;label&#34;:&#34;Arm 1 × π&#34;,&#34;expr&#34;:&#34;p1*phi((rw - mr1)/sqrt(v1))/sqrt(v1)&#34;},{&#34;label&#34;:&#34;Arm 2 × π&#34;,&#34;expr&#34;:&#34;p2*phi((rw - mr2)/sqrt(v2))/sqrt(v2)&#34;},{&#34;label&#34;:&#34;Arm 3 × π&#34;,&#34;expr&#34;:&#34;p3*phi((rw - mr3)/sqrt(v3))/sqrt(v3)&#34;},{&#34;label&#34;:&#34;Arm 4 (best) × π&#34;,&#34;expr&#34;:&#34;p4*phi((rw - mr4)/sqrt(v4))/sqrt(v4)&#34;}],&#34;fit&#34;:{&#34;x&#34;:&#34;a4&#34;,&#34;y&#34;:&#34;den4&#34;,&#34;label&#34;:&#34;Logged rewards of arm 4&#34;},&#34;mark&#34;:&#34;jraw&#34;},&#34;level&#34;:&#34;intermediate&#34;,&#34;bars&#34;:{&#34;title&#34;:&#34;Push on the best arm&#39;s preference from each of its logged rewards (baseline = J)&#34;,&#34;y&#34;:&#34;push&#34;,&#34;label&#34;:&#34;t&#34;},&#34;steps&#34;:[{&#34;title&#34;:&#34;Estimate each arm&#39;s value&#34;,&#34;text&#34;:&#34;The mean logged reward of each arm stands in for q(a): {m1}, {m2}, {m3} and {m4}.&#34;,&#34;show&#34;:[&#34;m1&#34;,&#34;m2&#34;,&#34;m3&#34;,&#34;m4&#34;]},{&#34;title&#34;:&#34;Turn preferences into a policy&#34;,&#34;text&#34;:&#34;Softmax over the preferences H gives the policy. Right now the best arm gets {p4:p} of the picks.&#34;,&#34;show&#34;:[&#34;p1&#34;,&#34;p2&#34;,&#34;p3&#34;,&#34;p4&#34;]},{&#34;title&#34;:&#34;Measure the policy&#34;,&#34;text&#34;:&#34;The expected reward is J = {jr}, and the regret against always pulling the best arm is {regret}.&#34;,&#34;show&#34;:[&#34;jr&#34;,&#34;regret&#34;]},{&#34;title&#34;:&#34;Take the gradient&#34;,&#34;text&#34;:&#34;Each preference moves by π(a)(q(a) − J): arms above J go up, arms below go down. For arm 4 that is {g4}, for arm 1 {g1}.&#34;,&#34;show&#34;:[&#34;g1&#34;,&#34;g4&#34;]},{&#34;title&#34;:&#34;Step uphill&#34;,&#34;text&#34;:&#34;Every preference moves by α = {alpha} times its gradient. The policy sharpens and J rises step by step; the chart tracks it.&#34;,&#34;show&#34;:[&#34;jr&#34;]},{&#34;title&#34;:&#34;Why the baseline&#34;,&#34;text&#34;:&#34;From single samples the gradient is noisy. Subtracting b = J leaves its mean unchanged but cuts the noise from {var0} to {varb}, {vratio} times less.&#34;,&#34;show&#34;:[&#34;var0&#34;,&#34;varb&#34;,&#34;vratio&#34;]}]}"><p>Interactive simulation. It needs JavaScript; the text below explains what it shows.</p></div>

## 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

1. Estimate each arm's value q(a) by its mean logged reward: 3.2125, 4.5145, 5.508 and 5.805 here.
2. Turn preferences into probabilities with a softmax: π(a) = e^H(a) / Σ e^H(b).
3. 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.
4. 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.
5. 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](/ai-simulations/ppo-clipped-objective-ratio). 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](/ai-simulations/grpo-reinforcement-learning). 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](/ai-simulations/q-learning-gridworld), and for the wider picture read [reinforcement learning from MDPs to Q-learning and PPO](/articles/reinforcement-learning-mdp-q-learning-ppo) or the [REINFORCE](/tools/reinforce) page.

## Sources

- [Multi-armed bandits, COMP 579 lecture notes (McGill University)](https://cs.mcgill.ca/~comp579/W25/Lectures/3-bandits.pdf)
- [Multi-armed bandits, Reinforcement Learning course (University of Edinburgh)](https://opencourse.inf.ed.ac.uk/sites/default/files/https/opencourse.inf.ed.ac.uk/rl/2024/rl2multiarmedbandits.pdf)
- [Chapter 2 bandits, reproducing Sutton and Barto's Figure 2.5 testbed](https://joseph-jnl.github.io/rlbook/chap2_bandits)
- [Reinforcement learning: MDP, Q-learning and PPO](/articles/reinforcement-learning-mdp-q-learning-ppo)
