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 ss, actions aa, a policy πθ(a∣s)\pi_\theta(a \mid s) parameterized by θ\theta, and a reward function rr. A trajectory is a sequence

τ=(s0,a0,s1,a1,…,sT), \tau = (s_0, a_0, s_1, a_1, \dots, s_T),

and its total return is R(τ)=∑trtR(\tau) = \sum_{t} r_t.

Our objective is the expected return under the policy:

J(θ)=Eτ∼πθ[R(τ)]=∫R(τ) P(τ;θ) dτ. J(\theta) = \mathbb{E}_{\tau \sim \pi_\theta}\big[R(\tau)\big] = \int R(\tau)\, P(\tau; \theta)\, d\tau.

The log-derivative trick

We want ∇θJ(θ)\nabla_\theta J(\theta), but θ\theta appears inside the distribution P(τ;θ)P(\tau; \theta). Move the gradient inside:

∇θJ(θ)=∫R(τ) ∇θP(τ;θ) dτ. \nabla_\theta J(\theta) = \int R(\tau)\, \nabla_\theta P(\tau; \theta)\, d\tau.

The score function (or log-derivative) identity

∇θP(τ;θ)=P(τ;θ) ∇θlog⁡P(τ;θ) \nabla_\theta P(\tau; \theta) = P(\tau; \theta)\, \nabla_\theta \log P(\tau; \theta)

turns this into an expectation we can sample:

∇θJ(θ)=Eτ[R(τ) ∇θlog⁡P(τ;θ)]. \nabla_\theta J(\theta) = \mathbb{E}_{\tau}\Big[R(\tau)\, \nabla_\theta \log P(\tau; \theta)\Big].

Expanding the trajectory likelihood

The trajectory probability factors as

P(τ;θ)=p(s0)∏t=0T−1πθ(at∣st) p(st+1∣st,at). P(\tau; \theta) = p(s_0) \prod_{t=0}^{T-1} \pi_\theta(a_t \mid s_t)\, p(s_{t+1} \mid s_t, a_t).

Taking the log:

log⁡P(τ;θ)=log⁡p(s0)+∑t[log⁡πθ(at∣st)+log⁡p(st+1∣st,at)]. \log P(\tau; \theta) = \log p(s_0) + \sum_{t} \Big[\log \pi_\theta(a_t \mid s_t) + \log p(s_{t+1} \mid s_t, a_t)\Big].

The dynamics p(st+1∣st,at)p(s_{t+1} \mid s_t, a_t) and p(s0)p(s_0) do not depend on θ\theta, so their gradients vanish. What survives is only the policy’s contribution:

∇θlog⁡P(τ;θ)=∑t∇θlog⁡πθ(at∣st). \nabla_\theta \log P(\tau; \theta) = \sum_{t} \nabla_\theta \log \pi_\theta(a_t \mid s_t).

REINFORCE

Substituting back gives the REINFORCE estimator:

∇θJ(θ)=Eτ[(∑t∇θlog⁡πθ(at∣st))R(τ)]. \nabla_\theta J(\theta) = \mathbb{E}_{\tau}\left[\left(\sum_{t} \nabla_\theta \log \pi_\theta(a_t \mid s_t)\right) R(\tau)\right].

In practice we roll out NN trajectories and average:

∇θJ(θ)≈1N∑i=1N∑t∇θlog⁡πθ(at(i)∣st(i)) R(τ(i)). \nabla_\theta J(\theta) \approx \frac{1}{N} \sum_{i=1}^{N} \sum_{t} \nabla_\theta \log \pi_\theta(a_t^{(i)} \mid s_t^{(i)})\, R(\tau^{(i)}).

The update is gradient ascent: θ←θ+α∇θJ(θ)\theta \leftarrow \theta + \alpha \nabla_\theta J(\theta).

Variance and baselines

REINFORCE is unbiased but noisy. Since E[∇θlog⁡P]=0\mathbb{E}[\nabla_\theta \log P] = 0, we can subtract any constant baseline bb without changing the expectation:

∇θJ(θ)=Eτ[∑t∇θlog⁡πθ(at∣st) (R(τ)−b)]. \nabla_\theta J(\theta) = \mathbb{E}_{\tau}\left[\sum_{t} \nabla_\theta \log \pi_\theta(a_t \mid s_t)\, \big(R(\tau) - b\big)\right].

A common choice is the state-value baseline b=V(st)b = V(s_t), which leads toward actor–critic methods — a topic for another post.