§17.1 Two kinds of randomized algorithm
Randomized algorithms split into two families, and the difference decides which one machine learning can use:
| Family | Answer | What is random | |
|---|---|---|---|
| Las Vegas | Las Vegas | always exact | the resources (time/memory) |
| Monte Carlo | Monte Carlo | approximate (random error) | the error (shrinks with compute) |
Because most machine-learning problems admit no precise answer — ruling out exact methods and Las Vegas las vegas algorithm A randomized algorithm that always returns the exact answer (or reports failure) but consumes a random amount of time/memory. defined in ch. 17 — open in glossary algorithms — we lean on Monte Carlo monte carlo algorithm A randomized algorithm returning an answer with random error that shrinks as more compute is spent; gives an approximate answer for any fixed budget (contrast Las Vegas). defined in ch. 17 — open in glossary approximations. Why sample at all? Three recurring reasons: to speed up a tractable-but-costly sum (minibatches subsample the full training cost); to approximate an intractable sum or integral (the gradient of an undirected model’s log partition function, chapter 18); and sometimes because sampling is the goal — we want a model that generates data like the training set.
§17.1.2 The basics: sums as expectations
The trick of Monte Carlo sampling monte carlo sampling Approximating a sum/integral s=E_p[f(x)] by the empirical average ŝ_n=(1/n)Σf(x⁽ⁱ⁾) of n samples from p; unbiased, with error ∝ 1/√n. defined in ch. 17 — open in glossary is to view an intractable sum or integral as an expectation, then approximate it by an average of samples:
Sum/integral as an expectation, then an average (eqs 17.1–17.3)
| the target: rewrite the (maybe exponentially large) sum/integral as an expectation of f under a distribution p | scalar | |
| the Monte Carlo estimate: just the average of f over n samples drawn from p | scalar | |
| n independent samples from p — the whole method rests on being able to draw these | n samples |
This estimate is trustworthy for three reasons. It is unbiased, (eq 17.4). By the law of large numbers it converges almost surely, (eq 17.5), provided is finite. And its own variance falls off as
The 1/n variance law (eqs 17.6–17.7)
| the variance of the estimate — how much it wobbles run to run | scalar | |
| dividing by n means the standard error falls like 1/√n: to halve the error you need 4× the samples, to shrink it 10× you need 100× | the Monte Carlo price |
The central limit theorem adds that is approximately Gaussian around , so we can put confidence intervals on it. See all of this at once — estimate by throwing darts, and watch the error track the line:
The estimate is unbiased and converges to π by the law of large numbers; its error shrinks like 1/√n (the dashed reference). So 100× the samples buys only ~10× the accuracy — the price of Monte Carlo.
Worked example — how many samples?
Suppose has variance . To reach a standard error of we need , i.e. samples. Want instead? That is — a hundred times more samples for a ten-times smaller error. This wall is exactly why a well-chosen sampling distribution (next) can be worth so much.
§17.2 Importance sampling
Everything above assumed we can sample from . When we cannot — or when sampling from is wasteful — importance sampling importance sampling Estimating an expectation (here the softmax negative-phase gradient) by sampling from a cheap proposal distribution q instead of the model, correcting the bias with importance weights — avoids computing scores for the whole vocabulary. Biased importance sampling normalizes the weights. defined in ch. 12 — open in glossary exploits a freedom in the setup. The product can be re-split around any distribution :
The importance-sampling identity (eqs 17.8, 17.10)
| the proposal / importance distribution — the one we actually sample from. Any q with support where pf ≠ 0 is valid | distribution | |
| the importance weight × f: reweight each q-sample by how much more likely it was under p than under q | scalar | |
| the importance-sampling estimator — same target s, but sampled from q | scalar |
The remarkable part: the estimator’s mean does not depend on , (eq 17.11) — every is correct on average. But its variance is exquisitely sensitive to (eq 17.12), and there is a single best choice — optimal importance sampling optimal importance sampling The minimum-variance choice q*∝p(x)|f(x)| (eq 17.13); gives zero variance when f has constant sign, but computing it solves the original problem. defined in ch. 17 — open in glossary :
The optimal proposal (eq 17.13)
| put sampling weight exactly where the integrand is large — where p·|f| has mass | unnormalized | |
| the normalizer making q* a distribution. Computing it essentially solves the original problem, so q* is an ideal, not a recipe | scalar | |
| the optimal proposal: when f has constant sign, Var[ŝ_{q*}] = 0 — a SINGLE sample suffices (in principle), because computing q* has already solved the problem | distribution |
The payoff and the peril are both huge. A good concentrates samples where they matter; a bad — especially in high dimensions, where a simple matches poorly — gives an estimator that usually underestimates and rarely, wildly overestimates it. Estimate a rare-event probability and feel the difference:
At μ = 0 (naive Monte Carlo) almost no samples land in the tail, so the variance is enormous. Slide μtoward the tail and q overlaps p·f — the estimator's variance collapses. The dashed q\* ∝ p·|f| is the optimal (zero-variance) choice of eq 17.13; overshoot μ and the error creeps back up.
When and are only known unnormalized (the usual case for energy-based models), use biased importance sampling biased importance sampling An importance-sampling estimator using unnormalized p̃,q̃ (needs no normalizers); biased but asymptotically unbiased as n→∞. defined in ch. 17 — open in glossary (eqs 17.14–17.16): divide the weighted sum of by the sum of the weights, using and needing no normalizers. It is biased — but asymptotically unbiased, since the denominator converges to as .
Why q matters so much in high dimensions
Look again at the weight . If some samples have tiny while is not small, that ratio explodes, and the variance with it. In high dimensions the dynamic range of joint probabilities is vast, so a simple almost always mismatches somewhere — collecting mostly near-useless samples punctuated by rare giant weights. Despite this, importance sampling is a workhorse in deep learning: accelerating large-vocabulary neural language models (§12.4), estimating partition functions (§18.7), scoring variational-autoencoder likelihoods (§20.10.3), and reducing gradient variance by sampling hard examples more often.
When even a good is out of reach, we give up on independent samples and build a sequence of samples that converges to — the Markov chain Monte Carlo approach.
Check yourself — Monte Carlo & importance sampling
1.What distinguishes a Monte Carlo algorithm from a Las Vegas algorithm?
2.The Monte Carlo estimate ŝ_n = (1/n)Σf(x⁽ⁱ⁾) has which key properties?
3.To cut a Monte Carlo estimate's standard error by a factor of 10, how many more samples do you need?
4.In importance sampling, how does the choice of proposal q affect the estimator?
5.Predict the outcome — in ImportanceSamplingLab you estimate P(x>3) under N(0,1) and slide the proposal mean μ from 0 up toward 3. What happens to the per-sample error?