Simulate systems over timeLesson 38 of 76 · 8 min

Move between states

Use a Markov model to compare how the starting state affects simulated outcomes at different steps.

A Markov model specifies the next-state distribution from the current state. In this first model, the past affects the future only through that state. The transition matrix has one row per current state and one column per next state.

The code labels clear, cloudy and wet as states 0, 1 and 2. For a path in the clear state, the row [.75,.20,.05] becomes cumulative cutoffs [.75,.95,1]. A uniform draw of .90 falls between .75 and .95, so the path moves to state 1 (cloudy). The code uses the current state’s row for each path at every step.

A transition matrix does not establish that a real process is Markov or that its probabilities are stable. If the duration already spent in a state changes its transition probability, the state description may need to include that duration. MCMC later uses Markov transitions for a different purpose: numerical sampling from a target distribution.

Read a row after fixing the current state
Current → nextClearCloudyWet
Clear0.750.200.05
Cloudy0.250.500.25
Wet0.150.350.50
Reference illustration. The authored transition matrix, ordered clear, cloudy, wet. Each row is a probability distribution for the next state conditional on the current state, so its entries sum to one.

Why must each row sum to one rather than the entire matrix?

Write a prediction (optional)

Seeded teaching simulation

Select Run Python below with the code unchanged.

Tools
About Python ↗

The code is editable. Press Ctrl+Enter or ⌘+Enter to run Python from the editor.

Published example output

This result comes from the original Python. Your runs appear above. Build environment ↗

Initial state: 0
Proportions in order: clear, cloudy, wet
Step 1: [0.7448 0.2067 0.0485]
Step 5: [0.4906 0.3238 0.1856]
Step 20: [0.4532 0.3316 0.2152]
Row sums: [1. 1. 1.]

Read the first result

The output gives the proportions of clear, cloudy and wet paths at steps 1, 5 and 20. Keep those three rows to compare with the next run.

Try an edit

Replace initial_state = 0 with initial_state = 2. Select Run Python again. Compare the clear, cloudy and wet proportions with the first run at steps 1, 5 and 20. At which step does the starting state make the biggest difference?

Back to the Python editor

Scope. The three-state transition probabilities are invented and constant over time.

Try a fresh case

The chance of leaving a state depends on how long it has already lasted. What should you question?

Choose an answer
Need a hint?
Hint 1

Ask what information predicts the next transition.

Hint 2

Check whether the current state contains that information.

Your notes