Worked examples — Monte Carlo simulation — law of large numbers basis
Before symbols fly, one reminder of the three characters from the parent note:
The scenario matrix
Every Monte Carlo problem falls into one of these case classes. The table is the map; the examples below fill every row.
| Cell | Case class | What makes it different | Example |
|---|---|---|---|
| A | Bounded integrand on | Textbook case, finite variance | Ex 1 |
| B | Integral on | Must rescale to a uniform | Ex 2 |
| C | Improper / infinite range | Uniform impossible — change the sampling density | Ex 3 |
| D | Probability = indicator average (0/1 output) | returns only 0 or 1 (Bernoulli) | Ex 4 |
| E | Degenerate: constant → zero variance | , converges in one sample | Ex 5 |
| F | Limiting behaviour: how does error scale? | Plan for a target accuracy | Ex 6 |
| G | Heavy tail: variance infinite → LLN breaks | The one case that does NOT converge | Ex 7 |
| H | Real-world word problem | Translate story → expectation | Ex 8 |
| I | Exam twist: variance reduction | Same , smaller | Ex 9 |
Prerequisite links you may want open: Bernoulli Distribution, Variance and Covariance, Central Limit Theorem, Chebyshev's Inequality, Numerical Integration, Importance Sampling, Strong Law of Large Numbers. This page is a child of Monte Carlo simulation — law of large numbers basis.
Ex 1 — Cell A: bounded integrand on
Active symbol: the random input is (uniform).
Forecast: guess the true value and guess whether samples get you 2 decimals right. (Write it down before reading on.)
Step 1 — Turn the integral into an expectation. Why this step? The uniform density on is the constant , so is by definition the average of . Here .
Step 2 — Write the estimator. Why? Replace the true average by the sample average — the LLN promises it converges.
Step 3 — True value & true variance (so we know what to expect). Why compute ? Because tells us the error size before running anything.
Step 4 — Predicted error at . Why plug in ? We picked the sample size the forecast asked about, so we can decide in advance whether it buys 2 decimals — no simulation needed.
Verify: , and , so a typical run lands within about (two SEs) of — good to roughly 2 decimals. Forecast checks out. Units: dimensionless (pure number).
Ex 2 — Cell B: integral on a general interval
Active symbol: the random input is (uniform, but now on ).
Forecast: the interval is length 3, not 1. Guess: does the " length" factor go inside or outside the average?
Step 1 — Sample uniformly on . A point has density (constant, but now not ). Why this step? On a longer interval, "evenly spread" means each unit of length gets probability , so the density shrinks.
Step 2 — Recover the integral from the expectation. Why? The density divides the integral; to undo it we multiply the average by . The length factor sits OUTSIDE the average.
Step 3 — Estimator. Why this exact form? Step 2 proved ; we simply replace the true average by the sample average (LLN) and keep the constant outside — that is the estimator we will actually run.
Step 4 — True value for checking. Why compute the exact integral here? This case has an elementary antiderivative , so we can get the true by hand and use it as the yardstick the Monte Carlo estimate must match — validation, not the method itself.
Verify: . Sanity: the interval is width 3 and ranges from to , so the integral must lie between and . Our sits comfortably inside. ✔
Ex 3 — Cell C: improper integral on
Active symbol: the random input is (exponential), built from a uniform via .
Forecast: you cannot sample "uniformly on " — an infinite line has no even spread. Guess how we dodge this.

Step 1 — Recognise a built-in density. The function for is a genuine probability density: it is positive and . This is the Exponential(1) distribution. Why this step? Since we cannot make a uniform on an infinite line, we instead sample from a density that already lives there. The red curve in the figure packs samples near 0 and thins out toward infinity, exactly where says the "mass" is.
Step 2 — Split the integrand into density leftover. Why? Whatever we multiply the density by becomes the function we average. Here the leftover is simply , and the active input is now (not a uniform).
Step 3 — Generate exponential samples. Draw and set . This standard trick (inverse-CDF) turns a uniform into an Exponential(1). Why? We only have uniform RNGs; reshapes them into the density we need. This is the seed of Importance Sampling.
Step 4 — Estimator. Why this form? Step 2 rewrote as with , so the sample mean of is the natural estimator — no extra density factor is needed because the is absorbed into how we sample .
Step 5 — True value. Why this formula? Standard result with .
Verify: . Sanity: oscillates between and but the samples cluster near small where , so a positive value below 1 is expected. ✔
Ex 4 — Cell D: probability as an indicator average
Active symbol: the random input is the pair , each coordinate an independent .
Forecast: the region above inside the unit square — guess its area as a fraction.

Step 1 — Define the indicator. Why this step? A probability is the expectation of a 0/1 indicator: . The output of is a coin flip — this is the Bernoulli Distribution.
Step 2 — Estimator = fraction of "yes" darts. Why? Averaging 0s and 1s just counts the fraction of successes — exactly the red-dot fraction in the figure.
Step 3 — True value. The area above the parabola is total square minus area under it: Why subtract? The whole square has area ; the region under the parabola has area ; everything else is the "above" region, so .
Step 4 — Its variance (Bernoulli). With : Why care? Indicator outputs always have variance , maximised at . Extreme probabilities (near 0 or 1) are cheaper to estimate.
Verify: . Sanity: the parabola dips low, so most of the square is above it — a fraction above is expected. ✔
Ex 5 — Cell E: degenerate, zero-variance case
Active symbol: the random input is — though as we'll see, it doesn't matter what we sample.
Forecast: how many samples does this "estimate" need to be exactly right?
Step 1 — Compute the expectation. Why? The average of a thing that's always 7 is 7 — no randomness involved.
Step 2 — Compute the variance. Why this matters? for every , even .
Step 3 — Interpret the degenerate limit. A single sample gives exactly. Adding more samples changes nothing. Why? With zero spread there is no noise to average away — the estimator has already hit the target on sample one.
Verify: , . Any run of any length returns exactly 7. ✔
Ex 6 — Cell F: limiting behaviour, planning
Active symbol: the random input is , same as Ex 1.
Forecast: Ex 1 hit at . To shrink the half-width ~, guess: a bit more samples, or a LOT more?

Step 1 — Write the target. The half-width (defined above) is . Demand Why this step? This half-width is the "" of the confidence interval; forcing it below is exactly the accuracy the problem requests, so we solve for the that achieves it.
Step 2 — Solve for . Why square? Because error lives under a square root; undoing it squares everything — the source of the brutal law.
Step 3 — Plug numbers. : So samples. Why substitute these particular numbers? is the factor from the Central Limit Theorem, is the variance we already computed in Ex 1, and is the squared target; plugging them turns the abstract inequality into a concrete sample budget we can actually buy.
Verify: the red line in the figure shows half-width . Going from half-width (at , i.e. ) down to is an reduction, needing more samples: . ✔ Matches. Twenty-fold accuracy costs ~340× the work.
Ex 7 — Cell G: heavy tail, LLN breaks
Active symbol: the random input is (heavy-tailed, not uniform).
Forecast: the density is symmetric about 0, so surely the mean is 0 and the sample mean converges there. Guess whether that reasoning is safe.

Step 1 — Check the integrability condition. The Weak Law needs . Test it: Why this step? For large the integrand behaves like , and diverges (logarithmically). The mean does not exist.
Step 2 — Consequence. Because , the Strong Law of Large Numbers does not apply. The sample mean does not settle down — remarkably, of Cauchy samples is itself standard Cauchy, no matter how big is. Why this matters? The red trace in the figure keeps taking wild jumps forever; occasional gigantic samples from the fat tails repeatedly hijack the running average.
Step 3 — The moral. Symmetry suggested "mean ", but the mean is undefined, so there is nothing for the LLN to converge to.
Verify: diverges — we confirm the tail has divergent integral by comparing partial integrals growing without bound. ✔ (See VERIFY.)
Ex 8 — Cell H: real-world word problem
Active symbol: the random input per box is a triple of uniforms ; the count of failures is then a derived random variable.
Forecast: with only a per-bulb failure, guess whether returns are rare () or common ().
Step 1 — Model each box. Number of failures (sum of 3 Bernoulli Distribution flips). Define Why this step? "Return probability" is — again a probability written as an indicator average.
Step 2 — Monte Carlo recipe. Simulate 3 uniforms per box, count how many are (failures), record whether , average over many boxes. Why? Each simulated box is one draw of ; the LLN converges the fraction returned to the true probability. A uniform below happens exactly of the time, so it faithfully mimics a bulb failing.
Step 3 — Exact value to check against. Why compute exactly? A word problem with a small discrete model can be checked exactly with the binomial formula , so we validate the simulator against ground truth instead of trusting it blindly.
Verify: (about ) — returns are rare, the "rare" forecast wins. Units: a probability, dimensionless, in . ✔
Ex 9 — Cell I: exam twist, variance reduction
Active symbol: the random input is ; the antithetic partner is also but built from the same draw.
Forecast: both give the same . Guess whether pairing helps, hurts, or does nothing.
Step 1 — True value (for checking). Why start here? has the elementary antiderivative , so we can pin the exact and later confirm both estimators aim at the same target.
Step 2 — Plain estimator variance. , and Numerically and , so . Why? ; variance is second moment minus mean squared, the standard .
Step 3 — Antithetic estimator. For each draw use , then average the 's. Why this step? and are negatively correlated (when one is large the other is small), so their average has smaller variance while keeping the same mean . This is the workhorse of Importance Sampling-style variance reduction.
Step 4 — Antithetic variance (per pair). By symmetry the two variances equal ; the covariance is negative: So Why ? Because is constant — the exponents always add to 1! That's why the correlation is so strongly negative and the variance nearly cancels.
Verify: plain per-sample variance ; antithetic per-pair variance . Ratio smaller variance — a massive win. Same target . ✔