Project 11: Monte Carlo Probability Lab

A self-contained deep-dive from the canonical Math from Foundations to Machine Learning curriculum.

  • Difficulty: Beginner-Intermediate
  • Time: 12-20 hours
  • Language: Python (alternatives: R, Julia, JavaScript, Rust)
  • Prerequisites: Project 7
  • Merged from: HS P05; ML-Math P12; ML-Found P09

What You Will Build

Build a seeded simulation suite that begins with descriptive statistics and grows into reusable Monte Carlo experiments. Load or generate a small dataset and compute frequency tables, mean, median, variance, and standard deviation. Then simulate coins, dice, cards, a roulette-style casino game, and geometric estimation of π. For every experiment, define the sample space and random variable, compute theoretical probabilities or expected value when available, track running estimates, and display confidence intervals and error against trial count. Include a bankroll simulation so negative expected value becomes observable over many repeated plays.

Real World Outcome

A single command runs reproducible experiments and generates a report containing summary statistics, empirical versus theoretical frequencies, an EV table, and convergence plots. The π view shows inside/outside points and the running estimate 4 * inside / total. Casino runs display bankroll paths from multiple seeds plus average drift and uncertainty, making clear that short-term wins can coexist with a negative long-run expectation.

Core Question

How can repeated random experiments estimate quantities and decisions reliably while every individual run remains uncertain?

Concepts You Must Understand First

  1. Descriptive statistics: center, spread, frequency tables, outliers, and why mean and median answer different questions.
  2. Sample spaces and events: probability is defined over explicit possible outcomes, not vague “randomness.”
  3. Random variables, expectation, and variance: EV is a probability-weighted average, not a guaranteed payoff.
  4. Law of large numbers: sample averages stabilize with more independent trials. See Blitzstein and Hwang, Introduction to Probability, Chapter 1.
  5. Standard error and confidence intervals: Monte Carlo precision generally improves on the order of 1/sqrt(n). See Bhargava, Grokking Algorithms, Monte Carlo discussion.

Build Milestones

  1. Implement tested descriptive-statistics functions and frequency reports for a small dataset.
  2. Define a common experiment interface with seed, trial count, outcome, and payoff.
  3. Add coin, dice, card, and casino experiments with theoretical probability/EV comparisons.
  4. Add π estimation, point visualization, running estimates, and interval/error plots.
  5. Run batches across seeds and sample sizes; export assumptions, uncertainty, and convergence evidence.

Hints in Layers

  1. Keep the random generator object explicit and pass it into experiments so seeds truly control every draw.
  2. Record cumulative sums and squared sums online; you need not retain millions of outcomes for convergence statistics.
  3. Validate games by enumerating one-round outcomes before trusting a large simulation.

Common Pitfalls and Debugging

  • Symptom: the same seed produces different reports. Cause: multiple implicit RNGs or changed draw order. Fix: use one injected generator and log configuration.
  • Symptom: roulette appears profitable. Cause: payouts omit the lost stake or probabilities do not sum to one. Fix: enumerate every outcome and compute theoretical EV first.
  • Symptom: confidence intervals are implausibly narrow. Cause: variance or standard error used the wrong denominator. Fix: test against a Bernoulli case with known variance.

Definition of Done

  • Descriptive statistics match hand-computed fixtures.
  • Every random experiment is seeded and reproducible.
  • Empirical frequencies and EV are compared with theoretical values where known.
  • π and casino experiments include convergence and uncertainty visualizations.
  • Batch runs across seeds demonstrate variation rather than hiding it.
  • The report states assumptions, trial count, seed, and limitations.

Previous: Project 10 · Complete learning path · Next: Project 12