SimuVeda/ Mathematics

Mathematics · Simulator 053

Markov Chains

Tomorrow's weather depends only on today's: that's a Markov chain. Edit the transition probabilities between sunny, cloudy and rainy days, watch a random walk hop between states, and see the long-run share of each state settle onto the stationary distribution.

CollegeCollege probability

Experiments 0 of 0 Challenge not tried

How the model works

A Markov chain has states and a transition matrix P, where Pᵢⱼ is the probability of moving from state i to state j in one step. Each row sums to 1. The future depends only on the present state, not the history (the Markov property).

The distribution after n steps is π₀Pⁿ. For a chain where every state can reach every other and there is no fixed cycle, this converges to a unique stationary distribution π with πP = π, whatever the starting state. The simulator finds π by repeatedly applying P.

The bars compare the fraction of days spent in each state so far with π. They agree better and better as the walk gets longer (the ergodic theorem). Mean return time to a state is 1/πᵢ.

Illustrative weather model with editable probabilities, not real climate data. Rows are normalised automatically if they do not sum to 1.

Guided experiments

  1. Default weather. Run 1000 days. Prediction: the visit fractions converge to the stationary distribution shown by the outline bars, whatever the starting day.
  2. Sticky rain. Make rain persist (rain → rain = 0.9). Prediction: long rainy spells, and the stationary share of rain rises substantially.
  3. Absorbing state. Rain → rain = 1. Prediction: once it rains it rains forever, so π puts 100% on rain.

Challenge

For a two-state chain with P(sun → rain) = 0.2 and P(rain → sun) = 0.4, what fraction of days are rainy in the long run?

Reveal the explanation

Balance requires π_sun × 0.2 = π_rain × 0.4, so π_rain = π_sun / 2. Together they sum to 1, so π_rain = 1/3. Set cloudy transitions to 0 to reproduce it.

References

  • Grinstead & Snell, Introduction to Probability, ch. 11 “Markov Chains” (open textbook).

Related simulators

Worked example

Default weather chain (rows are today, columns tomorrow): Sunny 0.6 / 0.3 / 0.1, Cloudy 0.3 / 0.4 / 0.3, Rainy 0.2 / 0.4 / 0.4.

Two days ahead: from a sunny day, P(rain in two days) = 0.6 × 0.1 + 0.3 × 0.3 + 0.1 × 0.4 = 0.19.

Long-run shares: solve π = πP with the shares adding to 1. The balance equations give Cloudy = 11/12 × Sunny and Rainy = 5/8 × Sunny, so π = (24, 22, 15) / 61 ≈ 39% sunny, 36% cloudy, 25% rainy.

Rainy spells: rain stays rain with probability 0.4, so a spell lasts on average 1 / (1 − 0.4) ≈ 1.7 days. With the sticky setting (0.9) it lasts 10 days.

Load the default chain, run 1000 days and compare the visit shares with 39%, 36% and 25%.

Common questions

What is a Markov chain?

A random process where the next state depends only on the current state, not on how it got there. It is described by a table of transition probabilities whose rows each add to 1.

What is a stationary distribution?

The long-run share of time spent in each state. Once the chain is in it, one more step leaves it unchanged: π = πP.

Does every Markov chain settle down?

Not always. A chain that is split into separate parts, or that cycles with a fixed period, may never settle. Chains where every state can reach every other and there is no fixed rhythm always converge to one stationary distribution.

Keyboard shortcuts

Space
Play or pause
S
Step
R
Reset
F
Full screen
?
Show this list