Monte Carlo Simulation Tutorial
Learn Monte Carlo Simulation step by step: 38 lessons in 9 chapters with examples, quizzes, diagrams and practice.
Start learning →
of lessons completed
Getting Started
- What Is Monte Carlo Simulation? Learn what Monte Carlo simulation is, why random sampling can answer hard questions, and run your first experiment in Python.
- Your First Simulation: Estimating Pi Estimate pi by throwing random darts at a square, see why the idea works, and vectorize the code with NumPy for speed.
- The Law of Large Numbers and Simulation Error See why Monte Carlo averages converge to the true value, plot running averages, and learn the 1/sqrt(N) rule for simulation error.
- Confidence Intervals for Monte Carlo Estimates Report a Monte Carlo answer with an honest error bar: central limit theorem, 95% intervals, coverage checks and choosing the sample size.
- Simulating Dice, Cards and Games Turn game rules into code: the birthday problem, the Monty Hall puzzle and a gambler's ruin walk, each checked against exact answers.
Random Numbers
- Pseudorandom Numbers and Seeds Learn how computers make 'random' numbers, why seeds make runs repeatable, and how to use seeds safely in Monte Carlo experiments.
- Linear Congruential Generators Build a linear congruential random number generator from scratch, see its period and its flaws, and learn why modern generators replaced it.
- Testing Randomness Check a random number generator with a chi-square uniformity test, a runs test and autocorrelation, and learn what a p-value does and does not tell you.
- NumPy Generators and Reproducible Streams Use numpy.random.default_rng for fast, reproducible simulation, spawn independent streams for parallel work, and avoid common seeding mistakes.
Sampling Methods
- Inverse Transform Sampling Turn uniform random numbers into exponential, Weibull and custom distributions with the inverse CDF method, verified by histograms and KS tests.
- Sampling the Normal Distribution: Box–Muller Generate normal random numbers with the Box–Muller transform and the polar method, then build correlated normals with a Cholesky factor.
- Acceptance–Rejection Sampling Sample from awkward densities by proposing from an easy distribution and accepting with the right probability. Build it, test it and tune its efficiency.
- Sampling Discrete Distributions Draw from any discrete distribution with cumulative sums, build binomial and geometric samplers, and meet the fast alias method.
- Sampling Points in Shapes and on Spheres Pick uniformly random points in a disk, a triangle and on a sphere, avoid the classic mistakes, and see why high-dimensional balls hold little volume.
Monte Carlo Integration
- Hit-or-Miss Integration Estimate definite integrals by throwing random points under a curve, compute the error bar, and see how it compares with the rectangle rule.
- Sample-Mean Integration Estimate integrals as the interval length times the average of f at random points, use any sampling density, and compare variance with hit-or-miss.
- Multidimensional Integration Integrate over boxes and odd regions in many dimensions, see why grid methods fail, and why Monte Carlo's 1/sqrt(N) error ignores dimension.
- Quasi-Monte Carlo and Low-Discrepancy Sequences Replace random points with evenly spread low-discrepancy sequences (van der Corput, Halton, Sobol) and get faster convergence for smooth integrals.
Variance Reduction
- Antithetic Variates Pair each random draw with its mirror image to cancel noise. Learn antithetic variates, when they work, and measure the gain in Python.
- Control Variates Reduce Monte Carlo variance by subtracting a correlated quantity with a known mean. Derive the best coefficient and test it in Python.
- Importance Sampling Sample where the action is and reweight. Learn importance sampling, the likelihood ratio, effective sample size and rare-event estimates in Python.
- Stratified Sampling and Latin Hypercubes Split the input range into strata and sample each one to spread points evenly. Compare stratified and Latin hypercube sampling in Python.
Markov Chain Monte Carlo
- Markov Chains for Sampling Understand transition matrices, stationary distributions and detailed balance, the ideas that let a Markov chain sample from any target distribution.
- The Metropolis-Hastings Algorithm Build the Metropolis-Hastings sampler from scratch: proposals, the acceptance ratio, step-size tuning and a bimodal example you can run.
- Gibbs Sampling Sample a joint distribution by cycling through full conditionals. Build a Gibbs sampler for a bivariate normal and see why correlation slows it.
- MCMC Diagnostics: Is My Chain Any Good? Check an MCMC run with trace plots, autocorrelation, effective sample size and the Gelman-Rubin R-hat. Implement each diagnostic from scratch.
Simulation of Systems
- Simulating Random Walks Simulate random walks, measure how far they wander, and test when they return home. A first model of a system built from random steps.
- Simulating a Queue Simulate a single-server queue with Lindley's recursion, compare with the M/M/1 formulas, and see why waits explode as utilization nears 100%.
- Discrete-Event Simulation Build a discrete-event simulator with a priority-queue event list, simulate a bank with two tellers and check the result against Erlang C.
- Inventory Simulation and the Newsvendor Problem Choose how much stock to order when demand is random. Simulate profits, find the best order quantity and verify it with the critical ratio rule.
Applications in Finance and Science
- Option Pricing by Monte Carlo Price a European call by simulating geometric Brownian motion, check against Black-Scholes, then add variance reduction and price an Asian option.
- Value at Risk by Simulation Estimate Value at Risk and expected shortfall for a two-asset portfolio by simulating correlated returns, and see how fat tails change the answer.
- Simulating an Epidemic with the SIR Model Simulate the spread of an infection with a random SIR model, see why many outbreaks fizzle out, and compare final sizes with the deterministic theory.
- Diffusion and Percolation Simulate diffusing particles and measure the diffusion coefficient, then explore percolation on a grid and find the critical threshold near 0.593.
Projects and Practice
- Project: Scheduling Under Uncertainty Simulate a project network with uncertain task durations, estimate finish-time percentiles, find critical tasks and choose a realistic deadline.
- Project: The Ising Model of Magnetism Simulate the 2-D Ising model with the Metropolis algorithm, watch magnetism appear below a critical temperature and compare with Onsager's exact result.
- Practice: Variance Reduction Practice problems on antithetic variates, control variates, importance sampling and stratified sampling, each with a worked, runnable solution.
- Practice: MCMC and Simulation of Systems Four practice problems on Metropolis sampling, Gibbs sampling, queues and inventory, each with a runnable solution you can check against theory.