Hyoungseo Son
Probability & Information Theory
Contents · Probability & Information Theory

Part 0 · Expectation & Monte Carlo

Part 1 · Entropy & Divergence

Part 2 · Score-Function Gradient

  • The Score-Function Gradient

Part 3 · Bayesian Estimation

Part 2 · Score-Function Gradient

The Score-Function Gradient

In reinforcement learning the objective is an expected return J(θ)=Expθ[f(x)]J(\theta)=\mathbb{E}_{x\sim p_\theta}[f(x)], where the policy pθp_\theta both generates the samples and carries the parameter you want to optimize. You cannot naively push θ\nabla_\theta inside the average, because the distribution you are averaging against is itself moving with θ\theta.

The log-derivative trick

Write the expectation as an integral and differentiate:

θExpθ[f(x)]=θf(x)pθ(x)dx=f(x)θpθ(x)dx.\nabla_\theta \mathbb{E}_{x\sim p_\theta}[f(x)] = \nabla_\theta \int f(x)\,p_\theta(x)\,dx = \int f(x)\,\nabla_\theta p_\theta(x)\,dx.

The one identity that rescues this is θpθ=pθθlogpθ\nabla_\theta p_\theta = p_\theta\,\nabla_\theta\log p_\theta. Substituting it turns the integral back into an expectation:

θExpθ[f(x)]=f(x)pθ(x)θlogpθ(x)dx=Expθ ⁣[f(x)θlogpθ(x)].\nabla_\theta \mathbb{E}_{x\sim p_\theta}[f(x)] = \int f(x)\,p_\theta(x)\,\nabla_\theta\log p_\theta(x)\,dx = \mathbb{E}_{x\sim p_\theta}\!\big[f(x)\,\nabla_\theta\log p_\theta(x)\big].

The vector θlogpθ(x)\nabla_\theta\log p_\theta(x) is the score. This is the likelihood-ratio or score-function estimator, known in RL as REINFORCE.12

It never differentiates ff

Read the right-hand side carefully: f(x)f(x) enters only as a plain multiplicative weight, never inside a derivative. So ff can be a black box: a discontinuous reward, a raw game score, an entire non-differentiable simulator. You need only sample xx, evaluate f(x)f(x), and know the score of your own sampling distribution. Contrast the pathwise (reparameterization) gradient, which pushes \nabla through ff and therefore requires df/dx\mathrm{d}f/\mathrm{d}x to exist, the way a differentiable simulator does. The score-function estimator trades that requirement for sampling, so it works exactly where the pathwise gradient cannot go.

Baselines cut variance for free

Any constant bb may be subtracted from ff without biasing the estimator, because the score has mean zero:

Expθ[θlogpθ(x)]=θpθ(x)dx=θpθ(x)dx=θ1=0.\mathbb{E}_{x\sim p_\theta}[\nabla_\theta\log p_\theta(x)] = \int \nabla_\theta p_\theta(x)\,dx = \nabla_\theta\int p_\theta(x)\,dx = \nabla_\theta 1 = 0.

Hence

E[(f(x)b)θlogpθ(x)]=E[f(x)θlogpθ(x)]\mathbb{E}\big[(f(x)-b)\,\nabla_\theta\log p_\theta(x)\big] = \mathbb{E}\big[f(x)\,\nabla_\theta\log p_\theta(x)\big]

for every bb. The mean is untouched, but the variance is not: choosing bb near the typical reward shrinks the fluctuating factor (f(x)b)(f(x)-b), and with it the spread of the estimate. A running mean reward is the standard cheap choice.

A worked example

Take a scalar Gaussian policy pθ=N(θ,1)p_\theta=\mathcal{N}(\theta,1) with reward f(x)=xf(x)=x. Its log-density and score are

logpθ(x)=12(xθ)212log2π,θlogpθ(x)=xθ.\log p_\theta(x) = -\tfrac12 (x-\theta)^2 - \tfrac12\log 2\pi, \qquad \nabla_\theta\log p_\theta(x) = x-\theta.

So the estimator is g=f(x)(xθ)=x(xθ)g = f(x)\,(x-\theta) = x\,(x-\theta), and its mean recovers the exact gradient:

E[x(xθ)]=E[x2]θE[x]=(θ2+1)θ2=1,\mathbb{E}[x(x-\theta)] = \mathbb{E}[x^2]-\theta\,\mathbb{E}[x] = (\theta^2+1) - \theta^2 = 1,

which matches θE[x]=θθ=1\nabla_\theta\mathbb{E}[x]=\nabla_\theta\theta=1 directly. A single sample is noisy: at θ=2\theta=2, drawing x=2.5x=2.5 gives g=2.5(0.5)=1.25g = 2.5(0.5)=1.25, and drawing x=1.3x=1.3 gives g=1.3(0.7)=0.91g=1.3(-0.7)=-0.91. Each is far from 11; averaged, they converge to it.

Now the variance. Let u=xθN(0,1)u=x-\theta\sim\mathcal N(0,1) and c=θbc=\theta-b, so with baseline bb the estimator is (u+c)u=u2+cu(u+c)\,u=u^2+cu. Using E[u]=0\mathbb{E}[u]=0, Var(u2)=2\mathrm{Var}(u^2)=2, and E[u3]=0\mathbb{E}[u^3]=0,

Var[(f(x)b)(xθ)]=Var(u2+cu)=2+c2=2+(θb)2.\mathrm{Var}\big[(f(x)-b)(x-\theta)\big] = \mathrm{Var}(u^2+cu) = 2 + c^2 = 2 + (\theta-b)^2.

Without a baseline, b=0b=0 and c=θc=\theta, so the variance is 2+θ22+\theta^2; at θ=2\theta=2 that is 66. The variance-minimizing baseline is b=θb=\theta, which for f(x)=xf(x)=x equals the mean reward E[x]=θ\mathbb{E}[x]=\theta, driving the variance down to its floor of 22. Same estimate of 11, a third of the variance.

The demo below draws NN samples of xN(θ,1)x\sim\mathcal N(\theta,1), forms both estimators over the identical draws, and plots the per-sample values on a shared gradient axis. Slide θ\theta up and the no-baseline spread (amber) widens as 2+θ22+\theta^2; the baselined spread (green) stays pinned near 22. Both clouds center on the same true gradient, 11.

Score-function gradient of N(θ, 1)interactive
-10-50510true = 1f(x)·s(f(x)−b)·s
estimate
1.187 vs 1.000
var, no baseline
6.85 (2+θ²=6.00)
var, + baseline
2.75 (≈2)
variance cut
2.5×
Each dot is one sample's estimate f(x)·(x−θ). Amber is the raw estimator, green subtracts the mean-reward baseline. The dashed line marks the true gradient, 1. The two variances on the right stay in step with the theory (2+θ² vs 2); toggle the baseline and raise N to watch the estimate settle.

A score-function estimator has exactly two knobs. What distribution you sample from fixes the score you multiply by; what you subtract from the reward fixes the variance you pay. Neither touches the bias, and neither needs a derivative of the reward.

Footnotes

  1. Williams, Simple statistical gradient-following algorithms for connectionist reinforcement learning (1992).

  2. Sutton, McAllester, Singh & Mansour, Policy Gradient Methods for Reinforcement Learning with Function Approximation (2000).