A1131 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

A11 · Lesson 31 · Monte Carlo simulation

Estimating chances by simulating them in NumPy

A voltage divider made of two 1% resistors should give 2.5 V. What fraction of production units land within ±0.5%? You could work out the distribution of the output by convolution, but with a nonlinear formula and several parts that quickly gets hard. Or you could build a hundred thousand dividers in software, measure them all, and count. That's Monte Carlo simulation: estimate a probability as a long-run frequency (lesson 1) with the computer doing the long run, and use the law of large numbers (lesson 29) to know how far to trust the answer.

  • Each draw builds one 10k/10k divider from 5 V, each resistor uniform within ±t%. The histogram is the output voltage.
  • With two parts the shape is a triangle, not a Gaussian; the line is the exact triangle.
  • The readouts compare the measured fraction within the spec with the triangle and with a Gaussian of the same spread. Which prediction does the simulation agree with?

Predict

A simulation runs 10,000 trials to estimate a probability near 0.5. About how precise is the estimate?

Name it

The method

  1. Write the system as a function of its random inputs: resistor values, noise samples, arrival times.
  2. Draw many independent sets of inputs from their distributions.
  3. Compute the output for each set, and estimate what you need from the results: a fraction (yield, error rate), a mean, a spread, a histogram.
  4. Attach an error bar. A fraction p^ from n trials has standard deviation p^(1−p^)/n; a mean has sn.

The method doesn't care how complicated the function is, which is why circuit simulators offer Monte Carlo runs over part tolerances and why communication systems are checked by simulating millions of bits.

In NumPy

import numpy as np
rng = np.random.default_rng(1)        # a generator with a fixed seed
r = rng.uniform(-1, 1, 100_000)       # 100,000 uniform draws at once
z = rng.normal(0, 0.5, 100_000)       # Gaussian, mean 0, sd 0.5
bits = rng.integers(0, 2, 100_000)    # random 0s and 1s
p_hat = np.mean(np.abs(z) > 1)        # a fraction: the mean of a True/False array
  • Vectorize: draw arrays and compute on whole arrays; a Python loop over a million trials is a hundred times slower.
  • Fix the seed while developing, so a run can be repeated exactly and a bug isn't mistaken for noise. Change it to check the answer doesn't depend on it.
  • Check against a case you can solve. A simulation that reproduces a known formula is far more believable in the cases you can't solve.

Rare events

To estimate a probability p to a relative error r, you need about 1r2 occurrences, so about 1r2p trials. A bit error rate of 10−9 is out of reach by plain simulation; engineers use formulas, bias the simulation toward errors (importance sampling), or extrapolate from higher noise.

Worked example

Simulating 100,000 of the 10k/10k dividers with ±1% resistors gives a mean of 2.49999 V and a standard deviation of 10.22 mV, with 74.9% within ±0.5% (±12.5 mV). How do these compare with the formulas?

Show the solution
  1. Sensitivity. Linearizing, ∂V∂R1=−5R2(R1+R2)2=−1.25×10−4V/Ω
  2. Spread of each resistor. σ=20012=57.7Ω
  3. Spread of the output. σV=2×1.25×10−4×57.7=10.21mV agreeing with the simulation to 0.2%.
  4. Fraction within spec. The output error is close to the difference of two uniform errors, a triangle reaching ±25 mV, so P=1−(1−12.525)2=0.75
  5. Compare with a Gaussian. A Gaussian with the same spread would predict 78%: with only two parts, the central limit theorem hasn't taken hold yet, and the simulation catches it.

Answer the formulas give 10.21 mV and 75% within spec, close to the simulation's 10.22 mV and 74.9%.

Worked example

A simulation of 200,000 bits at A/σ=2 counts 4513 errors. What BER does it measure, and is it consistent with Q(2)?

Show the solution
  1. Measured BER. p^=4513200000=0.0226
  2. Its standard deviation. 0.0226×0.977200000=0.00033
  3. Compare with Q(2). Q(2)=0.02275 lies within one standard deviation, so the simulation agrees with the formula.

Answer it measures a BER of 0.0226, with a standard deviation of 0.00033, consistent with Q(2)=0.02275.

Check

A simulation estimates a probability near 0.5. How many trials give a standard deviation of 0.01?

Check

A 10k/10k divider runs from 5 V, and each resistor is uniform within ±2%. What is the standard deviation of the output, by linearization?

Write units the way you normally would: 1.5 mV, 1500 µV and 0.0015 V are the same answer.

Check

A Monte Carlo run of 1000 circuits finds a fraction 0.95 within spec. What is the standard deviation of that yield estimate?

Check

Write divider_mc(n, rng, tol) returning an array of n output voltages of a 10 kΩ/10 kΩ divider from 5 V, each resistor drawn as 10e3 * (1 + tol * rng.uniform(-1, 1, n)). The test checks the mean, the spread and the yield within ±0.5%.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write ber_mc(r, n, rng): send n random bits as ±1, add Gaussian noise with standard deviation 1/r (so A/σ=r), decide by the sign, and return the fraction of bits decided wrong. The test compares with Q(r).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why does Monte Carlo error shrink only as 1/n, and what does that mean in practice?

Practice

Keep practicing

6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.