REINFORCE and baselines
REINFORCE is a Monte‑Carlo policy‑gradient algorithm that estimates the gradient of expected return by sampling trajectories and can use a baseline (typically a state-value) to reduce variance without biasing the gradient.
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
REINFORCE gives a direct method to improve a parametrised stochastic policy using sampled episodes. Engineers use it when they need a model-free method to learn policies for episodic tasks (simulations, game episodes, robot tasks) and when value‑function bootstrapping (TD methods) is not desired. Adding a baseline (usually a state‑value estimate) cuts variance of the gradient estimate and makes learning more stable.
Key ideas
Start with the fundamentals and definitions.
- Markov decision process (MDP): a tuple (S, A, P, R, γ) where S = states, A = actions, P(s'|s,a) = transition probability, R(s,a) = expected immediate reward, and 0 ≤ γ ≤ 1 is discount. (We assume episodic MDPs for REINFORCE.)
- Policy (πθ): a differentiable parametric mapping from states to a probability distribution over actions, πθ(a|s). Parameters are θ ∈ R^n. We require πθ(a|s) differentiable w.r.t θ.
- Trajectory (episode) τ: sequence s0, a0, r1, s1, a1, r2, ... , sT (terminal). Return for time t is G_t = Σ_{k=t+1}^{T} γ^{k-t-1} r_k.
- Objective J(θ): expected return from start distribution (could be average over states), J(θ) = E_{τ~πθ}[sum of discounted rewards]. We want to find θ that maximises J(θ).
- Policy gradient: the gradient ∇θ J(θ) tells how to change θ to increase expected return. The policy gradient theorem gives a way to express this gradient in terms of expectations over trajectories.
Policy gradient (derivation often asked in exams) — step by step (assumptions: episodic MDP, differentiable πθ, interchange of gradient and expectation allowed):
- Write objective as expectation over trajectories: J(θ) = E_{τ~pθ(τ)}[R(τ)] where R(τ) = Σ_{t=0}^{T-1} γ^{t} r_{t+1} and pθ(τ) depends on πθ.
- Differentiate: ∇θ J(θ) = ∇θ ∫ pθ(τ) R(τ) dτ = ∫ ∇θ pθ(τ) R(τ) dτ.
- Use log-derivative trick: ∇θ pθ(τ) = pθ(τ) ∇θ log pθ(τ). So ∇θ J(θ) = ∫ pθ(τ) ∇θ log pθ(τ) R(τ) dτ = E_{τ~pθ}[R(τ) ∇θ log pθ(τ)].
- Expand log pθ(τ). For an MDP with fixed initial state distribution and dynamics independent of θ: log pθ(τ) = Σ_{t=0}^{T-1} log πθ(a_t|s_t) + const. Therefore ∇θ log pθ(τ) = Σ_{t=0}^{T-1} ∇θ log πθ(a_t|s_t).
- Plug back and rearrange by time indices: ∇θ J(θ) = E_{τ}[Σ_{t=0}^{T-1} ∇θ log πθ(a_t|s_t) G_t]. This is the classic Monte‑Carlo policy gradient expression. Using G_t (reward‑to‑go) is valid because rewards before time t do not depend on a_t and their contribution has zero expectation when multiplied by ∇θ log πθ(a_t|s_t) (causality).
Define terms used in variance reduction:
- Baseline b(s): any function of state (not action) subtracted from the return in the gradient estimator: ∇θ J(θ) = E[Σ_t ∇θ log πθ(a_t|s_t) (G_t - b(s_t))]. The baseline b may also depend on time t or parameters θ, but as long as it does not depend on the sampled action, subtracting it remains unbiased (we do not differentiate through b in the policy update).
- Advantage Aπ(s,a): Aπ(s,a) = Qπ(s,a) - Vπ(s). If b(s) = Vπ(s) then (G_t - b(s_t)) is an estimate of the advantage.
Key theorem (no bias from baseline): For any function b(s) that does not depend on the action, E_{a~π}[∇θ log πθ(a|s) b(s)] = b(s) ∇θ Σ_a πθ(a|s) = b(s) ∇θ 1 = 0. Therefore subtracting b(s) does not change expected gradient — it only affects variance. Show this step in exams.
Optimal baseline (exam derivations): two useful cases commonly asked:
- If a single scalar baseline b (same for all states) is used, the variance-minimising scalar b* satisfies b* = E[G ||g||^2] / E[||g||^2], where g = ∇θ log πθ(a|s) evaluated at sampled (s,a) and ||·|| is Euclidean norm. (This follows by minimising E[(G - b)^2 ||g||^2] over b.)
- If baselines may depend on s, the variance is minimised (pointwise) by b*(s) = E[G | s] — that is, the state-value Vπ(s). This is the usual justification for using a learned value function as baseline.
Algorithm commonly required in exams (REINFORCE with baseline): sample N episodes under πθ, for each episode compute G_t, and update θ ← θ + α Σ_t ∇θ log πθ(a_t|s_t) (G_t - b(s_t)). If b(s) = Vw(s) is approximated by parameters w, update w (e.g. by least squares or Monte‑Carlo regression) to reduce (Vw(s_t) - G_t)^2. When computing the policy-gradient update, treat Vw(s_t) as a baseline constant with respect to θ (do not differentiate the actor through the baseline).
What to draw in diagrams (exam practice): draw an episodic timeline: boxes for s0,a0 -> r1 -> s1,a1 -> r2 -> ... -> terminal. Label G_t as sum of future discounted rewards. If showing softmax policy, draw parameter vector θ and softmax mapping to π(a|s).
Formulas
J(θ) = E_{τ~pθ(τ)}[R(τ)] — J(θ) is expected total (discounted) reward; unit: unit of reward (unitless if rewards are unitless). Applies for episodic objective.
∇θ J(θ) = E_{τ~pθ}[R(τ) ∇θ log pθ(τ)] — log-derivative identity; pθ(τ) is trajectory probability; unit: same as J per unit θ.
∇θ J(θ) = E_{τ}[ Σ_{t=0}^{T-1} ∇θ log πθ(a_t|s_t) · G_t ] — main Monte‑Carlo policy gradient; G_t = Σ_{k=t+1}^{T} γ^{k-t-1} r_k.
∇θ J(θ) = E[ Σ_t ∇θ log πθ(a_t|s_t) (G_t - b(s_t)) ] — with baseline b(s); b must not depend on a.
b^*(s) = E[G_t | s_t = s] — variance-minimising state‑dependent baseline (state‑value), unit: same as reward.
b^* (scalar) = E[G ||g||^2] / E[||g||^2], g = ∇θ log πθ(a|s) — optimal scalar baseline when same b used everywhere.
Policy parameter update (stochastic gradient ascent): θ <- θ + α · ∇̂θ J(θ) where ∇̂θ J(θ) is the Monte‑Carlo estimate Σ_t ∇θ log πθ(a_t|s_t) (G_t - b(s_t)); α is step size (unit: 1/θ units).
Notes on units: RL uses unitless parameters/gradients and rewards; indicate reward units if the environment provides them.
Worked examples
Example 1 — one‑state bandit (model answer style). Problem: Single state s with two actions A and B. Immediate rewards: r(A)=1, r(B)=0. Policy is softmax with parameters θ = [θ_A, θ_B], πθ(a) = e^{θ_a} / (e^{θ_A}+e^{θ_B}). For θ = [0,0] compute ∇J(θ). Steps:
- π(A)=π(B)=0.5.
- For softmax, ∂ log π(a)/∂θ_i = 1_{i=a} - π(i). So gradient vector for action A: g_A = [1 - 0.5, 0 - 0.5] = [0.5, -0.5]. For action B: g_B = [0 - 0.5, 1 - 0.5] = [-0.5, 0.5].
- Return G equals immediate reward (episode ends): G(A)=1, G(B)=0.
- Expected gradient ∇J = π(A)G(A)g_A + π(B)G(B)g_B = 0.51[0.5,-0.5] + 0.50[-0.5,0.5] = [0.25, -0.25]. Answer: the gradient is [0.25, -0.25].
Example 2 — show baseline reduces variance in same bandit. Use same setup. Consider gradient estimator from a single sample: Z = G · g (no baseline) or Z_b = (G - b) · g. For θ=[0,0], g_A=[0.5,-0.5], g_B=[-0.5,0.5]. Choose b = E[G] = 0.5. If we sample action A (prob 0.5), Z = 1 * g_A = [0.5,-0.5]; Z_b = (1-0.5)g_A = 0.5[0.5,-0.5] = [0.25,-0.25]. If we sample action B, Z = 0 * g_B = [0,0]; Z_b = (0-0.5)g_B = (-0.5)[-0.5,0.5] = [0.25,-0.25]. Thus with b=0.5 both possible single-sample estimates equal the expected gradient [0.25,-0.25] — variance zero. Conclusion: baseline removed variance in this deterministic-return example.
Example 3 — short derivation exam question (model answer). "Derive that subtracting any state‑dependent baseline does not bias the policy gradient estimate." Model answer (short): E_{π}[∇θ log πθ(a|s) b(s)] = E_{s}[b(s) Σ_a πθ(a|s) ∇θ log πθ(a|s)] = E_s[b(s) ∇θ Σ_a πθ(a|s)] = E_s[b(s) ∇θ 1] = 0. Hence baseline causes no bias.
Common mistakes
- Confusing Qπ(s,a), Vπ(s) and Aπ(s,a) and using them interchangeably without defining which is used as baseline.
- Dropping the ∇ log π and writing gradient as ∇π directly — exams want the log-derivative step.
- Forgetting E_{π}[∇ log π] = 0 proof when justifying baseline unbiasedness.
- Using high learning rate α and expecting convergence; REINFORCE needs small α and many episodes.
For your exam
Types of questions:
- Definitions: define policy, return, baseline, advantage.
- Short notes: explain REINFORCE algorithm and why baseline helps.
- Derivations: derive the policy gradient using log‑derivative trick; show baseline does not bias gradient; derive optimal scalar baseline formula.
- Numericals: compute gradient for small MDP or bandit with softmax policy (one or two steps). Show every step and carry units (rewards) if present.
- Pseudocode/program: give REINFORCE loop and update; may be asked to add baseline estimated by Monte‑Carlo regression.
What a full‑marks answer includes:
- Clear statement of assumptions (episodic, differentiable πθ, Markov property).
- Precise derivation of ∇J(θ) using log-derivative trick with intermediate steps.
- Proof that baseline does not change expected gradient (use E[∇ log π]=0).
- If asked for variance reduction, include derivation of b*(s) = E[G|s] or scalar b* formula, and a short explanation.
- If numeric, compute π, ∇ log π, G_t and then ∇J numerically with steps and final vector.
Quick check
- State the Monte‑Carlo policy gradient formula for an episodic task (one line).
- Why does subtracting a state‑dependent baseline not change the expected gradient? Give the short proof idea.
- For a one‑state, two‑action bandit with rewards r(A)=1, r(B)=0 and policy softmax with θ=[0,0], what is ∇J? Answers: 1) ∇θ J(θ) = E[Σ_t ∇θ log πθ(a_t|s_t) G_t]. 2) Because E_a[∇θ log πθ(a|s)] = ∇θ Σ_a πθ(a|s) = 0, so E[∇ log π · b(s)] = 0. 3) [0.25, -0.25].
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?