Abstract
Policy gradients are the foundation of a whole family of reinforcement learning algorithms. This post derives the core estimator from scratch — no black boxes.
Setup
We have a Markov decision process with states s, actions a, a policy πθ(a∣s) parameterized by θ, and a reward function r. A trajectory is a sequence
τ=(s0,a0,s1,a1,…,sT),and its total return is R(τ)=∑trt.
Our objective is the expected return under the policy:
J(θ)=Eτ∼πθ[R(τ)]=∫R(τ)P(τ;θ)dτ.The log-derivative trick
We want ∇θJ(θ), but θ appears inside the distribution P(τ;θ). Move the gradient inside:
∇θJ(θ)=∫R(τ)∇θP(τ;θ)dτ.The score function (or log-derivative) identity
∇θP(τ;θ)=P(τ;θ)∇θlogP(τ;θ)turns this into an expectation we can sample:
∇θJ(θ)=Eτ[R(τ)∇θlogP(τ;θ)].Expanding the trajectory likelihood
The trajectory probability factors as
P(τ;θ)=p(s0)t=0∏T−1πθ(at∣st)p(st+1∣st,at).Taking the log:
logP(τ;θ)=logp(s0)+t∑[logπθ(at∣st)+logp(st+1∣st,at)].The dynamics p(st+1∣st,at) and p(s0) do not depend on θ, so their gradients vanish. What survives is only the policy’s contribution:
∇θlogP(τ;θ)=t∑∇θlogπθ(at∣st).REINFORCE
Substituting back gives the REINFORCE estimator:
∇θJ(θ)=Eτ[(t∑∇θlogπθ(at∣st))R(τ)].In practice we roll out N trajectories and average:
∇θJ(θ)≈N1i=1∑Nt∑∇θlogπθ(at(i)∣st(i))R(τ(i)).The update is gradient ascent: θ←θ+α∇θJ(θ).
Variance and baselines
REINFORCE is unbiased but noisy. Since E[∇θlogP]=0, we can subtract any constant baseline b without changing the expectation:
∇θJ(θ)=Eτ[t∑∇θlogπθ(at∣st)(R(τ)−b)].A common choice is the state-value baseline b=V(st), which leads toward actor–critic methods — a topic for another post.