Policy Gradients Explained — Reinforcement Learning Math
Why learn the policy directly?
Everything in Parts 1–3 learned a value function — a score for every (state, action) pair. The policy was derived from it: pick the action with the highest Q-value.
This works. But it has a hidden assumption: you can find the maximum Q-value over all possible actions.
If you have 4 actions (up, down, left, right), taking the argmax is trivial. If you have 1,000 possible actions, it's slow but doable. If actions are continuous — any real number in a range — the argmax over an infinite set is impossible.
There's also a deeper problem: value-based methods produce deterministic policies. In some problems, the optimal policy is stochastic — sometimes you should go left, sometimes right, depending on something the state doesn't capture. A deterministic argmax can never learn that.
Policy gradient methods bypass the value function entirely. They directly parameterise and optimise the policy. This chapter shows you the theorem that makes this possible — and why it's true.
Parameterising the policy
A parameterised policy is a neural network that maps states to action probabilities:
The goal is to find the parameters θ that maximise the expected total reward:
Where
We want to compute
The question is: how do you compute this gradient? The expected reward involves the environment's transition probabilities, which we don't know.
The Policy Gradient Theorem
Here is the theorem:
Where:
= the direction to adjust θ to make action more/less likely in state = the actual return received from step onward - The expectation
means we average this over many sampled trajectories
In plain English: to increase expected reward, adjust the policy parameters in the direction that makes good actions (those followed by high returns) more likely, and bad actions (those followed by low returns) less likely.
This is the formal version of "do more of what worked." The gradient tells you exactly how much to push each parameter.
Why log probability? The mathematical reason
This might seem arbitrary. Why
The answer comes from a simple identity:
This looks like a rearrangement — but it's crucial. When you compute the gradient of the expected reward, the environment's transition probabilities appear in the expectation. They cancel out when you use the log-probability form, leaving an expression that only depends on things we can compute: the policy itself and the observed rewards.
You can verify this makes the gradient computable without knowing the environment dynamics — which is exactly what we need for model-free learning.
REINFORCE: the simplest policy gradient algorithm
The theorem immediately gives us an algorithm. Run episodes, collect returns, update the policy:
def reinforce_update(policy, optimizer, episode, gamma=0.99):
# Compute discounted returns for each timestep
returns = []
G = 0
for reward in reversed(episode.rewards):
G = reward + gamma * G
returns.insert(0, G)
returns = torch.tensor(returns)
# Normalise returns (reduces variance)
returns = (returns - returns.mean()) / (returns.std() + 1e-8)
# Policy gradient update
loss = 0
for log_prob, G in zip(episode.log_probs, returns):
loss -= log_prob * G # negative because we do gradient ascent
optimizer.zero_grad()
loss.backward()
optimizer.step()This is REINFORCE (Williams, 1992) — one of the oldest policy gradient algorithms, and still the clearest expression of the theorem.
The variance problem — and why Actor-Critic fixes it
REINFORCE works, but it's slow. The reason: G_t is very noisy.
One episode, the agent gets lucky and G_t is high. The next episode, it's unlucky and G_t is low. The policy gradient update swings back and forth with this noise, making training unstable.
The fix is the baseline: subtract an estimate b(s) from the return before multiplying.
Crucially, the baseline does not change the expected gradient — it only reduces its variance. This is provable:
The proof:
The best baseline is
In plain English: not "how good was this action?" but "how much better was this action than what I'd expect on average in this state?"
This is exactly what the Critic computes in Actor-Critic methods. The Critic estimates
Connecting to every algorithm that follows
Every policy gradient algorithm in this course is a refinement of REINFORCE:
| Algorithm | What they change |
|---|---|
| REINFORCE | Raw return |
| Actor-Critic | Replace |
| A2C | Multiple parallel environments; synchronous updates |
| PPO | Clip the policy update ratio to prevent large jumps |
| SAC | Add entropy bonus; Gaussian policy for continuous actions |
They all share the same gradient direction from the theorem. They differ only in how they estimate