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 , where the policy both generates the samples and carries the parameter you want to optimize. You cannot naively push inside the average, because the distribution you are averaging against is itself moving with .
The log-derivative trick
Write the expectation as an integral and differentiate:
The one identity that rescues this is . Substituting it turns the integral back into an expectation:
The vector is the score. This is the likelihood-ratio or score-function estimator, known in RL as REINFORCE.12
It never differentiates
Read the right-hand side carefully: enters only as a plain multiplicative weight, never inside a derivative. So can be a black box: a discontinuous reward, a raw game score, an entire non-differentiable simulator. You need only sample , evaluate , and know the score of your own sampling distribution. Contrast the pathwise (reparameterization) gradient, which pushes through and therefore requires 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 may be subtracted from without biasing the estimator, because the score has mean zero:
Hence
for every . The mean is untouched, but the variance is not: choosing near the typical reward shrinks the fluctuating factor , 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 with reward . Its log-density and score are
So the estimator is , and its mean recovers the exact gradient:
which matches directly. A single sample is noisy: at , drawing gives , and drawing gives . Each is far from ; averaged, they converge to it.
Now the variance. Let and , so with baseline the estimator is . Using , , and ,
Without a baseline, and , so the variance is ; at that is . The variance-minimizing baseline is , which for equals the mean reward , driving the variance down to its floor of . Same estimate of , a third of the variance.
The demo below draws samples of , forms both estimators over the identical draws, and plots the per-sample values on a shared gradient axis. Slide up and the no-baseline spread (amber) widens as ; the baselined spread (green) stays pinned near . Both clouds center on the same true gradient, .
- estimate
- 1.187 vs 1.000
- var, no baseline
- 6.85 (2+θ²=6.00)
- var, + baseline
- 2.75 (≈2)
- variance cut
- 2.5×
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
-
Williams, Simple statistical gradient-following algorithms for connectionist reinforcement learning (1992). ↩
-
Sutton, McAllester, Singh & Mansour, Policy Gradient Methods for Reinforcement Learning with Function Approximation (2000). ↩