liveBook · Manning

Monte Carlo simulation

As explained in Grokking Statistics

A Monte Carlo simulation is a computational simulation method that models uncertain real-world processes by repeatedly generating random outcomes from probability distributions.

Monte Carlo simulation models an uncertain real-world process by repeatedly generating random values from probability distributions. Comparing runs reveals possible outcomes when collecting real-world data is difficult. section 3.11 section 3.12

Planning a coffee-shop hire

A coffee-shop owner wants to estimate the extra espresso shots an inexperienced barista might pull during a three-hour morning. A Poisson model can simulate shots per minute; an exponential model can generate the elapsed time before each shot. Repeated runs produce different totals, typically around 40–70 shots, while the exponential version also supplies a delivery timeline. section 3.11

A practical simulation workflow is:

  • Choose a probability distribution that matches the question: event counts over an interval or time between events. section 3.11 section 3.12
  • Generate random outcomes, accumulate them across the time window, and compare results across runs. section 3.11
from numpy.random import exponential as expon

mu = .3  #1

time_elapsed = 0.0  #2
shots_pulled = 0  #3

while True:  #4
    time_elapsed_to_shot = expon(scale=1/mu, size=1)[0]  #5
    time_elapsed += time_elapsed_to_shot  #6
    if time_elapsed > 180:  #7
        break  #8
    shots_pulled += 1  #9
    print(f"{time_elapsed:.2f} minutes: {shots_pulled} shots pulled")  #10

print(f"Total shots pulled in 3 hours: {shots_pulled}")  #11

The event-timeline implementation generates exponential waiting times, stops after 180 minutes, and counts each completed shot. section 3.11

How Grokking Statistics covers it

How deep it goes
Explains distribution choice and demonstrates two implementations, but defers more ambitious multi-component models.
Examples
Uses a coffee-shop barista, espresso-shot counts, and delivery timelines as its central example.
What it assumes
Assumes familiarity with basic probability distributions while introducing their roles through a concrete scenario.
What you can do after
Build it from scratch with NumPy random generators to estimate totals and event timelines.
How it connects
Connects probability distributions with Poisson event counts and exponential waiting times.

Also explained in

Compare how 8 books explain Monte Carlo simulation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Statistics. The section links point to the parts of the book it draws on.