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.
| Current → next | Clear | Cloudy | Wet |
|---|---|---|---|
| Clear | 0.75 | 0.20 | 0.05 |
| Cloudy | 0.25 | 0.50 | 0.25 |
| Wet | 0.15 | 0.35 | 0.50 |
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.
A saved draft is open. To run the published example, open Tools and select Restore example. Recover draft brings your code back.
Execution error
Technical error details
Your result
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?
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?
Need a hint?
Hint 1
Ask what information predicts the next transition.
Hint 2
Check whether the current state contains that information.