Contents/ Part VI · Probability/ Chapter 37

Probability by Simulation

Every rule in this Part can be checked, or replaced, by a computer that simply runs the experiment many times and counts. The Monte Carlo method turns "I cannot derive this" into "I can simulate this", and it is one of the most practical ideas in all of statistics and AI.

⏱️ ~15 min read
🐍 Notebook included
📊 Chapter 37

This Part taught you to derive probabilities: count outcomes, apply rules, reverse conditionals. But many real questions resist a clean formula. The way out is wonderfully direct: build the random experiment in code, run it tens of thousands of times, and let the frequencies answer the question.

~
The Monte Carlo method estimates a probability or an expectation by simulating the random process many times and averaging the results. It rests on the Law of Large Numbers: as the number of trials grows, the observed frequency converges to the true probability.
🎰
Named for a casino

The method was developed in the 1940s by Stanislaw Ulam and John von Neumann on the Manhattan Project, while computing how neutrons scatter, a problem too tangled for pen and paper. Ulam's uncle gambled at the Monte Carlo casino in Monaco, and the codename stuck. Today the same idea prices options, renders movie lighting, trains reinforcement-learning agents, and powered the engine behind AlphaGo.

1

The Monte Carlo Loop

The entire method is one short loop. Define what counts as a success, run the experiment once at random, repeat a large number of times, and divide the successes by the trials. That ratio is your estimated probability.

Estimate a probability by repeating a random trial Define the event e.g. "sum = 7" Simulate one random trial Repeat N times, count the successes Estimate = count / N repeat × N (loop back)

For example, the chance of at least one six in four rolls is 1 − (5/6)⁴ ≈ 0.5177 by the complement rule. Simulate 500,000 sets of four rolls, count the ones containing a six, and the frequency comes out 0.517: a measurement of the probability, accurate to a few thousandths, with no algebra at all.

2

A Classic: Estimating π with Darts

The showpiece of Monte Carlo: estimate π using nothing but random points. Scatter darts uniformly over a unit square. The fraction landing inside the quarter circle equals the ratio of the areas, which is π/4. Multiply by 4 and you have π.

The fraction of darts inside the quarter circle is π/4 inside (counts) outside fraction inside ≈ 0.785 π ≈ 4 × 0.785 = 3.14

With 100,000 darts the estimate comes out near 3.141. No formula for π was used, only random points and a ratio of areas. This is Monte Carlo integration in miniature: a hard quantity, an area, an integral, an expectation, becomes a simple average over random samples.

3

How Accurate? The 1/√N Law

Simulation is not magic, the estimate has its own random error. That error shrinks in proportion to 1/√N, where N is the number of trials. The honest, and slightly painful, consequence: to cut the error in half you need four times the samples; for one more decimal place, a hundred times.

More trials, less error: the estimate funnels onto the truth true value number of trials N → (to halve the error, quadruple N; error ∝ 1/√N)

On log-log axes, plotting error against N gives a straight line of slope −1/2, the fingerprint of 1/√N convergence. The takeaway is balance: simulation is trivially easy to write, but can be expensive to make precise. For a rough answer it is unbeatable; for high precision, an exact formula (when one exists) wins.

🌱
Set a seed

Because simulation uses randomness, two runs give slightly different answers. Fixing the random seed (for example np.random.default_rng(36)) makes a run reproducible: you, and anyone reading your work, get the exact same numbers every time. Reproducibility is a courtesy and a discipline, not an optional extra.

4

When Intuition Fails: Monty Hall

Simulation's greatest gift is settling arguments. Three doors hide one car and two goats. You pick a door; the host, who knows where the car is, opens a different door to reveal a goat, then asks if you want to switch. Should you? Almost everyone says it cannot matter. Almost everyone is wrong.

200,000 simulated games: switching wins twice as often Stay wins 1/3 (≈ 33%) Switch wins 2/3 (≈ 67%)

Switching wins about 2/3 of the time, double the odds of staying. The reason: your first pick is right only 1/3 of the time, so 2/3 of the time the car is behind one of the other two doors, and the host helpfully removes the wrong one. The host's choice is not random, it leaks information. You can argue this in words forever; 200,000 simulated games end the debate in a second.

5

Monte Carlo in Machine Learning & AI

Monte Carlo is not a beginner's toy that real systems outgrow, it is everywhere in modern AI. Whenever a quantity is an expectation or an integral too hard to compute exactly, the answer is almost always: sample it.

Monte Carlo ideaIn ML / AI it appears asConcrete example
Estimate an expectationReinforcement learningaverage sampled rollouts to estimate expected reward
Sample a posteriorBayesian inference (MCMC)draw from P(parameters | data) when no formula exists
Resample your dataThe bootstrapa confidence interval for any statistic, no algebra
Random forward passesMonte Carlo dropoutrun a net many times to estimate its uncertainty
Search by random rolloutsMonte Carlo Tree Searchthe planning engine behind AlphaGo
🤖
Why this matters for AI research

The hardest quantities in machine learning are expectations over enormous or unknown distributions: an agent's expected return, a model's posterior over parameters, the uncertainty in a prediction. None has a closed form, so practitioners reach for the tool in this chapter, average a batch of samples. MCMC samples Bayesian posteriors; policy-gradient methods estimate reward gradients from sampled episodes; Monte Carlo dropout turns a single network into an uncertainty estimate; the bootstrap quantifies confidence with no formula; and Monte Carlo Tree Search let AlphaGo plan in a space with more positions than atoms in the universe. The simple dart-throwing loop you just wrote is, quite literally, a load-bearing pillar of modern AI.

🐍

Simulate it yourself

The companion notebook estimates a probability by counting (and matches the exact 0.5177), throws 100,000 random darts to approximate π with a scatter plot, measures the 1/√N error law on log-log axes, settles Monty Hall with 200,000 games (stay 1/3, switch 2/3), and bootstraps a 95% confidence interval from a single sample.

📓 View Notebook (code & outputs) ▶ Open in Colab ⬇ View / Download on GitHub

View opens the rendered notebook instantly (no setup). Open in Colab runs & edits it live in your browser. To run locally, install numpy and matplotlib and launch jupyter notebook.

🎓 Key Takeaways

  • Monte Carlo estimates a probability or expectation by simulating the experiment many times and averaging; it rests on the Law of Large Numbers.
  • The loop is: define the event, run one random trial, repeat N times, divide successes by N.
  • Estimating π with random darts shows the core trick: a hard area or integral becomes an average over samples.
  • Error shrinks like 1/√N: quadruple the samples to halve the error. Set a seed for reproducibility.
  • In ML/AI: reinforcement learning, MCMC, the bootstrap, MC dropout, and tree search (AlphaGo) are all Monte Carlo.
6

Practice Challenges

Five short challenges, beginner to intermediate. Try them in Python before checking the solutions, simulation is best learned by writing the loop yourself.

1

Estimate a probability

Simulate 200,000 sets of 3 fair coin flips and estimate P(at least one head). Compare to the exact value.

Hint: exact is 1 − (1/2)³ = 7/8.
2

Estimate an expectation

Estimate E[max of two dice] by simulation. (Awkward by hand, easy to simulate.)

Hint: average the per-trial maximum; exact is 161/36 ≈ 4.47.
3

The birthday problem

In a room of 23 people, estimate P(at least two share a birthday). Prepare to be surprised.

Hint: draw 23 birthdays from 0–364; check for any repeat. Expect about 0.51.
4

Monte Carlo integration

Estimate the integral of x² from 0 to 1 by averaging f(U) for uniform U on [0,1].

Hint: E[U²] equals the integral; exact is 1/3.
5

Bootstrap a CI

Given a sample, estimate a 95% confidence interval for its median by resampling with replacement 10,000 times.

Hint: take the 2.5th and 97.5th percentiles of the resample medians.
Check your work

A fully-worked solutions notebook walks through all five challenges, each verified in code. Try them yourself first, then compare.

📓 View Solutions ▶ Open Solutions in Colab ⬇ View / Download on GitHub
7

Quiz: Test Yourself

Eight quick questions on simulation and Monte Carlo. Answer them, hit Check Answers, and keep refining until you score 100%. Your progress is saved, so you can hop back to the chapter and return anytime.

🏁
That completes Probability

You have built probability from outcomes and events all the way to Bayesian updating, expectation, and simulation. Next, the Probability Distributions part turns to the named distributions, the Bernoulli, binomial, Poisson, and the famous normal curve, that give these ideas their standard shapes.