MathsDiscrete Math

Markov Chain
calculator.

Type a transition matrix to find where a Markov chain settles, or where it stops.

How it's worked out

A Markov chain moves between a fixed set of states one step at a time. The chance of the next state depends only on the state it is in now. You describe it with a transition matrix P. The entry in row i and column j is the chance of moving from state i to state j in one step, so every row adds up to 1.

After n steps. Grinstead and Snell/11%3A_Markov_Chains/11.01%3A_Introduction) show that entry (i, j) of the matrix power Pⁿ is the chance of being in state j exactly n steps after starting in state i. If u is the starting distribution as a row, the distribution after n steps is u times Pⁿ.

u(n)\displaystyle u^{(n)}=uPn\displaystyle {}= u\,P^{n}

The long run. A stationary distribution π is a row of shares that one more step leaves unchanged. The tool solves these equations directly, with one of them swapped for the rule that the shares add up to 1. It does not just multiply P by itself until the numbers stop moving, because for some chains they never stop.

π\displaystyle \pi=πP\displaystyle {}= \pi P
π1+π2+⋯+πk\displaystyle \pi_1 + \pi_2 + \cdots + \pi_k=1\displaystyle {}= 1

When every entry you type is a decimal, a fraction or a percentage, the same equations are also solved with exact fractions, so a share of 0.4 shows as 2/5. The mean number of steps to come back to state i after leaving it is 1 divided by πᵢ, which section 11.5/11%3A_Markov_Chains/11.05%3A_Mean_First_Passage_Time_for_Ergodic_Chains) calls the mean recurrence time.

Chains that stop. A state is absorbing when the chain can never leave it, so its own entry is 1. Put the other states, the transient ones, first. Q holds the moves among transient states and R holds the moves into absorbing ones. The fundamental matrix/11%3A_Markov_Chains/11.02%3A_Absorbing_Markov_Chains) N counts the expected visits to each transient state. Its row sums give the expected steps before the chain stops, and NR gives the chance of ending in each absorbing state.

N\displaystyle N=(I−Q)−1\displaystyle {}= (I - Q)^{-1}
t\displaystyle t=N𝟏\displaystyle {}= N\,\mathbf{1}
B\displaystyle B=NR\displaystyle {}= N R

Worked examples

Weather in the Land of Oz

This chain comes from the Grinstead and Snell textbook. Oz never has two nice days in a row. After a nice day, rain and snow are equally likely. After rain or snow, the same weather comes back half the time, and the rest splits evenly between the other two.

Starting from a rainy day, the chances two days later are 0.4375 for rain, 0.1875 for a nice day and 0.375 for snow. The book prints this first row of P² as .438, .188 and .375.

π\displaystyle \pi=(25, 15, 25)\displaystyle {}= \left(\frac{2}{5},\ \frac{1}{5},\ \frac{2}{5}\right)

In the long run 2/5 of days are rainy, 1/5 are nice and 2/5 are snowy. By step 6, every row of Pⁿ already reads 0.4, 0.2, 0.4 to three places. Nice days come back every 5 days on average.

Open this example in the tool

Maya models customers switching brands

Maya tracks which of three brands a shopper buys each month, or whether they buy none. Of Brand X buyers, 80% stay with X, 10% move to Y, and 5% each go to Z or to buying nothing. She fills in the other rows the same way from her panel data.

A shopper who buys X this month buys X again three months later with chance 0.584. Over time the shares settle at 40.4% for X, 26.9% for Y, 25% for Z and 7.7% buying nothing. The exact shares are 21/52, 7/26, 1/4, 1/13.

Brand X holds the largest long-run share even though Z keeps more of its own buyers than Y does. X loses fewer buyers each month, 20% against 30% for Y. A non-buyer comes back to buying nothing about once every 13 months.

Open this example in the tool

A drunkard's walk that stops at either end

A walker stands on corner 2 of a street with corners 0 to 4. Each step goes left or right with equal chance. Corner 0 is home and corner 4 is a bar, and the walk stops at either one, so those states are absorbing.

From corner 2 the walk takes 4 steps on average, and it ends at home with chance 0.5. From corner 1 it takes 3 steps on average and ends at home with chance 0.75. These match the values in Grinstead and Snell.

The middle row of N is 1, 2, 1. That says a walk from corner 2 visits corner 2 itself 2 times on average, counting the start, and corners 1 and 3 once each.

Open this example in the tool

Reading your result

Regular chains settle. A chain is regular when some power of P has every entry above 0. Then every row of Pⁿ approaches the same row π, whatever the start. The Land of Oz chain is regular, and its rows match π to three places by step 6.

Periodic chains keep swinging. The period of a state is the greatest common divisor of the step counts on which the chain can be back there. In the chain where A always moves to B, B moves to A or C, and C always moves to B, a start at A gives chance 0.5 of A after 4 steps and 0 after 5. The shares π = (1/4, 1/2, 1/4) still describe how the time splits over many steps, but Pⁿ never converges.

Two closed classes mean no single answer. A closed class is a group of states the chain cannot leave. With two or more of them, the long run depends on where the chain starts, so the tool lists the classes instead of one π. When those classes are single absorbing states, it shows the chance of ending in each.

Short chains settle fast. With two states, a 30% chance of sun turning to rain and a 40% chance of rain turning to sun, a rainy start already gives a 0.556 chance of sun three days later. The long-run share of sun is 0.571.

Common mistakes

  • Rows against columns. This tool reads each row as "from" and each column as "to", so rows add up to 1. Some linear algebra courses write the matrix the other way, with columns adding up to 1. Transpose such a matrix before you type it.
  • Rounding a row to two places. Three entries of 0.33 add up to 0.99, which is not 1, so the row is refused. Type 1/3 instead, or make one entry 0.34.
  • Expecting a periodic chain to settle. π exists for a periodic chain, but the chance of a state after n steps keeps changing with n. Do not read the value at one large n as the long-run share.
  • Leaving in memory of older states. If tomorrow depends on today and yesterday, the states are not enough on their own. Make each state a pair, such as "rain then nice", so the next step depends only on the current pair.

Questions people ask

Write one balance equation per column: each πⱼ equals the sum of πᵢ times the entry in row i, column j. These equations are never independent, so drop one and use π₁ + π₂ + π₃ = 1 in its place. Then solve the three linear equations by substitution or elimination.

Sources