Monte Carlo Estimation in Depth
First-visit and every-visit estimators: bias, variance and convergence.
Two Monte Carlo estimators read the same episodes and disagree. The unbiased one is not always the more accurate: in a one-state chain, the biased every-visit estimate has less than half the mean squared error of the first-visit estimate after one episode, and times more in the long run. This lesson computes such things exactly: the bias and variance of Monte Carlo estimators, how the variance of a return grows with the horizon, what a constant step size buys, and how to put honest error bars on a value.
What you’ll learn
- Prove that first-visit Monte Carlo is unbiased and that every-visit Monte Carlo is a biased but consistent ratio estimator, and compute both mean squared errors exactly in a one-state chain.
- Derive the variance of the return as an expected discounted sum of squared TD errors, the variance Bellman equation, and bound its growth with the horizon.
- Analyze constant-α Monte Carlo as an exponentially weighted average, and derive the step size that best tracks a drifting value.
- Build asymptotic and finite-sample confidence intervals for Monte Carlo estimates, and narrow them with control variates.
First-visit and every-visit as statistical estimators
Proof. The time of the first visit is a stopping time, so by the strong Markov property the trajectory after it is distributed as a fresh trajectory from , independent of how the episode got there; distinct episodes are independent. That gives (a). For (b), apply the same property at every visit: each has conditional mean given that visit occurs, so . The pairs are i.i.d. across episodes, and the strong law applied to numerator and denominator gives . But is a ratio estimator, and the expectation of a ratio is not the ratio of expectations: returns from the same episode share their future, and episodes with many visits are weighted more heavily.
Singh and Sutton (1996) analyzed these estimators, for undiscounted absorbing Markov chains, as the batch limits of TD(1) with two kinds of eligibility trace: accumulating traces give every-visit MC, replacing traces first-visit MC. They showed that the every-visit method is biased, the first-visit method unbiased, and that the first-visit method has the lower mean squared error in the long run, with a close relation to the maximum-likelihood estimate. The smallest example already shows the whole story.
One nonterminal state . Each step pays reward 1; the chain stays in with probability and terminates with probability ; . An episode lasts steps on , the returns after its visits are , and
First visit. , the mean episode length, so its mean squared error is pure variance:
Every visit, one episode. . Its mean is , so it underestimates, and its variance is a quarter of :
Every visit, many episodes. With , the delta method for the ratio gives . Using the first four moments of the geometric distribution, this is times the first-visit constant:
| Episodes | ||
|---|---|---|
| 1 | 2.00 | 0.75 |
| 5 | 0.40 | 0.37 |
| 10 | 0.20 | 0.23 |
| 50 | 0.040 | 0.056 |
| as | 2 | 3 |
Why does first-visit win in the end? Here is the maximum-likelihood estimate of : the likelihood of geometric lengths is maximized at . It is also the certainty-equivalence value, the value of the chain whose staying probability is the observed fraction of steps that stayed. And it attains the Cramér–Rao bound , so no unbiased estimator does better. Every-visit uses more returns per episode, which helps when episodes are few, but those returns are deterministic functions of the same and add nothing asymptotically except bias and extra weight on long episodes.
In the one-state chain with (true value ), a single episode happens to last steps. What is the every-visit estimate?
Show the answer
2.5
The returns after the four visits are , whose average is ; the first-visit estimate is . On average over episodes, every-visit's single-episode estimate is , a bias of : later visits have shorter remaining returns and pull the average down.
How noisy is a return? A Bellman equation for variance
First-visit MC is unbiased, so its error is all variance, . How large is the variance of a return, and how does it grow with the horizon? A one-line identity answers both: the Monte Carlo error is a discounted sum of TD errors.
Let be the TD error computed with the true values, with at termination. Summing , every intermediate value cancels:
The Bellman equation says , so each has conditional mean zero given the history up to . The are martingale differences, hence uncorrelated: for , is known at time , and
Square the sum: the cross terms vanish, and the variance of the return is the expected discounted sum of squared surprises, discounted by :
Split off the first term and use the Markov property: satisfies its own Bellman equation, with "reward" and discount (Sobel, 1982):
Two regimes follow. If transitions are deterministic and only the rewards are noisy, with variance per step, every surprise is a reward fluctuation and : the standard deviation grows like the square root of the horizon. At and it is , and a 95% interval of needs about episodes. If instead one early random transition decides between futures whose values differ by order , a single surprise is that large and the standard deviation grows linearly in the horizon. Monte Carlo's cost is set by these surprises, and TD learning, whose target contains only one of them, is designed to avoid paying for all of them at once.
Why does the variance Bellman equation discount by rather than ?
- Because variances of later steps are less reliable
- Because the return from the next state enters multiplied by , and scaling a random variable by scales its variance by
- Because the TD errors are squared, which squares every term of the return
- It is a convention; would work equally well
Show the answer
B. Because the return from the next state enters multiplied by , and scaling a random variable by scales its variance by
, and . The surprises are uncorrelated, so their variances add with weights .
Constant step sizes: forgetting and tracking
Constant- Monte Carlo, , unrolls into an exponentially weighted average:
Its weights have effective sample size , so it behaves like an average of the last or so returns. On a stationary problem with independent returns of variance , the bias decays like and the variance settles at : the estimate never converges, it hovers. Why accept that? Because in control the target moves.
Model the drift as a random walk observed through noisy returns: in round the value is with , and the agent sees with , everything independent. Let be the error of the current estimate as a prediction of this round's value. Substituting the update:
The three terms are independent, so in steady state :
A small lags behind the drift (the term); a large one passes the noise through (the term). Setting the derivative to zero gives a quadratic in , with :
This is exactly the steady-state gain of the Kalman filter for a random walk observed in white noise, so in this model no linear estimator tracks better. The running mean, , is the worst choice here: its effective memory keeps growing, so its lag behind the drifting value grows without bound.
Returns have noise and the value they estimate drifts by per episode. What constant step size minimizes the steady-state squared error?
Show the answer
0.049
, so , close to . Its mean squared error is about , against at (too noisy) and at (too slow).
In generalized policy iteration the drift is real: every policy change shifts the returns that a state's estimate averages, so constant steps are the norm in practice. The price is that convergence statements then hold only in a weak sense (a noise ball, or convergence with decaying steps), and the interplay between how fast the policy changes and how fast the values average is exactly what makes the convergence of Monte Carlo control delicate (Monte Carlo Control & Its Convergence).
Error bars, sample sizes and control variates
With independent first-visit returns, the central limit theorem gives the asymptotic confidence interval . For guarantees at finite , use a concentration inequality. If returns lie in an interval of length ( for rewards in ), Hoeffding's inequality gives , and Bernstein's inequality, which uses the variance , gives . The episodes needed for accuracy with probability :
Any sum of terms with conditional mean zero is a valid control variate for a return. If the expected rewards are known, has mean zero, and keeps the mean while discarding all reward noise. With a model and an approximate value function , take the centered surprises , where is the TD error computed with ; telescoping as before,
With the sum vanishes and the estimate is with zero variance: what remains is governed by the Bellman residual of . A model-free learner cannot compute , which is why policy-gradient methods use the one control variate they can always center, a state-dependent baseline (Baselines & Variance Reduction), and why off-policy evaluation combines models with importance weights in doubly robust estimators.
Key takeaways
- First-visit MC averages i.i.d. returns (strong Markov property), so it is unbiased with variance ; every-visit MC is a ratio estimator, consistent but biased.
- In a one-state chain, every-visit has MSE against after one episode, but times first-visit's asymptotically; first-visit is there the maximum-likelihood estimate and attains the Cramér–Rao bound.
- with uncorrelated true TD errors, so is the expected -discounted sum of squared surprises and obeys a Bellman equation; it grows like the horizon or its square.
- Constant- MC is an exponentially weighted average with a noise floor ; for a value drifting by per round, the best constant step is about , the Kalman gain.
- Variance-aware (Bernstein) intervals can need far fewer episodes than range-based (Hoeffding) ones; control variates remove noise in proportion to their correlation with it, perfectly for a control variate built from .
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Why are the first-visit returns from different episodes independent and identically distributed?
- Because every episode starts in
- Because the first visit is a stopping time, so by the strong Markov property the future after it is a fresh trajectory from , independent of the past and of other episodes
- Because rewards are bounded
- Because first-visit MC uses a step size of
Show the answer
B. Because the first visit is a stopping time, so by the strong Markov property the future after it is a fresh trajectory from , independent of the past and of other episodes
Whatever happened before the first visit, the process restarts at at that random time, so each first-visit return has the law of the return from . Episodes need not start in , and boundedness only matters for finite variance.
In the one-state chain with , which estimator has the lower mean squared error after one episode, and which after 50 episodes?
- First-visit after one episode; every-visit after 50
- First-visit in both cases, because it is unbiased
- Every-visit after one episode; first-visit after 50
- Every-visit in both cases, because it uses more returns
Show the answer
C. Every-visit after one episode; first-visit after 50
After one episode every-visit's MSE is against first-visit's : its bias is small compared with the variance it removes. Asymptotically every-visit's MSE is times first-visit's; at they are about and .
Constant- Monte Carlo runs on a problem whose values never change, with independent returns of variance . What happens to its mean squared error as ?
- It tends to 0, like the running mean's
- It grows without bound
- It settles at about
- It settles at , as if only one return were used
Show the answer
C. It settles at about
The bias decays like , but each new return keeps moving the estimate by times its noise, so the variance settles at , roughly the variance of an average of returns. The same property lets it track a value that drifts.
A return is corrected by subtracting , where the are TD errors computed with the exact . What is the variance of the corrected estimate, and what is the catch?
- Zero, because the corrected estimate equals ; the catch is that computing the correction requires , the very thing being estimated
- Unchanged, because subtracting a zero-mean term cannot reduce variance
- Zero, and there is no catch: this is how TD(0) works
- Doubled, because two noisy quantities are combined
Show the answer
A. Zero, because the corrected estimate equals ; the catch is that computing the correction requires , the very thing being estimated
By telescoping, , a constant given . The ideal control variate removes all the noise but needs the answer. With an approximate and a model, the leftover variance is governed by the Bellman residual of , which is how practical control variates and doubly robust estimators trade modeling effort for variance.
Further reading
- S. P. Singh & R. S. Sutton, "Reinforcement learning with replacing eligibility traces," Machine Learning 22, 123–158 (1996).
- M. J. Sobel, "The variance of discounted Markov decision processes," Journal of Applied Probability 19 (1982).
- M. G. Azar, R. Munos & H. J. Kappen, "Minimax PAC bounds on the sample complexity of reinforcement learning with a generative model," Machine Learning 91, 325–349 (2013).
- R. S. Sutton & A. G. Barto, Reinforcement Learning: An Introduction, ch. 5 (MIT Press, 2nd ed., 2018).