mc_mean_and_stderr, is used wherever the book reports a Monte Carlo estimate with an error bar.
A.1 Monte Carlo Estimates and Error Bars
Almost every quantity in RL is an expectation we cannot compute exactly: the objective , a gradient, a pass rate. We estimate it by sampling. Given i.i.d. samples of a random variable with mean and variance , the sample mean is unbiased, , and its variance is The standard error is the standard deviation of the estimate. In practice is unknown and is replaced by the sample standard deviation . By the central limit theorem, is an approximate 95% confidence interval. The error shrinks as : four times as many samples halve it.A.2 The Variance of Sums
A trajectory’s return is a sum of rewards, and a policy-gradient estimate is a sum of per-token terms. The variance of a sum is For independent terms the covariances vanish and the variance grows linearly in . For positively correlated terms it grows faster. With equal variances and pairwise correlation , the variance of the mean of terms is which does not go to zero as when . Samples that share a cause, such as completions of the same prompt or many tokens of one trajectory, carry less information than their count suggests. The effective sample size is . We generate correlated samples by giving every member of a group a shared component plus an independent one, and compare the measured variance of the group mean with the formula.A.3 Control Variates
Suppose we want and know another function whose mean is known exactly. Then for any constant , is still unbiased, and its variance is . Minimizing over gives The more correlated is with , the more variance it removes, and the mean of does not need to be close to the mean of . The baseline in policy gradients is a control variate: Chapter 6 subtracts , whose expectation is exactly zero by Section A.5. Here we check the formula with and for , where .A.4 Importance Sampling
We often need an expectation under one distribution but have samples from another, . In RL, is the current policy and is the older policy or inference engine that generated the data. If wherever , The importance-weighted estimator is unbiased. Its variance is not free: and the factor inside the expectation makes the variance large where puts much more mass than . A useful diagnostic is the effective sample size of the weights, , which equals when all weights are equal and approaches 1 when one weight dominates. Check unbiasedness on a categorical distribution, using a close to and one far from it.Products of ratios
For a trajectory of steps, the importance weight is a product of per-step ratios, . Even when each factor has mean 1 and small spread, the product’s variance grows exponentially with . Model each per-token log-ratio as an independent Gaussian with mean , which makes each ratio have mean exactly 1.A.5 The Log-Derivative Trick
The gradient of an expectation with respect to the parameters of the sampling distribution is using . The right-hand side can be estimated from samples without differentiating , which may be a test suite. This is the score-function or REINFORCE estimator, and is the score. Setting gives the identity that makes baselines legal: Check both on a softmax distribution over five outcomes. Autograd gives the exact gradient of ; the score-function estimator averages over samples.A.6 KL Divergence
The Kullback–Leibler divergence from to is Three properties are used throughout the book. Nonnegativity. By Jensen’s inequality applied to the concave , , with equality only when . Asymmetry. in general. Forward KL, , is what maximum likelihood minimizes; it penalizes for missing modes of the data. Reverse KL, , is the usual RL regularizer; it is estimated with samples from the policy and penalizes the policy for putting mass where the reference has little. Chapter 10 and Chapter 16 discuss the consequences. The chain rule. For sequences generated autoregressively, , so the log-ratio is a sum over tokens and The sequence KL is the expected sum of per-token KLs along prefixes sampled from . This is what justifies per-token KL penalties. Check it exactly on a two-token sequence model with a vocabulary of three.A.7 Estimating KL from Samples
In training we cannot sum over all sequences, so KL is estimated from samples (the policy) of the log-ratio between the policy and a reference . With , the three estimators in John Schulman’s note Approximating KL Divergence (2020) for are is unbiased, because its expectation is the definition, but it is negative for many samples and has high variance. is also unbiased, because , so it is plus a control variate in the sense of Section A.3. It is always nonnegative, since . is biased but has low variance when and are close. Check this numerically.Summary
- A Monte Carlo mean has standard error for independent samples;
mc_mean_and_stderrcomputes it, and correlated samples have a smaller effective sample size. - A control variate with a known mean keeps an estimator unbiased and removes the fraction of its variance. Policy-gradient baselines are control variates.
- Importance sampling is unbiased, but its variance grows with the mismatch between distributions and grows exponentially with the length of a product of ratios.
- The log-derivative trick turns the gradient of an expectation into an expectation of ; the score has mean zero.
- KL is nonnegative and asymmetric, and a sequence KL is the expected sum of per-token KLs. Of the sample estimators, and are unbiased and is never negative.
Exercises
- Show that the variance of the importance-sampling estimator is minimized by . Why can’t this choice be used in practice?
- In Section A.4, change in the products-of-ratios experiment to 0.03. At what does the effective sample size fall below 10%? Compare with the prediction .
- Prove that for every sample, and that .
- Repeat the control-variate experiment with , whose mean is 1. Compute and the variance reduction. Why is it smaller than for ?
- Change an assumption in Section A.1: draw the 200 samples as 25 groups of 8 that share a per-group success probability drawn uniformly from . Does the naive standard error still give 95% coverage? Use Section A.2 to explain the result.
- Design review: a teammate computes a 95% interval for a pass rate from 4,000 rollouts, which are 8 samples on each of 500 tasks, treating all 4,000 as independent. What is wrong, and what should the interval be computed over?