Policy Gradient Theorem
You can improve a policy without modeling the world by following the gradient of expected reward, estimated from the rewards your own actions actually earned.
Why Does This Exist?
Value methods learn by bootstrapping predictions of future reward, which breaks when function approximation distorts those predictions. Policy methods skip the middleman: they tune the action probabilities directly to earn more. But differentiating expected reward looks impossible at first, because the expectation depends on unknown transition dynamics.
The policy gradient theorem removes the dynamics from the derivative. It shows the gradient depends only on the policy's own score function and the returns actually observed, so sampling episodes suffices. REINFORCE, actor-critic, and PPO all rest on this identity. This page proves the shape and shows the variance fix.
The one idea you need first
The score function is : how a parameter nudge changes the log probability of an action. Its expectation under the policy is zero, which is why it can weight returns without bias. Expectation, variance and covariance cover the averaging rules used below.
Think of It Like This
Tipping restaurant dishes by profit
You run a food stall with three dishes and want more profit. Each day you note which dishes customers picked and the day's takings. To adjust the menu, you push probability toward dishes sold on high-profit days and away from dishes sold on losing days, scaled by how surprising each sale was.
Returns are the daily takings, the score function is the surprise adjustment, and the policy is the menu mix. Where the analogy stops: real menus change customer arrivals (the dynamics), but the theorem shows you never need that arrival model to compute the direction.
How It Actually Works
For episodic return and parameters , the theorem states:
In plain words: weight each action's score direction by the return that followed it, then average over experience. High-return actions get reinforced along their score vector; low-return actions get pushed away. The environment dynamics never appear because differentiating the log probability cancels them through the likelihood-ratio trick.
REINFORCE samples this directly: run an episode, and for each step add . Variance is brutal, so actor-critic subtracts a baseline , usually the value estimate, without bias:
Worked example
One state, two actions with logits , so . Action Up always returns , Down returns . Sample Up once.
Score for Up under softmax: . REINFORCE step with rate : . New logits give . One good episode moved Up from 50 to 73 percent. With baseline (mean return), the weight drops to and the step lands at , smaller and steadier.
Code
import numpy as np
def softmax(z: np.ndarray) -> np.ndarray: e = np.exp(z - z.max()) return e / e.sum()
theta = np.array([0.0, 0.0])p = softmax(theta) # [0.5, 0.5]score_up = np.array([1 - p[0], -p[1]]) # grad log pi(Up)theta_new = theta + 0.1 * 10.0 * score_up # REINFORCE, G=10print(softmax(theta_new)) # -> [0.73105858 0.26894142]The printed probabilities match the hand step: 0.731 for Up after one reinforced episode.
Watch Out For
Forgetting the baseline must not depend on the action
A baseline that uses the sampled action, like the return of a different action in the same state, leaks bias into the gradient. The proof needs to depend on state only so its score-weighted expectation stays zero. Action-dependent bonuses silently tilt the policy.
Use or a running average of returns, never the advantage of a cherry-picked action.
Sampling with too large a step
Policy gradients are noisy, and a step of 0.1 on a 10-return signal moves probabilities by 20 points, as the example shows. Two such steps in a row can collapse exploration to a near-deterministic policy before values are known. Loss looks great while evaluation on new starts fails.
Keep steps small, clip updates as PPO does, or normalize returns per batch. If entropy collapses in the first hundred episodes, the rate is the suspect.
The Quick Version
- The policy gradient is expected return times the score function, with no dynamics model needed.
- REINFORCE samples it from complete episodes; actor-critic subtracts a state baseline to cut variance.
- The one-state example moves Up from 0.50 to 0.73 on a single return of 10.
- Baselines must depend on state only, or bias creeps in.
- Small steps or clipping protect against premature entropy collapse.