Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Policy gradient theorem

The policy gradient theorem gives an exact expression for the gradient of expected return with respect to policy parameters and is the basis for REINFORCE and actor–critic methods.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Policy gradients let an agent directly optimise a parameterised policy by gradient ascent on expected return. Engineers use them when actions are continuous, when value-based methods (like Q-learning or DQN) are hard to apply, or when deploying actor–critic architectures in control systems, robotics or continuous-control tasks.

Key ideas

  • Return for an episode (trajectory) τ = s0,a0,r0,s1,a1,r1,...: R(τ) = Σ_{t=0}^{T} γ^t r_t. For continuing tasks use infinite horizon with γ<1.

  • Objective (episodic): J(θ) = E_{τ∼p_θ}[R(τ)], where p_θ(τ) is trajectory probability under policy π_θ and environment dynamics. We want ∇_θ J(θ).

  • Likelihood-ratio (score-function) trick: ∇_θ p_θ(τ) = p_θ(τ) ∇_θ log p_θ(τ). This moves derivative inside expectation.

  • REINFORCE (episodic policy gradient) derivation (standard exam derivation):

    1. Write J(θ) = ∫ p_θ(τ) R(τ) dτ.
    2. Differentiate: ∇_θ J = ∫ ∇_θ p_θ(τ) R(τ) dτ.
    3. Apply likelihood-ratio: = ∫ p_θ(τ) ∇θ log p_θ(τ) R(τ) dτ = E{τ∼p_θ}[∇_θ log p_θ(τ) R(τ)].
    4. Because p_θ(τ)=p(s0) Π_t π_θ(a_t|s_t) P(s_{t+1}|s_t,a_t), only π_θ terms depend on θ, so log p_θ(τ)=Σ_t log π_θ(a_t|s_t).
    5. Therefore ∇_θ J = E[ (Σ_t ∇_θ log π_θ(a_t|s_t)) R(τ) ].
    6. Rearrange to use reward-to-go (to reduce variance) G_t = Σ_{k=t}^T γ^{k-t} r_k: ∇θ J = E[ Σ{t=0}^T ∇_θ log π_θ(a_t|s_t) G_t ].
  • Policy gradient theorem (state-value form, continuing or with start distribution μ): For objective J(θ)=Σ_s μ(s) V^{π_θ}(s), under standard MDP assumptions, ∇_θ J(θ) = Σ_s d^{π}(s) Σ_a ∇_θ π_θ(a|s) Q^{π}(s,a), where d^{π}(s) is the (discounted, not necessarily normalized) state-visitation distribution under π (depends on μ and γ). This statement avoids explicit trajectory probabilities and supports actor-critic derivations. For continuous action spaces, replace sums over a with integrals and interpret π as a density.

    Typical exam derivation steps: start from ∇V^{π}(s) = Σ_a ∇π(a|s) Q^{π}(s,a) + Σ_a π(a|s) γ Σ_{s'} P(s'|s,a) ∇V^{π}(s'), expand recursively, multiply by μ(s) and sum over s to collect geometric series producing d^{π}(s).

  • Baseline b(s): subtracting any function b(s) from the return (or Q) inside the expectation does not change the expected value of the gradient (unbiased) but can reduce variance. Proof: E[∇ log π(a|s) b(s)] = b(s) ∇ Σ_a π(a|s) = b(s) ∇1 = 0.

  • Advantage A^{π}(s,a) = Q^{π}(s,a) − V^{π}(s). Actor–critic uses a critic to estimate V or Q; the actor updates parameters using estimated advantage to reduce variance.

  • Assumptions used in derivations: policy differentiable, interchange of gradient and integral (regularity), full support on needed actions, finite expected returns (integrability), and (for continuing case) existence of stationary visitation distribution d^{π} (or use discounted visitation definition).

  • Practical notes: REINFORCE uses full-episode returns and high variance. Actor–critic replaces G_t by learned Q or advantage to reduce variance and allow bootstrapping.

Formulas

J(θ) = E_{τ∼p_θ}[R(τ)] — J(θ) is expected (discounted) return; in reward units and valid for episodic or properly defined continuing tasks.

∇_θ J(θ) = E_{τ∼p_θ}[∇_θ log p_θ(τ) R(τ)] — holds when p_θ and log p_θ differentiable and integrability holds.

log p_θ(τ) = Σ_{t=0}^T log π_θ(a_t|s_t) + const — constant depends on environment dynamics, which are θ-independent.

∇_θ J(θ) = E[Σ_{t=0}^T ∇_θ log π_θ(a_t|s_t) G_t] — G_t is reward-to-go from time t. (REINFORCE)

∇_θ J(θ) = Σ_s d^{π}(s) Σ_a ∇_θ π_θ(a|s) Q^{π}(s,a) — policy gradient theorem (state-weighted form). d^{π}(s) is a discounted visitation measure; not necessarily normalized and dimensionless.

E[∇_θ log π_θ(a|s) b(s)] = 0 — baseline property; b(s) any function of state only.

Symbols: θ (policy parameters, dimensionless vector), π_θ(a|s) (probability mass or density; units depend on action space), Q^{π}(s,a), V^{π}(s), G_t (returns, reward units), γ (discount, dimensionless), d^{π}(s) (discounted state-visitation measure over states; not necessarily normalized, dimensionless).

Worked examples

  1. Derive REINFORCE gradient (model answer).

    • Given: episodic MDP with start distribution p(s0), policy π_θ, trajectory τ with return R(τ).
    • Show: ∇θ J = E{τ}[Σ_t ∇_θ log π_θ(a_t|s_t) G_t].
    • Answer steps (short): J = ∫ p_θ(τ) R(τ) dτ; ∇J = ∫ ∇p_θ(τ) R dτ = ∫ p_θ(τ) ∇ log p_θ(τ) R dτ. Since log p_θ(τ)=Σ_t log π_θ(a_t|s_t)+const, substitute and separate sums to get the stated formula. To reduce variance use G_t instead of full R because E[∇θ log π_θ(a_t|s_t) Σ{k<t} r_k] = 0 (past-reward independence/causality); thus the reward-to-go is valid and lowers variance.
  2. Numerical one-step bandit (short, complete numeric): single-state MDP (t=0 only). Two actions a1,a2 with rewards R(a1)=5, R(a2)=1 deterministically. Softmax policy with parameters θ1=0.2, θ2=-0.1: π(a1)=e^{θ1}/(e^{θ1}+e^{θ2}).

    • Compute probabilities: e^{0.2}=1.22140, e^{-0.1}=0.90484, sum=2.12624, π(a1)=1.22140/2.12624=0.5743, π(a2)=0.4257.
    • If observed sample chooses a1 and reward r=5, REINFORCE one-sample gradient estimate: ∇{θ1} log π(a1)·r, ∇{θ2} log π(a1)·r.
      • ∇_{θ1} log π(a1) = 1 - π(a1) = 0.4257.
      • ∇_{θ2} log π(a1) = -π(a2) = -0.4257. (For softmax with two params: gradient vector is one-hot minus probabilities.)
      • Gradient estimate = r·[0.4257, -0.4257] = [2.1285, -2.1285].
    • Interpretation: increase θ1, decrease θ2; step-size α gives parameter update θ ← θ + α · gradient.
    • Final answer: gradient estimate [2.1285, −2.1285] (reward units).
  3. Baseline proof (model answer): show subtracting b(s) does not bias gradient. Start with ∇J = E[Σ_t ∇ log π(a_t|s_t) (G_t − b(s_t))]. The extra term E[Σ_t ∇ log π(a_t|s_t) b(s_t)] = Σ_t E[b(s_t) ∇ log π(a_t|s_t)] = Σ_t E[b(s_t) E_{a_t∼π}[∇ log π(a_t|s_t)]] = Σ_t E[b(s_t) ∇ Σ_a π(a|s_t)] = 0.

Common mistakes

  • Thinking any baseline must be zero or that adding baseline changes bias; in fact any state-only baseline is unbiased.
  • Confusing Q^{π} and advantage A^{π} in formulas; policy gradient theorem uses Q but actor–critic often uses advantage.
  • Dropping the requirement that π_θ be differentiable and have support where needed; gradients undefined otherwise.

For your exam

Kinds of questions: short definitions (policy gradient theorem, baseline, advantage), derivations (derive REINFORCE gradient; derive policy gradient theorem from ∇V formula), numerical (compute sample gradient for a softmax policy in a bandit), and short notes (variance reduction techniques, actor–critic summary). A full-marks derivation must state assumptions (differentiability, integrability), show the likelihood-ratio trick step, expand log p_θ(τ) into sum over t, and justify replacing full return by reward-to-go. For the policy-gradient-theorem derivation give the recursive expansion of ∇V, multiply by start distribution and sum to show state-visitation weighting.

Quick check

  1. State the likelihood-ratio trick in one line.
  2. Why can you subtract a baseline b(s) from the return without biasing the policy gradient?
  3. In a one-step bandit with softmax parameters θ1,θ2 and observed action a1 with reward r, what is ∇_{θ1} log π(a1)?

Answers: 1) ∇_θ p_θ(x) = p_θ(x) ∇θ log p_θ(x). 2) Because E[∇ log π(a|s) b(s)]=b(s) ∇ Σ_a π(a|s)=0, so subtraction adds zero expectation. 3) ∇{θ1} log π(a1) = 1 − π(a1).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?