Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 36 ยท Getting Started

Simulating Stochastic Processes in Python

Learn to simulate stochastic processes with NumPy: seeds, vectorized paths, a reusable simulator and Monte Carlo estimates checked against exact answers.

Simulation is the practical heart of this track. For most processes you can write the rules in a few lines, run thousands of sample paths, and estimate probabilities or averages by counting. This Monte Carlo approach is also a superb way to test your mathematics: if the formula and the simulation disagree, one of them is wrong.

In this lesson you set up the toolkit you will reuse in every later chapter.

Random numbers and seeds

NumPy's modern generator is created with np.random.default_rng(seed). A seed makes runs reproducible: the same seed gives the same "random" numbers. Computers produce pseudo-random numbers from a deterministic algorithm, but for our purposes they behave like true randomness.

Python Seeds and basic draws

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The p= argument of choice draws from any finite distribution. You will use it for Markov chains.

Vectorize across paths

A loop in Python over a million steps is slow. NumPy lets you draw all steps at once and use cumsum:

Python
steps = rng.choice([-1, 1], size=(n_paths, n_steps))   # all steps for all paths
paths = steps.cumsum(axis=1)                           # one row per path

This produces n_paths complete random walks in one pass. Memory is the limit: n_paths * n_steps numbers.

Monte Carlo estimation

To estimate P(A), simulate many paths and compute the fraction where A happens. To estimate E[f(X)], average f over the paths. The error shrinks like 1/โˆšN for N paths: 100 times more paths gives 10 times less error.

Here is a test with a known answer. For a symmetric random walk of 10 steps, the walk is back at 0 at step 10 with probability C(10,5) / 2^10 = 252/1024 โ‰ˆ 0.2461. We can check this by simulation.

Python Estimate P(S_10 = 0) and compare with the exact value

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The standard error formula โˆš(p(1-p)/N) tells you how far a typical estimate lands from the truth. Always report one when you present a Monte Carlo number.

A reusable simulator

Most discrete-time processes fit the same pattern: start in a state, then repeatedly apply a rule that may use randomness. Write the rule as a function step(state, rng) and reuse a generic loop.

flowchart LR
    A["start state x0"] --> B["step(state, rng)"]
    B --> C["append to path"]
    C --> D{"more steps?"}
    D -- yes --> B
    D -- no --> E["return path"]

The generic simulation loop.

Python One simulator, two different processes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The same loop simulates both. This design lets you swap in the transition rule of a Markov chain, a queue or any other discrete-time model.

Python loops are slow for large simulations. Use a generic loop to get the model right on a few paths, then vectorize or reduce the work when you need millions of runs.

Good habits

  • Seed your generator while developing so results can be reproduced and debugged.
  • Use one generator and pass it around, rather than creating a new one for each call.
  • Check against theory whenever a closed form exists.
  • Report uncertainty: a Monte Carlo estimate is a random number too.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. Why use a seed when simulating?

    The same seed gives the same pseudo-random sequence, so results can be reproduced.
  2. 2. If you multiply the number of simulated paths by 100, the typical Monte Carlo error is divided by about...

    The error scales like 1/โˆšN, and โˆš100 = 10.
  3. 3. What is the exact value of P(S_10 = 0) for the symmetric random walk?

    The walk needs exactly 5 up-steps among 10, which has C(10,5) = 252 favorable sequences out of 2^10 = 1024.

Summary

  • np.random.default_rng(seed) gives reproducible pseudo-random numbers.
  • Vectorize with cumsum to simulate many paths at once.
  • Monte Carlo estimates a probability by a fraction and an expectation by an average; the error shrinks like 1/โˆšN.
  • A generic simulate(step, x0, n, rng) loop works for many discrete-time processes.
  • Always compare with an exact answer when you can.

Next: Classifying Stochastic Processes