Simple Algorithm
Reinforcement Learning · Fixed Point · 28 min

Baselines & Variance Reduction

Optimal baselines, control variates and the mirage of action-dependent baselines.

A baseline never changes the expected policy gradient, so it can be chosen purely to cut variance. Which one cuts it most? Not the value function: in a two-action example the value baseline does 64 times worse than no baseline at all. This lesson takes the estimator's variance apart, derives the optimal baseline, and follows action-dependent baselines from their promising results to the discovery that the gains were a mirage.

What you’ll learn

  • Decompose the variance of the policy-gradient estimator into state, action and trajectory terms, and say which of them a baseline can reduce.
  • Derive the variance-optimal baseline and the exact excess variance of any other baseline.
  • Explain why action-dependent baselines need a correction term, how Q-Prop and Stein control variates supply it, and what Tucker et al. found when they re-examined them.
  • Use reward-to-go, discounting and common random numbers to reduce variance, and state what each costs.

Where the variance comes from

Write one sample of the gradient at a visited state as g^=ψ(S,A) (Q^−b)\hat{\mathbf g}=\boldsymbol\psi(S,A)\,\big(\hat Q-b\big), where ψ(s,a)=∇log⁡π(a∣s)\boldsymbol\psi(s,a)=\nabla\log\pi(a\mid s) is the score, Q^\hat Q an unbiased but noisy estimate of Qπ(S,A)Q^\pi(S,A) such as the return, and b=b(S)b=b(S) a baseline. Three sources of randomness enter: which state is visited, which action is drawn there, and how the rest of the trajectory unfolds. Conditioning on SS and then on AA, the law of total variance splits the total variance Σ\Sigma (the trace of the covariance) along exactly these lines, as in Tucker et al. (2018):

Σ=Var⁡S(EA∣S[ψ Aπ(S,A)])⏟Σs: which states+ESVar⁡A∣S(ψ (Qπ(S,A)−b(S)))⏟Σa: which action+ES,AVar⁡rest∣S,A(ψ Q^)⏟Στ: rest of the trajectory\Sigma=\underbrace{\Var_S\Big(\E_{A\mid S}\big[\boldsymbol\psi\,A^\pi(S,A)\big]\Big)}_{\Sigma_s:\ \text{which states}}+\underbrace{\E_S\Var_{A\mid S}\Big(\boldsymbol\psi\,\big(Q^\pi(S,A)-b(S)\big)\Big)}_{\Sigma_a:\ \text{which action}}+\underbrace{\E_{S,A}\Var_{\text{rest}\mid S,A}\big(\boldsymbol\psi\,\hat Q\big)}_{\Sigma_\tau:\ \text{rest of the trajectory}}
For vectors, Var⁡\Var means the trace of the covariance. The middle term uses E[Q^∣S,A]=Qπ(S,A)\E[\hat Q\mid S,A]=Q^\pi(S,A), and the first uses EA∣S[ψ]=0\E_{A\mid S}[\boldsymbol\psi]=\mathbf 0, which removes both bb and VπV^\pi.

Only Σa\Sigma_a depends on the baseline. A state baseline shrinks it but cannot remove it; an action-dependent one could remove it entirely; and no baseline touches Στ\Sigma_\tau, the noise of future rewards, or Σs\Sigma_s, the noise of which states happen to be visited. Other tools attack those.

ToolWhat it reducesCost
State baseline b(s)≈Vπ(s)b(s)\approx V^\pi(s)part of Σa\Sigma_anone in bias; a value function to fit
Action-dependent control variate ϕ(s,a)\phi(s,a)all of Σa\Sigma_a, in principlea correction term that is easy to get wrong
Reward-to-gonoise from rewards earned before the actionnone
Critic, nn-step and λ-returns, GAEΣτ\Sigma_\taubias when the critic is wrong
Discount γ<1\gamma<1 as a knobΣτ\Sigma_\tau, by shortening the effective horizonbias with respect to the undiscounted objective
Bigger batches; common random numberseverything; noise in comparisons between policiescompute; control over the simulator's randomness
Check your understanding

On long-horizon locomotion tasks with discounted Monte Carlo returns, Tucker et al. found one term dominating the variance. Which, and what follows for action-dependent baselines?

  1. Σa\Sigma_a, so action-dependent baselines should help a lot
  2. Στ\Sigma_\tau, so even a perfect action-dependent baseline, which can only remove Σa\Sigma_a, would barely help
  3. Σs\Sigma_s, so only running more environments in parallel can help
  4. All three terms were about equal
Show the answer

B. Στ\Sigma_\tau, so even a perfect action-dependent baseline, which can only remove Σa\Sigma_a, would barely help

Over hundreds of steps the randomness of the rest of the trajectory swamps everything else, and baselines cannot touch it. With GAE's shorter effective horizon Στ\Sigma_\tau shrinks and an oracle action-dependent baseline could help, but the learned ones they tested did not reduce variance below a learned state baseline.

The optimal baseline

DerivationProof: the excess-variance identity
  1. E∥g^∥2=E[∥ψ∥2(Qπ)2]−2b E[∥ψ∥2Qπ]+b2 E[∥ψ∥2]\E\norm{\hat{\mathbf g}}^2=\E\big[\norm{\boldsymbol\psi}^2(Q^\pi)^2\big]-2b\,\E\big[\norm{\boldsymbol\psi}^2Q^\pi\big]+b^2\,\E\big[\norm{\boldsymbol\psi}^2\big]

    The mean E[g^]=E[ψ Qπ]\E[\hat{\mathbf g}]=\E[\boldsymbol\psi\,Q^\pi] does not depend on bb, because E[ψ]=0\E[\boldsymbol\psi]=\mathbf 0. Minimizing the variance therefore means minimizing the second moment, a quadratic in bb:

  2. tr⁡Var⁡b(g^)=tr⁡Var⁡b⋆(g^)+E[∥ψ∥2] (b−b⋆)2\operatorname{tr}\Var_b(\hat{\mathbf g})=\operatorname{tr}\Var_{b^\star}(\hat{\mathbf g})+\E\big[\norm{\boldsymbol\psi}^2\big]\,(b-b^\star)^2

    Complete the square around b⋆b^\star:

  3. So b⋆b^\star averages the action values with weights π(a∣s)∥ψ(s,a)∥2\pi(a\mid s)\norm{\boldsymbol\psi(s,a)}^2, where Vπ(s)V^\pi(s) uses π(a∣s)\pi(a\mid s) alone. Actions with large scores, typically the rare ones, count for more. ■\blacksquare

DerivationTwo actions: the value of the opposite policy
  1. b⋆=(1−p) Q(a1)+p Q(a2)versusVπ=p Q(a1)+(1−p) Q(a2)b^\star=(1-p)\,Q(a_1)+p\,Q(a_2)\qquad\text{versus}\qquad V^\pi=p\,Q(a_1)+(1-p)\,Q(a_2)

    Take two actions with π(a1)=p\pi(a_1)=p and a logistic parameterization, so ψ(a1)=1−p\psi(a_1)=1-p and ψ(a2)=−p\psi(a_2)=-p. The weights π(a)ψ(a)2\pi(a)\psi(a)^2 are p(1−p)2p(1-p)^2 and (1−p)p2(1-p)p^2, proportional to 1−p1-p and pp:

  2. ψ(a1)(Q(a1)−b⋆)=ψ(a2)(Q(a2)−b⋆)=p(1−p)(Q(a1)−Q(a2))\psi(a_1)\big(Q(a_1)-b^\star\big)=\psi(a_2)\big(Q(a_2)-b^\star\big)=p(1-p)\big(Q(a_1)-Q(a_2)\big)

    The optimal baseline is the value of the opposite policy, and with it both actions give the same estimate, the exact gradient: zero variance.

  3. Var⁡b=Vπ=0.9 (0.01)2+0.1 (0.81)2−0.092=0.0576=64×0.0009,Var⁡b⋆=0\Var_{b=V^\pi}=0.9\,(0.01)^2+0.1\,(0.81)^2-0.09^2=0.0576=64\times0.0009,\qquad\Var_{b^\star}=0

    Now let p=0.9p=0.9, Q(a1)=1Q(a_1)=1 and Q(a2)=0Q(a_2)=0, so Vπ=0.9V^\pi=0.9, b⋆=0.1b^\star=0.1 and the gradient is 0.090.09. Without a baseline the samples are 0.10.1 (probability 0.9) and 00 (probability 0.1), with variance 0.00090.0009. With b=Vπb=V^\pi they are 0.1×0.1=0.010.1\times0.1=0.01 and (−0.9)(−0.9)=0.81(-0.9)(-0.9)=0.81:

  4. Here the value baseline is 64 times worse than none: the rare action has a large score, and centering at VπV^\pi gives it a large advantage, −0.9-0.9, so its rare samples are huge. Shift both values up by 10 and the picture flips: no baseline now has variance 9.189.18, while both baselines are unaffected. VπV^\pi protects against offsets; b⋆b^\star also accounts for which samples the score amplifies.

Predict before you look

Same two actions with p=0.8p=0.8, Q(a1)=5Q(a_1)=5 and Q(a2)=2Q(a_2)=2. What is the variance-optimal baseline b⋆b^\star?

Show the answer

2.6

b⋆=(1−p) Q(a1)+p Q(a2)=0.2×5+0.8×2=2.6b^\star=(1-p)\,Q(a_1)+p\,Q(a_2)=0.2\times5+0.8\times2=2.6, far from Vπ=4.4V^\pi=4.4. With it every sample equals the exact gradient p(1−p)×(5−2)=0.48p(1-p)\times(5-2)=0.48.

The history of this result is a lesson in itself. Weaver and Tao (2001) showed that for the GPOMDP estimator, in the limit where its discount-induced bias vanishes, the variance-minimizing constant reward baseline is the long-run average reward. Greensmith, Bartlett and Baxter (2004) treated baselines and critics as additive control variates, computed optimal baselines, and showed that the popular choices, the average reward as a baseline and even the true value function as a critic, can be suboptimal. Practice still uses a learned Vπ(s)V^\pi(s): it is easy to fit by regression, unbiased whatever its errors, and usually close enough. The identity above prices the gap at each state: E∥ψ∥2 (Vπ(s)−b⋆(s))2\E\norm{\boldsymbol\psi}^2\,\big(V^\pi(s)-b^\star(s)\big)^2.

Action-dependent baselines, and a mirage

A baseline that also depends on the action, ϕ(s,a)\phi(s,a), could remove Σa\Sigma_a entirely: take ϕ=Qπ\phi=Q^\pi. But it is no longer free, because EA∼π[ψ(s,A) ϕ(s,A)]=∇θEa∼π[ϕ(s,a)]\E_{A\sim\pi}\big[\boldsymbol\psi(s,A)\,\phi(s,A)\big]=\nabla_{\boldsymbol\theta}\E_{a\sim\pi}[\phi(s,a)] is not zero in general. The remedy is to add that expectation back, computed analytically or with low variance:

g^=ψ(S,A) (Q^−ϕ(S,A))+∇θ Ea∼πθ(⋅∣S)[ϕ(S,a)]\hat{\mathbf g}=\boldsymbol\psi(S,A)\,\big(\hat Q-\phi(S,A)\big)+\nabla_{\boldsymbol\theta}\,\E_{a\sim\pi_{\boldsymbol\theta}(\cdot\mid S)}\big[\phi(S,a)\big]
Unbiased for any fixed ϕ\phi: the first term's mean is short by exactly E[ψ ϕ]=∇θEπ[ϕ]\E[\boldsymbol\psi\,\phi]=\nabla_{\boldsymbol\theta}\E_\pi[\phi], which the second term restores. This is a control variate whose mean is known.

Q-Prop (Gu, Lillicrap, Ghahramani, Turner & Levine, 2017) uses a first-order Taylor expansion of an off-policy critic QwQ_{\mathbf w} around the policy's mean action. For a Gaussian policy with mean mθ(s)m_{\boldsymbol\theta}(s), the correction term is then a deterministic-policy-gradient term, ∇θmθ(s) ∇aQw(s,a)∣a=mθ(s)\nabla_{\boldsymbol\theta}m_{\boldsymbol\theta}(s)\,\nabla_aQ_{\mathbf w}(s,a)\big|_{a=m_{\boldsymbol\theta}(s)}, so the method blends a likelihood-ratio gradient with a critic's pathwise gradient. Stein control variates (Liu et al., 2018) allow general nonlinear ϕ\phi and compute the correction with the reparameterization trick, which they connect to Stein's identity. Wu et al. (2018) proposed action-dependent baselines that factorize over the dimensions of the action. All reported large gains in sample efficiency on MuJoCo tasks.

Tucker, Bhupatiraju, Gu, Turner, Ghahramani and Levine (2018) then measured the three variance terms on the same benchmarks. With discounted Monte Carlo returns Στ\Sigma_\tau dominated, so even an optimal action-dependent baseline could barely help. With GAE an oracle could help, but the learned state–action baselines reduced variance no more than learned state baselines. Reviewing the released code, they traced the reported gains to implementation choices that departed from the published methods:

  • Q-Prop's code standardized the learning signal (subtracting its batch mean and dividing by its batch standard deviation) but left the correction term unnormalized, an adaptive and biased trade-off. Made unbiased, it did not outperform TRPO.
  • In the Stein control variate experiments, the value function behind the GAE advantages was poorly fit, so even a plain state-dependent baseline would have cut the variance about as much as the action-dependent one: that, not action dependence, explains the very large reported reductions. The baseline was also fitted on the same batch used for the gradient, which biases the estimate. Unbiased state and state–action baselines then performed about the same.
  • The code of a related method, LAX (Grathwohl et al., 2018), measured the variance of an estimator that neither compared method used, and a bug gave LAX a different advantage estimator; once corrected, it showed no improvement on the task examined.
Check your understanding

An implementation subtracts a learned ϕ(S,A)\phi(S,A) from the return but omits the term ∇θEa∼π[ϕ(S,a)]\nabla_{\boldsymbol\theta}\E_{a\sim\pi}[\phi(S,a)]. What is the effect?

  1. None, since baselines never bias the gradient
  2. Lower variance and no bias, as long as ϕ\phi fits QπQ^\pi well
  3. A bias of −∇θEa∼π[ϕ(S,a)]-\nabla_{\boldsymbol\theta}\E_{a\sim\pi}[\phi(S,a)], which vanishes when ϕ\phi ignores the action but not in general
  4. The estimate becomes the natural gradient
Show the answer

C. A bias of −∇θEa∼π[ϕ(S,a)]-\nabla_{\boldsymbol\theta}\E_{a\sim\pi}[\phi(S,a)], which vanishes when ϕ\phi ignores the action but not in general

The first term has mean E[ψQ^]−E[ψϕ]\E[\boldsymbol\psi\hat Q]-\E[\boldsymbol\psi\phi], and E[ψϕ]=∇θEπ[ϕ]\E[\boldsymbol\psi\phi]=\nabla_{\boldsymbol\theta}\E_\pi[\phi]. For a state baseline this is zero; for an action-dependent one it is generally not. A well-fit ϕ≈Qπ\phi\approx Q^\pi makes it worse, not better: subtracting the whole signal leaves almost nothing.

More ways to cut variance

Reward-to-go is a baseline. Replacing G0G_0 by GtG_t subtracts the past rewards ∑k<tγkRk+1\sum_{k<t}\gamma^kR_{k+1}, which are fixed before AtA_t is drawn. It costs nothing and usually helps, since past rewards are pure noise for the decision at step tt.

The discount is a dial. With an undiscounted or average-reward objective, computing returns with γ<1\gamma<1 shortens the effective horizon to about 11−γ\frac{1}{1-\gamma} steps, which cuts Στ\Sigma_\tau at the price of bias. Baxter and Bartlett (2001) made this precise for their GPOMDP estimator of the average-reward gradient: its discount β\beta trades bias against variance, and the right setting depends on the mixing time of the controlled chain, since the bias is small only once 11−β\frac{1}{1-\beta} is long compared with it. GAE's λ\lambda is a second, finer dial of the same kind (Generalized Advantage Estimation).

Common random numbers. When comparing two policies, or estimating a gradient by finite differences, use the same random numbers for both. PEGASUS (Ng & Jordan, 2000) fixes the simulator's random seeds in advance, which turns each policy's estimated return into a deterministic function of its parameters: differences between nearby policies then reflect the policies, not the luck of the draw, and standard optimizers apply. It requires a simulator whose randomness you control, and it optimizes the average over a fixed set of scenarios, an estimate of the true objective. Mirrored sampling in evolution strategies, which evaluates θ+ϵ\boldsymbol\theta+\boldsymbol\epsilon and θ−ϵ\boldsymbol\theta-\boldsymbol\epsilon together, is a close relative (antithetic variates).

Bigger batches. Averaging NN independent estimates divides the variance by NN, the bluntest and most reliable tool, and the reason policy-gradient methods scale with parallel actors (Distributed Actor–Critic: A3C & IMPALA).

Key takeaways

  • The variance of the policy-gradient estimator splits into state, action and trajectory terms; baselines act only on the action term, so they cannot remove the noise of future rewards or of which states are visited.
  • The optimal constant baseline is b⋆=E[∥ψ∥2Q]/E[∥ψ∥2]b^\star=\E\big[\norm{\boldsymbol\psi}^2Q\big]/\E\big[\norm{\boldsymbol\psi}^2\big] and any other bb costs E∥ψ∥2 (b−b⋆)2\E\norm{\boldsymbol\psi}^2\,(b-b^\star)^2. With two actions, b⋆b^\star is the value of the opposite policy, and VπV^\pi can even be worse than no baseline.
  • Action-dependent baselines need the correction term ∇θEπ[ϕ]\nabla_{\boldsymbol\theta}\E_\pi[\phi]; on common benchmarks their reported gains were traced to biased implementations (Tucker et al., 2018).
  • Reward-to-go is a free baseline; discounting and λ trade bias for less trajectory noise; common random numbers make comparisons between policies nearly noise-free.

Check yourself

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

Question 1

The variance-optimal constant baseline at a state averages the action values with which weights?

  1. The probabilities π(a∣s)\pi(a\mid s), so that b⋆=Vπ(s)b^\star=V^\pi(s)
  2. The probabilities times the squared norms of the scores, π(a∣s)∥ψ(s,a)∥2\pi(a\mid s)\norm{\boldsymbol\psi(s,a)}^2
  3. The advantages Aπ(s,a)A^\pi(s,a)
  4. Equal weights for every action
Show the answer

B. The probabilities times the squared norms of the scores, π(a∣s)∥ψ(s,a)∥2\pi(a\mid s)\norm{\boldsymbol\psi(s,a)}^2

Minimizing the quadratic E[∥ψ∥2(Q−b)2]\E\big[\norm{\boldsymbol\psi}^2(Q-b)^2\big] in bb gives the π∥ψ∥2\pi\norm{\boldsymbol\psi}^2-weighted average. Rare actions usually have large scores, so they count for more than in VπV^\pi; with two actions, b⋆b^\star is the value of the opposite policy.

Question 2

Two actions, logistic parameterization, π(a1)=0.7\pi(a_1)=0.7, Q(a1)=0Q(a_1)=0 and Q(a2)=10Q(a_2)=10. What is the variance-optimal baseline b⋆b^\star?

  1. 3
  2. 5
  3. 7
  4. 10
Show the answer

C. 7

b⋆=(1−p) Q(a1)+p Q(a2)=0.3×0+0.7×10=7b^\star=(1-p)\,Q(a_1)+p\,Q(a_2)=0.3\times0+0.7\times10=7, the value of the opposite policy, while Vπ=3V^\pi=3. With b⋆b^\star both actions give the exact gradient p(1−p)(Q(a1)−Q(a2))=−2.1p(1-p)\big(Q(a_1)-Q(a_2)\big)=-2.1.

Question 3

What did Tucker et al. (2018) conclude about action-dependent baselines on standard continuous-control benchmarks?

  1. They reduce variance by orders of magnitude, as originally reported
  2. They are biased however they are implemented
  3. They are equivalent to following the natural gradient
  4. Learned state–action baselines reduced variance no more than state baselines, and the reported gains came from implementation choices that departed from the published, unbiased methods
Show the answer

D. Learned state–action baselines reduced variance no more than state baselines, and the reported gains came from implementation choices that departed from the published, unbiased methods

Their variance decomposition showed the action term was small next to the trajectory term, and their review of the released code found deviations, such as a normalization applied to only part of Q-Prop's estimator, that made the methods biased. With the deviations removed, the advantage disappeared.

Question 4

Why does PEGASUS fix the simulator's random numbers before optimizing a policy?

  1. So that the estimated return becomes a deterministic function of the parameters, and differences between nearby policies are not swamped by sampling noise
  2. To remove the need for a simulator
  3. To make the learned policy deterministic
  4. To make the estimate exactly equal to the true expected return
Show the answer

A. So that the estimated return becomes a deterministic function of the parameters, and differences between nearby policies are not swamped by sampling noise

With common random numbers, two policies are scored on the same scenarios, so their difference reflects the policies rather than luck, and ordinary optimizers can work on a deterministic surrogate (gradient methods need it to be differentiable, which a discontinuous reward such as "1 inside the goal region" breaks). The price is that the surrogate averages over a finite set of scenarios: an estimate of the objective that becomes accurate only with enough of them.

Further reading

  • L. Weaver & N. Tao, "The optimal reward baseline for gradient-based reinforcement learning," UAI (2001).
  • E. Greensmith, P. L. Bartlett & J. Baxter, "Variance reduction techniques for gradient estimates in reinforcement learning," JMLR 5, 1471–1530 (2004).
  • S. Gu, T. Lillicrap, Z. Ghahramani, R. E. Turner & S. Levine, "Q-Prop: Sample-efficient policy gradient with an off-policy critic," ICLR (2017).
  • G. Tucker, S. Bhupatiraju, S. Gu, R. E. Turner, Z. Ghahramani & S. Levine, "The mirage of action-dependent baselines in reinforcement learning," ICML (2018).
  • A. Y. Ng & M. I. Jordan, "PEGASUS: A policy search method for large MDPs and POMDPs," UAI (2000).