Simple Algorithm
Artificial Intelligence · Deep Layer · 28 min

The Policy Gradient Theorem

REINFORCE, baselines and variance.

The policy-gradient theorem contains a small miracle: the gradient of expected return involves no derivative of the state distribution, although every parameter change reshapes where the agent goes. From that identity follow REINFORCE, the theory of baselines, compatible critics, the natural gradient, and, despite non-concavity, proofs that policy gradients find globally optimal policies.

What you’ll learn

  • Prove the policy-gradient theorem for discounted MDPs from the Bellman equation, and explain the role of the discounted occupancy measure.
  • Derive the variance-optimal state-dependent baseline, and show that the value function can be a poor baseline.
  • Prove the compatible-function-approximation theorem and its corollary that compatible critic weights are the natural gradient.
  • Prove the performance-difference lemma and the O(1/T)\order{1/T} global convergence rate of natural policy gradient for softmax policies.

The theorem and its proof

DerivationProof
  1. ∇Vπ(s)=∑a∇πθ(a∣s) Qπ(s,a)⏟ϕ(s)+γ∑s′Pπ(s′∣s) ∇Vπ(s′)\nabla V^\pi(s)=\underbrace{\sum_a\nabla\pi_\theta(a\mid s)\,Q^\pi(s,a)}_{\phi(s)}+\gamma\sum_{s'}P_\pi(s'\mid s)\,\nabla V^\pi(s')

    Differentiate Vπ(s)=∑aπθ(a∣s)Qπ(s,a)V^\pi(s)=\sum_a\pi_\theta(a\mid s)Q^\pi(s,a) with the product rule. Rewards do not depend on θ\theta, so ∇Qπ(s,a)=γ∑s′P(s′∣s,a)∇Vπ(s′)\nabla Q^\pi(s,a)=\gamma\sum_{s'}P(s'\mid s,a)\nabla V^\pi(s'):

  2. ∇Vπ(s)=∑t≥0γt∑xPr⁡(st=x∣s0=s,π) ϕ(x)\nabla V^\pi(s)=\sum_{t\ge0}\gamma^t\sum_x\Pr(s_t=x\mid s_0=s,\pi)\,\phi(x)

    This is a Bellman equation for the gradient, with "reward" ϕ\phi. Solve it as for any value function, ∇Vπ=(I−γPπ)−1ϕ\nabla V^\pi=(I-\gamma P_\pi)^{-1}\phi, and expand the Neumann series:

  3. ∇J=11−γ∑xdρπ(x)∑a∇πθ(a∣x) Qπ(x,a)=11−γ Edρπ,π[Qπ ∇log⁡πθ]\nabla J=\frac1{1-\gamma}\sum_xd^\pi_\rho(x)\sum_a\nabla\pi_\theta(a\mid x)\,Q^\pi(x,a)=\frac1{1-\gamma}\,\E_{d^\pi_\rho,\pi}\big[Q^\pi\,\nabla\log\pi_\theta\big]

    Average over s0∼ρs_0\sim\rho; the discounted state probabilities sum to dρπ/(1−γ)d^\pi_\rho/(1-\gamma). Finally write ∇π=π ∇log⁡π\nabla\pi=\pi\,\nabla\log\pi:

The state distribution depends on θ\theta, yet its derivative never appeared: it was absorbed when the gradient's Bellman recursion was unrolled. The price is that states must be weighted by dρπd^\pi_\rho, which discounts later visits. Most implementations weight every visited state equally, dropping the factor γt\gamma^t; the resulting update is not the gradient of the discounted objective, and in general not the gradient of any function (Thomas, 2014; Nota & Thomas, 2020). The usual justification reads γ\gamma as a variance-reduction device for an undiscounted objective and accepts the bias (Schulman et al., 2016).

Estimators, baselines and variance

REINFORCE estimates the gradient from a trajectory by g^=∑tγt∇log⁡πθ(at∣st)(Gt−b(st))\hat g=\sum_t\gamma^t\nabla\log\pi_\theta(a_t\mid s_t)\big(G_t-b(s_t)\big), unbiased for any baseline that does not depend on ata_t, because Ea∼π[∇log⁡π(a∣s)]=0\E_{a\sim\pi}[\nabla\log\pi(a\mid s)]=0. Its variance grows with the horizon, since GtG_t sums many random rewards; discounting, baselines and bootstrapped critics all trade bias for variance. Which baseline is best?

DerivationProof
  1. E∥g (Q−b)∥2=E[∥g∥2Q2]−2b E[∥g∥2Q]+b2 E∥g∥2\E\norm{g\,(Q-b)}^2=\E\big[\norm g^2Q^2\big]-2b\,\E\big[\norm g^2Q\big]+b^2\,\E\norm g^2

    The mean E[g (Q−b)]=E[gQ]\E[g\,(Q-b)]=\E[gQ] does not depend on bb, because E[g]=0\E[g]=0. So the variance depends on bb only through the second moment, a quadratic in bb:

  2. E∥g (Q−b)∥2=E∥g (Q−b∗)∥2+E∥g∥2 (b−b∗)2\E\norm{g\,(Q-b)}^2=\E\norm{g\,(Q-b^*)}^2+\E\norm g^2\,\big(b-b^*\big)^2

    Complete the square:

  3. So b∗b^* is a ∥g∥2\norm g^2-weighted average of QQ, not the plain average Vπ(s)=EπQV^\pi(s)=\E_\pi Q. The two coincide when ∥g∥2\norm g^2 is the same for every action, which fails for softmax policies: rare actions have large scores.

Predict before you look

A two-action softmax policy with tabular logits has π=(0.8, 0.2)\pi=(0.8,\,0.2) and Q=(1, 0)Q=(1,\,0) in some state, so Vπ=0.8V^\pi=0.8. What is the variance-optimal baseline b∗b^*?

Show the answer

0.2

In logit space g(a1)=(0.2,−0.2)g(a_1)=(0.2,-0.2) and g(a2)=(−0.8,0.8)g(a_2)=(-0.8,0.8), with ∥g∥2=0.08\norm g^2=0.08 and 1.281.28. Then b∗=0.8⋅0.08⋅1+0.2⋅1.28⋅00.8⋅0.08+0.2⋅1.28=0.0640.32=0.2b^*=\frac{0.8\cdot0.08\cdot1+0.2\cdot1.28\cdot0}{0.8\cdot0.08+0.2\cdot1.28}=\frac{0.064}{0.32}=0.2. With two actions the scores are collinear, and b∗b^* makes both samples equal to (0.16,−0.16)(0.16,-0.16): zero variance. The value baseline b=0.8b=0.8 gives a total variance of 0.115, nine times that of no baseline at all (0.0128).

So the value function is a convenient, often near-optimal baseline, but not the optimal one. Baselines also matter beyond variance: with sampled updates, the baseline changes the learning dynamics themselves, for example how quickly the policy commits to one action (Chung et al., 2021). Action-dependent baselines (control variates with a correction term, such as Q-Prop; Gu et al., 2017) can remove more variance in principle, but careful measurements found that in common benchmarks the reported gains came from implementation differences rather than variance reduction: most of the variance comes from states and future trajectories, not from the current action (Tucker et al., 2018).

Compatible critics and the natural gradient

DerivationProof
  1. Ed,π[(Qπ−fw∗) ∇log⁡πθ]=0⟹Ed,π[Qπ ∇log⁡πθ]=Ed,π[fw∗∇log⁡πθ]\E_{d,\pi}\big[(Q^\pi-f_{w^*})\,\nabla\log\pi_\theta\big]=0\quad\Longrightarrow\quad\E_{d,\pi}\big[Q^\pi\,\nabla\log\pi_\theta\big]=\E_{d,\pi}\big[f_{w^*}\nabla\log\pi_\theta\big]

    A least-squares optimum makes the residual orthogonal to the features, and here the features are the score vectors:

  2. E[∇log⁡πθ ∇log⁡πθ⊤] w∗=(1−γ) ∇J⟹w∗=(1−γ) F−1∇J\E\big[\nabla\log\pi_\theta\,\nabla\log\pi_\theta^\top\big]\,w^*=(1-\gamma)\,\nabla J\quad\Longrightarrow\quad w^*=(1-\gamma)\,F^{-1}\nabla J

    By the theorem the left side is (1−γ)∇J(1-\gamma)\nabla J, which proves the first claim. The right side is F(θ) w∗F(\theta)\,w^*:

  3. Since Ea∼πfw(s,a)=0\E_{a\sim\pi}f_w(s,a)=0 for every ww, a compatible critic represents only advantages. Compatibility is restrictive, a critic linear in features fixed by the actor, so practical critics are not compatible and their gradients are biased. Konda and Tsitsiklis (2000) proved convergence of two-timescale actor–critic with compatible features.

Why F−1∇JF^{-1}\nabla J? The ordinary gradient is steepest for the Euclidean length of a parameter change, which depends on how the policy happens to be parameterized. Measure a step instead by how much it changes the policy. To second order, DKL(πθ(⋅∣s) ∥ πθ+Δ(⋅∣s))=12Δ⊤Fs(θ)Δ+O(∥Δ∥3)\KL\big(\pi_\theta(\cdot\mid s)\,\|\,\pi_{\theta+\Delta}(\cdot\mid s)\big)=\tfrac12\Delta^\top F_s(\theta)\Delta+\order{\norm\Delta^3}, because the score has mean zero and −E[∇2log⁡π]=E[∇log⁡π ∇log⁡π⊤]-\E[\nabla^2\log\pi]=\E[\nabla\log\pi\,\nabla\log\pi^\top]. Maximizing ∇J⊤Δ\nabla J^\top\Delta subject to 12Δ⊤FΔ≤ε\tfrac12\Delta^\top F\Delta\le\varepsilon gives Δ∝F−1∇J\Delta\propto F^{-1}\nabla J, the natural gradient (Amari, 1998; Kakade, 2002). Its direction is invariant, to first order, under smooth reparameterizations of the policy.

For a tabular softmax policy the natural gradient has a closed form, using the pseudo-inverse of FF because adding a constant to a state's logits changes nothing. The update θ←θ+ηF†∇J\theta\leftarrow\theta+\eta F^\dagger\nabla J adds η1−γAπ(s,a)\frac{\eta}{1-\gamma}A^\pi(s,a) to each logit (Agarwal et al., 2021), so

πt+1(a∣s)∝πt(a∣s) exp⁡(η1−γ Aπt(s,a)),\pi_{t+1}(a\mid s)\propto\pi_t(a\mid s)\,\exp\Big(\frac{\eta}{1-\gamma}\,A^{\pi_t}(s,a)\Big),

a multiplicative-weights, or mirror-descent, step in every state. As η→∞\eta\to\infty it becomes policy iteration.

Global convergence despite non-concavity

J(θ)J(\theta) is not concave, even for tabular softmax policies, yet policy gradients reach global optima. The key is an identity that compares any two policies through advantages.

With π′=π∗\pi'=\pi^* the lemma shows that a policy with no improving direction is optimal on every state that matters. For the direct parameterization (the probabilities themselves), V∗(ρ)−Vπ(ρ)≤11−γ∥dρπ∗/μ∥∞max⁡πˉ(πˉ−π)⊤∇πVπ(μ)V^*(\rho)-V^\pi(\rho)\le\frac{1}{1-\gamma}\norm{d^{\pi^*}_\rho/\mu}_\infty\max_{\bar\pi}(\bar\pi-\pi)^\top\nabla_\pi V^\pi(\mu), a gradient-domination inequality (Agarwal et al., 2021): first-order stationarity implies global optimality. The distribution-mismatch coefficient ∥dρπ∗/μ∥∞\norm{d^{\pi^*}_\rho/\mu}_\infty measures how well the distribution μ\mu used for optimization covers the states the optimal policy visits; exploration enters the convergence rates through it.

DerivationProof: mirror descent, state by state
  1. log⁡Zt(s) ≥ ∑aπt(a∣s) η At(s,a)1−γ=0\log Z_t(s)\ \ge\ \sum_a\pi_t(a\mid s)\,\frac{\eta\,A_t(s,a)}{1-\gamma}=0

    Write At=AπtA_t=A^{\pi_t} and Zt(s)=∑aπt(a∣s) eηAt(s,a)/(1−γ)Z_t(s)=\sum_a\pi_t(a\mid s)\,e^{\eta A_t(s,a)/(1-\gamma)}, so that η1−γAt(s,a)=log⁡πt+1(a∣s)πt(a∣s)+log⁡Zt(s)\frac{\eta}{1-\gamma}A_t(s,a)=\log\frac{\pi_{t+1}(a\mid s)}{\pi_t(a\mid s)}+\log Z_t(s). By Jensen's inequality and EπtAt=0\E_{\pi_t}A_t=0:

  2. V(t+1)(μ)−V(t)(μ)=1η Es∼dμπt+1[DKL(πt+1 ∥ πt)(s)+log⁡Zt(s)]≥1−γη Es∼μlog⁡Zt(s)≥0V^{(t+1)}(\mu)-V^{(t)}(\mu)=\frac1\eta\,\E_{s\sim d^{\pi_{t+1}}_\mu}\Big[\KL\big(\pi_{t+1}\,\|\,\pi_t\big)(s)+\log Z_t(s)\Big]\ge\frac{1-\gamma}{\eta}\,\E_{s\sim\mu}\log Z_t(s)\ge0

    Monotone improvement. Apply the lemma with π′=πt+1\pi'=\pi_{t+1} and substitute; drop the nonnegative KL term, and use dμπt+1≥(1−γ)μd^{\pi_{t+1}}_\mu\ge(1-\gamma)\mu:

  3. V∗(ρ)−V(t)(ρ)=1η Es∼d∗[DKL(π∗ ∥ πt)(s)−DKL(π∗ ∥ πt+1)(s)+log⁡Zt(s)]V^*(\rho)-V^{(t)}(\rho)=\frac1\eta\,\E_{s\sim d^*}\Big[\KL\big(\pi^*\,\|\,\pi_t\big)(s)-\KL\big(\pi^*\,\|\,\pi_{t+1}\big)(s)+\log Z_t(s)\Big]

    Progress against the optimum. Apply the lemma with π′=π∗\pi'=\pi^* and d∗=dρπ∗d^*=d^{\pi^*}_\rho. Under π∗\pi^* the average log-ratio is a difference of two KL divergences:

  4. ∑t<T(V∗(ρ)−V(t)(ρ))≤log⁡∣A∣η+V(T)(d∗)−V(0)(d∗)1−γ≤log⁡∣A∣η+1(1−γ)2\sum_{t<T}\big(V^*(\rho)-V^{(t)}(\rho)\big)\le\frac{\log\abs{\mathcal A}}{\eta}+\frac{V^{(T)}(d^*)-V^{(0)}(d^*)}{1-\gamma}\le\frac{\log\abs{\mathcal A}}{\eta}+\frac1{(1-\gamma)^2}

    Bound the last term with the previous step, taking μ=d∗\mu=d^*; sum over t<Tt<T; let the KL terms telescope; and use DKL(π∗ ∥ uniform)≤log⁡∣A∣\KL(\pi^*\,\|\,\text{uniform})\le\log\abs{\mathcal A} and 0≤V≤1/(1−γ)0\le V\le1/(1-\gamma):

  5. By monotone improvement the last iterate is the best, so its gap is at most the average gap, which is the theorem. Choosing η=(1−γ)2log⁡∣A∣\eta=(1-\gamma)^2\log\abs{\mathcal A} gives 2/((1−γ)2T)2/\big((1-\gamma)^2T\big).

Method (exact gradients, tabular)GuaranteeSource
Projected gradient ascent, direct parameterizationε\varepsilon-optimal after O(∣S∣∣A∣ ∥dρπ∗/μ∥∞2/((1−γ)6ε2))\order{\abs S\abs A\,\norm{d^{\pi^*}_\rho/\mu}_\infty^2/((1-\gamma)^6\varepsilon^2)} iterationsAgarwal et al. (2021)
Softmax policy gradientconverges; rate O(1/t)\order{1/t} with problem-dependent constants, which can be exponential in ∣S∣\abs S and 1/(1−γ)1/(1-\gamma)Mei et al. (2020); Li et al. (2021)
Entropy-regularized softmax policy gradientlinear convergence to the regularized optimumMei et al. (2020)
Natural policy gradient (softmax)log⁡∣A∣ηT+1(1−γ)2T\frac{\log\abs{\mathcal A}}{\eta T}+\frac{1}{(1-\gamma)^2T}, independent of ∣S∣\abs SAgarwal et al. (2021)
Entropy-regularized NPG; policy mirror descent with growing stepslinear convergence, dimension-freeCen et al. (2022); Lan (2023); Xiao (2022)
For linear-quadratic control, where states and actions are continuous, policy gradient also converges globally despite non-convexity (Fazel, Ge, Kakade & Mesbahi, 2018).

Key takeaways

  • ∇J=11−γEdρπ,π[Qπ∇log⁡πθ]\nabla J=\frac1{1-\gamma}\E_{d^\pi_\rho,\pi}[Q^\pi\nabla\log\pi_\theta] follows from a Bellman equation for ∇Vπ\nabla V^\pi; states are weighted by the discounted occupancy measure, whose own derivative never appears. Dropping γt\gamma^t in practice gives a direction that is not a gradient.
  • The variance-optimal baseline weights QQ by the squared score norm; the value function is near-optimal in many problems but can be far worse, even worse than no baseline.
  • Compatible critics, fw=w⊤∇log⁡πθf_w=w^\top\nabla\log\pi_\theta fit by least squares, give exact gradients, and their weights are the natural gradient (1−γ)F−1∇J(1-\gamma)F^{-1}\nabla J.
  • The natural gradient is steepest ascent in KL geometry; for tabular softmax it is the multiplicative-weights update πt+1∝πtexp⁡(ηAπt/(1−γ))\pi_{t+1}\propto\pi_t\exp\big(\eta A^{\pi_t}/(1-\gamma)\big).
  • The performance-difference lemma yields gradient domination and global convergence despite non-concavity; NPG converges at rate O(1/T)\order{1/T} with no dependence on ∣S∣\abs S, while vanilla softmax policy gradient can be exponentially slow.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

Which quantity does not appear in the policy-gradient theorem?

  1. The action values Qπ(s,a)Q^\pi(s,a)
  2. The score ∇θlog⁡πθ(a∣s)\nabla_\theta\log\pi_\theta(a\mid s)
  3. The gradient of the state distribution, ∇θdρπ(s)\nabla_\theta d^\pi_\rho(s)
  4. The discounted occupancy measure dρπ(s)d^\pi_\rho(s)
Show the answer

C. The gradient of the state distribution, ∇θdρπ(s)\nabla_\theta d^\pi_\rho(s)

Unrolling the gradient's Bellman recursion absorbs every effect of θ\theta on future states into the weighting by dρπd^\pi_\rho. No derivative of the dynamics or of the state distribution is needed, which is what makes sample-based estimation possible.

Question 2

A compatible critic fw(s,a)=w⊤∇θlog⁡πθ(a∣s)f_w(s,a)=w^\top\nabla_\theta\log\pi_\theta(a\mid s) has which property for every ww?

  1. fw(s,a)≥0f_w(s,a)\ge0
  2. Ea∼πfw(s,a)=0\E_{a\sim\pi}f_w(s,a)=0, so it can only represent advantages
  3. fw=Qπf_w=Q^\pi exactly
  4. fwf_w is the same for all actions in a state
Show the answer

B. Ea∼πfw(s,a)=0\E_{a\sim\pi}f_w(s,a)=0, so it can only represent advantages

Ea∼π∇log⁡πθ(a∣s)=0\E_{a\sim\pi}\nabla\log\pi_\theta(a\mid s)=0, so Eafw(s,a)=w⊤0=0\E_af_w(s,a)=w^\top0=0. A compatible critic is centered in every state: it approximates AπA^\pi, and fitting it to QπQ^\pi or to AπA^\pi gives the same weights.

Question 3

The natural gradient F(θ)−1∇JF(\theta)^{-1}\nabla J is the direction of steepest ascent when the length of a step is measured by:

  1. The Euclidean distance between parameter vectors
  2. The change in expected return
  3. The number of actions whose probability changes
  4. The expected KL divergence between the old and new policies (to second order)
Show the answer

D. The expected KL divergence between the old and new policies (to second order)

EsDKL(πθ(⋅∣s) ∥ πθ+Δ(⋅∣s))=12Δ⊤FΔ+O(∥Δ∥3)\E_s\KL\big(\pi_\theta(\cdot\mid s)\,\|\,\pi_{\theta+\Delta}(\cdot\mid s)\big)=\tfrac12\Delta^\top F\Delta+\order{\norm\Delta^3}, and maximizing ∇J⊤Δ\nabla J^\top\Delta subject to 12Δ⊤FΔ≤ε\tfrac12\Delta^\top F\Delta\le\varepsilon gives Δ∝F−1∇J\Delta\propto F^{-1}\nabla J. Measuring steps in policy space makes the direction independent, to first order, of how the policy is parameterized.

Question 4

For tabular softmax policies, how does the natural policy gradient bound V∗(ρ)−V(T)(ρ)≤log⁡∣A∣ηT+1(1−γ)2TV^*(\rho)-V^{(T)}(\rho)\le\frac{\log\abs{\mathcal A}}{\eta T}+\frac{1}{(1-\gamma)^2T} depend on the number of states?

  1. Not at all
  2. Linearly
  3. Exponentially
  4. Through the distribution-mismatch coefficient
Show the answer

A. Not at all

The mirror-descent proof works state by state and combines the states with the optimal policy's own occupancy measure, so neither ∣S∣\abs S nor a mismatch coefficient appears. Vanilla softmax policy gradient, by contrast, can need time exponential in ∣S∣\abs S.

Further reading

  • R. S. Sutton, D. McAllester, S. Singh & Y. Mansour, "Policy gradient methods for reinforcement learning with function approximation," NeurIPS 12 (2000).
  • S. M. Kakade, "A natural policy gradient," NeurIPS 14 (2002).
  • J. Mei, C. Xiao, C. Szepesvári & D. Schuurmans, "On the global convergence rates of softmax policy gradient methods," ICML (2020).
  • A. Agarwal, S. M. Kakade, J. D. Lee & G. Mahajan, "On the theory of policy gradient methods: Optimality, approximation, and distribution shift," Journal of Machine Learning Research 22(98), 1–76 (2021).
  • A. Agarwal, N. Jiang, S. M. Kakade & W. Sun, Reinforcement Learning: Theory and Algorithms (monograph draft, 2019–2022).