Contents · Probability & Information Theory
Part 0 · Expectation & Monte Carlo
- Expectation, Variance, and Monte Carlo
Part 1 · Entropy & Divergence
Part 2 · Score-Function Gradient
Part 3 · Bayesian Estimation
Part 0 · Expectation & Monte Carlo
Expectation, Variance, and Monte Carlo
Reinforcement learning runs on one operation: estimating an expectation from samples. A return, a value, a policy gradient are all expectations over trajectories the agent cannot enumerate, so it averages over the ones it happened to sample. That average is the Monte Carlo estimator, and its variance is the central difficulty of the whole field.
Expectation and variance
For a random variable with density and any function ,
a weighted average of over every outcome. The spread around that mean is the variance,
Both are integrals we cannot do in closed form once lives in a high-dimensional space (a trajectory of states and actions). Sampling is the way out.
The Monte Carlo estimator
Draw independent samples and average:
Two facts make it work. It is unbiased,
so on average it lands on the right answer for any . And because the samples are independent, variances add and the pulls out:
The standard error is the square root,
The error shrinks like , and (this is the point) that rate carries no . A grid quadrature in dimensions needs points for accuracy ; Monte Carlo pays the same whether is a scalar or a thousand-step trajectory. That dimension-independence is why RL samples instead of integrating.1
A worked example
Let and estimate . Since and a standard normal has mean , variance ,
The single-sample variance uses the fourth moment :
So the estimator has standard error
At that is : a typical run reports about . To cut the error to you need samples. Ten times less error costs a hundred times more samples, the tax in one line.
Watch it converge
Each frame draws a batch of standard normals, squares them, and updates the running mean . The blue curve is that estimate against (log axis); the green funnel is the theoretical band .
- N
- 0
- estimate
- -
- std error
- -
The estimate is erratic for small and then settles into the funnel, staying inside the one standard-error band roughly of the time because is asymptotically normal. The funnel is the whole story: not a fixed accuracy, but an accuracy that buys itself down at rate .
Why this is the whole game in RL
Every value estimate is this average. A Monte Carlo return is one sample of ; the value is its expectation, estimated by averaging returns observed from . The REINFORCE policy gradient is the same move on a different :
an unbiased Monte Carlo estimate of a gradient.2 Its variance is large, because compounds noise over an entire episode, so is big and useful learning needs either many samples or a smaller . Almost every practical trick (baselines, advantage estimation, control variates) is variance reduction: shrink so the same buys a tighter estimate.
Footnotes
-
MacKay, Information Theory, Inference, and Learning Algorithms (2003). ↩
-
Williams, Simple statistical gradient-following algorithms for connectionist reinforcement learning (1992). ↩