Hyoungseo Son
Probability & Information Theory
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 E[f(X)]\mathbb{E}[f(X)] 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 XX with density pp and any function ff,

E[f(X)]=f(x)p(x)dx,\mathbb{E}[f(X)] = \int f(x)\,p(x)\,dx ,

a weighted average of ff over every outcome. The spread around that mean is the variance,

Var[f(X)]=E[(f(X)E[f(X)])2]=E[f(X)2]E[f(X)]2.\operatorname{Var}[f(X)] = \mathbb{E}\big[(f(X)-\mathbb{E}[f(X)])^2\big] = \mathbb{E}[f(X)^2] - \mathbb{E}[f(X)]^2 .

Both are integrals we cannot do in closed form once XX lives in a high-dimensional space (a trajectory of states and actions). Sampling is the way out.

The Monte Carlo estimator

Draw NN independent samples x1,,xNpx_1,\dots,x_N \sim p and average:

μ^N=1Ni=1Nf(xi).\hat\mu_N = \frac{1}{N}\sum_{i=1}^{N} f(x_i) .

Two facts make it work. It is unbiased,

E[μ^N]=1NiE[f(xi)]=E[f(X)],\mathbb{E}[\hat\mu_N] = \frac{1}{N}\sum_i \mathbb{E}[f(x_i)] = \mathbb{E}[f(X)] ,

so on average it lands on the right answer for any NN. And because the samples are independent, variances add and the 1/N21/N^2 pulls out:

Var[μ^N]=1N2iVar[f(xi)]=σ2N,σ2=Var[f(X)].\operatorname{Var}[\hat\mu_N] = \frac{1}{N^2}\sum_i \operatorname{Var}[f(x_i)] = \frac{\sigma^2}{N} , \qquad \sigma^2 = \operatorname{Var}[f(X)] .

The standard error is the square root,

SE(μ^N)=σN.\operatorname{SE}(\hat\mu_N) = \frac{\sigma}{\sqrt{N}} .

The error shrinks like 1/N1/\sqrt{N}, and (this is the point) that rate carries no dd. A grid quadrature in dd dimensions needs NhdN \sim h^{-d} points for accuracy hh; Monte Carlo pays the same 1/N1/\sqrt{N} whether XX is a scalar or a thousand-step trajectory. That dimension-independence is why RL samples instead of integrating.1

A worked example

Let XN(0,1)X \sim \mathcal{N}(0,1) and estimate μ=E[X2]\mu = \mathbb{E}[X^2]. Since Var[X]=E[X2]E[X]2\operatorname{Var}[X] = \mathbb{E}[X^2] - \mathbb{E}[X]^2 and a standard normal has mean 00, variance 11,

μ=E[X2]=1.\mu = \mathbb{E}[X^2] = 1 .

The single-sample variance uses the fourth moment E[X4]=3\mathbb{E}[X^4]=3:

σ2=Var[X2]=E[X4]E[X2]2=312=2.\sigma^2 = \operatorname{Var}[X^2] = \mathbb{E}[X^4] - \mathbb{E}[X^2]^2 = 3 - 1^2 = 2 .

So the estimator μ^N=1Nixi2\hat\mu_N = \frac{1}{N}\sum_i x_i^2 has standard error

SE(μ^N)=2N.\operatorname{SE}(\hat\mu_N) = \sqrt{\frac{2}{N}} .

At N=100N=100 that is 0.020.141\sqrt{0.02} \approx 0.141: a typical run reports about 1.00±0.141.00 \pm 0.14. To cut the error to 0.010.01 you need N=2/0.012=20,000N = 2/0.01^2 = 20{,}000 samples. Ten times less error costs a hundred times more samples, the 1/N1/\sqrt{N} tax in one line.

Watch it converge

Each frame draws a batch of standard normals, squares them, and updates the running mean μ^N\hat\mu_N. The blue curve is that estimate against NN (log axis); the green funnel is the theoretical band 1±2/N1 \pm \sqrt{2/N}.

Monte Carlo · estimating E[X²] = 1interactive
1021101001k10k100k
N
0
estimate
-
std error
-
Blue is the running estimate of E[X²] from x_i ~ N(0,1); the green funnel is the ± one standard-error band sqrt(2/N) around the true value 1. The estimate wanders for small N and settles into the funnel, which narrows like 1/sqrt(N). Play to draw continuously, +1k to add a batch, Reset to restart, and the slider sets samples per frame.

The estimate is erratic for small NN and then settles into the funnel, staying inside the ±\pm one standard-error band roughly 68%68\% of the time because μ^N\hat\mu_N is asymptotically normal. The funnel is the whole story: not a fixed accuracy, but an accuracy that buys itself down at rate 1/N1/\sqrt{N}.

Why this is the whole game in RL

Every value estimate is this average. A Monte Carlo return Gt=k0γkrt+kG_t = \sum_{k\ge 0}\gamma^k r_{t+k} is one sample of f(X)f(X); the value V(s)=E[Gts]V(s)=\mathbb{E}[G_t \mid s] is its expectation, estimated by averaging returns observed from ss. The REINFORCE policy gradient is the same move on a different ff:

θJ(θ)=E[θlogπθ(as)G]1Ni=1Nθlogπθ(aisi)Gi,\nabla_\theta J(\theta) = \mathbb{E}\big[\, \nabla_\theta \log \pi_\theta(a\mid s)\, G \,\big] \approx \frac{1}{N}\sum_{i=1}^{N} \nabla_\theta \log \pi_\theta(a_i\mid s_i)\, G_i ,

an unbiased Monte Carlo estimate of a gradient.2 Its variance is large, because GG compounds noise over an entire episode, so σ\sigma is big and useful learning needs either many samples or a smaller σ\sigma. Almost every practical trick (baselines, advantage estimation, control variates) is variance reduction: shrink σ\sigma so the same NN buys a tighter estimate.

Footnotes

  1. MacKay, Information Theory, Inference, and Learning Algorithms (2003).

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