Search

Search papers and lessons

Markov Chain

Level 2 · 12 min read

A Markov chain is a random process that hops between states, where the next state depends only on the current one, not on the path that led there.

On this page

  • Colour = topic mix
  • Probability
  • Linear algebra
  • Stochastic processes
  • Discrete math
  • Sign in to see your progress

TL;DR

A Markov chain is a sequence of random states, one per time step, where the probability of the next state depends only on the state you are in now. The rest of the history is forgotten. Each state has a fixed set of probabilities for where it goes next, collected in a transition matrix.

Run the chain for long enough and, for many chains, the probability of being in each state settles to a fixed pattern called the stationary distribution, no matter where you started.

Where you'll see it: in the forward noising steps of a diffusion model, in the PageRank algorithm that ranks web pages, and in Markov chain Monte Carlo, which samples from distributions too complicated to sample directly.

Intuition

Forgetting the past on purpose

Think of a weather model that only says "sunny" or "rainy". Tomorrow's weather is random, but the odds are not the same every day. If today is sunny, tomorrow is probably sunny too. If today is rainy, the odds tilt the other way.

The Markov idea is that these odds depend only on today. It does not matter that it rained all last week, or that the forecast is unusual this month. Once you know today's state, the past adds nothing to the forecast for tomorrow. This is called the Markov property, and it is a strong simplification. It is also what makes the model tractable: you only need one table of numbers, not a record of every day.

Why do we want this? Many processes look like this at the level we care about. A customer moves between "free", "paying" and "cancelled". A web surfer jumps between pages. A letter in a sentence depends mostly on the letter before it. Writing down the full history would be hopeless, but writing down one step at a time is easy.

0123456780.80.850.90.951n (days from today)probability of sunny
  • P(sunny on day n | sunny today)
  • long-run value 5/6
Starting sunny, the chance of sunny on day n drops quickly toward 5/6 and then stays there. The three marked points are days 0, 1 and 2, with values 1, 0.9 and 0.86.

The math

Definition

Definition: Discrete-time Markov chain

A sequence of random variables X0,X1,X2,…X_0, X_1, X_2, \dots taking values in a finite or countable set SS of states is a Markov chain if, for every time nn and every choice of states i0,…,in−1,i,ji_0, \dots, i_{n-1}, i, j,

P(Xn+1=j∣Xn=i,Xn−1=in−1,…,X0=i0)=P(Xn+1=j∣Xn=i).P(X_{n+1} = j \mid X_n = i, X_{n-1} = i_{n-1}, \dots, X_0 = i_0) = P(X_{n+1} = j \mid X_n = i).

When the right-hand side does not depend on nn, the chain is time-homogeneous, and we write it as PijP_{ij}.

The vertical bar means "given that". The left side conditions on the entire history. The right side conditions only on the present state. That equality is the Markov property. The numbers PijP_{ij} are the transition probabilities: the chance of moving from state ii to state jj in one step. They are conditional probabilities, so for each fixed ii they must add to 1 over all jj.

Collect them into a matrix PP, with entry PijP_{ij} in row ii and column jj. Each row is a probability distribution, so every row sums to 1. Such a matrix is called a row-stochastic matrix.

Distributions as row vectors

Let πn\boldsymbol{\pi}_n be the row vector whose ii-th entry is P(Xn=i)P(X_n = i). Then splitting on the current state with the law of total probability gives

πn+1=πnP.\boldsymbol{\pi}_{n+1} = \boldsymbol{\pi}_n P.

Each step is one matrix multiplication. Starting from π0\boldsymbol{\pi}_0, after nn steps we have πn=π0Pn\boldsymbol{\pi}_n = \boldsymbol{\pi}_0 P^n. The entries of PnP^n are the nn-step transition probabilities.

Stationary distribution

A row vector π\boldsymbol{\pi} is a stationary distribution if it is a probability vector (entries non-negative, summing to 1) and

πP=π.\boldsymbol{\pi} P = \boldsymbol{\pi}.

If the chain starts in π\boldsymbol{\pi}, it stays in π\boldsymbol{\pi} forever. For a finite chain that is irreducible (any state can reach any other) and aperiodic (no rigid cycle in the timing of returns), there is exactly one stationary distribution, and π0Pn\boldsymbol{\pi}_0 P^n converges to it for every starting distribution π0\boldsymbol{\pi}_0. This convergence is what makes Markov chains useful for sampling and for ranking.

To find π\boldsymbol{\pi}, solve the linear system π(P−I)=0\boldsymbol{\pi} (P - I) = 0 together with ∑iπi=1\sum_i \pi_i = 1.

Two-state chains

A two-state chain with P(sunny→rainy)=aP(\text{sunny} \to \text{rainy}) = a and P(rainy→sunny)=bP(\text{rainy} \to \text{sunny}) = b has stationary distribution

πsunny=ba+b,πrainy=aa+b.\pi_{\text{sunny}} = \frac{b}{a + b}, \qquad \pi_{\text{rainy}} = \frac{a}{a + b}.

The stationary probability of a state is larger when it is harder to leave. The formula comes from balancing the flow into and out of each state, which is the same as solving the system above for two states.

Worked examples

Example
A weather chain, by hand

States are sunny (S) and rainy (R). The transition matrix is

P=(0.90.10.50.5),P = \begin{pmatrix} 0.9 & 0.1 \\ 0.5 & 0.5 \end{pmatrix},

so a sunny day stays sunny with probability 0.9, and a rainy day turns sunny with probability 0.5.

  1. Start certain it is sunny: π0=(1,0)\boldsymbol{\pi}_0 = (1, 0).
  2. One step: π1=(1,0)P=(0.9,0.1)\boldsymbol{\pi}_1 = (1, 0) P = (0.9, 0.1). Matches the first row of PP.
  3. Two steps: π2=(0.9,0.1)P=(0.9⋅0.9+0.1⋅0.5, 0.9⋅0.1+0.1⋅0.5)=(0.86,0.14)\boldsymbol{\pi}_2 = (0.9, 0.1) P = (0.9 \cdot 0.9 + 0.1 \cdot 0.5,\ 0.9 \cdot 0.1 + 0.1 \cdot 0.5) = (0.86, 0.14).
  4. Stationary distribution: set πS=0.9πS+0.5πR\pi_S = 0.9 \pi_S + 0.5 \pi_R, which gives 0.1πS=0.5πR0.1 \pi_S = 0.5 \pi_R, so πS=5πR\pi_S = 5 \pi_R. With πS+πR=1\pi_S + \pi_R = 1, we get πR=16\pi_R = \frac{1}{6} and πS=56\pi_S = \frac{5}{6}.
  5. Check: (5/6,1/6)P=(56⋅0.9+16⋅0.5, 56⋅0.1+16⋅0.5)=(56,16)(5/6, 1/6) P = (\frac{5}{6} \cdot 0.9 + \frac{1}{6} \cdot 0.5,\ \frac{5}{6} \cdot 0.1 + \frac{1}{6} \cdot 0.5) = (\frac{5}{6}, \frac{1}{6}). It is unchanged.

Interpretation: the chance of sunny starts at 1, then slides to 0.9 and 0.86, and keeps closing in on 56≈0.833\frac{5}{6} \approx 0.833. In the long run about five days in six are sunny, whatever today was. The plot above shows the same convergence in closed form.

Example
A subscription chain with an absorbing state

A customer is in one of three states each month: Free, Paid, or Cancelled. The transitions are:

FromTo FreeTo PaidTo Cancelled
Free0.60.30.1
Paid00.80.2
Cancelled001

A new customer starts Free, so π0=(1,0,0)\boldsymbol{\pi}_0 = (1, 0, 0).

  1. After month one: π1=(0.6,0.3,0.1)\boldsymbol{\pi}_1 = (0.6, 0.3, 0.1).
  2. After month two, take each entry of π1\boldsymbol{\pi}_1 times the matching column of PP:
    • Free: 0.6⋅0.6=0.360.6 \cdot 0.6 = 0.36 (only Free feeds Free).
    • Paid: 0.6⋅0.3+0.3⋅0.8=0.18+0.24=0.420.6 \cdot 0.3 + 0.3 \cdot 0.8 = 0.18 + 0.24 = 0.42.
    • Cancelled: 0.6⋅0.1+0.3⋅0.2+0.1⋅1=0.06+0.06+0.1=0.220.6 \cdot 0.1 + 0.3 \cdot 0.2 + 0.1 \cdot 1 = 0.06 + 0.06 + 0.1 = 0.22.
  3. Check: 0.36+0.42+0.22=10.36 + 0.42 + 0.22 = 1.

Interpretation: after two months, 22% of customers have already cancelled. Cancelled is absorbing, since once there the chain never leaves. So in the long run every customer ends there, and the stationary distribution is concentrated on Cancelled. This is why the question for a business is usually not the stationary distribution but the expected time until absorption.

Tradeoffs and failure modes

The Markov assumption is powerful and often wrong. Use it when the current state really does carry the information you need.

SituationWhat goes wrongBetter choice
Behaviour depends on a longer historyA one-step chain throws away the useful contextEnlarge the state to include the history, or use a higher-order chain
Transition probabilities change over timeA fixed matrix cannot describe the processUse a time-inhomogeneous chain or a hidden Markov model
The chain has more than one long-run patternThe stationary distribution is not unique, so the answer depends on the startCheck irreducibility, or condition on the starting state
The state space is hugeWriting down PP is impossibleSimulate the chain instead of computing with the matrix (Monte Carlo)
Hidden states drive what you observeYou cannot see the chain's state directlyUse a [[latent-variable-model

A common error is treating a chain as Markov because its current state is easy to write down, when the true dynamics depend on something not in the state. Check whether conditioning on the present really makes the past irrelevant.

History

Andrey Markov, a Russian mathematician, introduced the idea in 1906 while studying sequences of linked random events, partly to argue that independence is not necessary for the law of large numbers. The problem he was solving was how far probability theory could go beyond independent trials. He applied the idea about 1913 to letters in Pushkin's novel Eugene Onegin, counting how often vowels followed consonants.

It caught on because it was simple enough to compute and general enough to model queues, genetics and language. Its biggest modern impact came from simulation. Markov chains underlie the Metropolis algorithm of 1953, and in the 1990s, researchers found they could sample from complicated distributions by running a carefully designed chain, which made Bayesian inference practical for many problems.

Check yourself

QuestionFor a chain with transition matrix P=(0.50.50.20.8)P = \begin{pmatrix} 0.5 & 0.5 \\ 0.2 & 0.8 \end{pmatrix}, what is the probability of being in state 1 after two steps, starting from state 1?Show answer

Start with π0=(1,0)\boldsymbol{\pi}_0 = (1, 0). After one step, π1=(0.5,0.5)\boldsymbol{\pi}_1 = (0.5, 0.5). After two steps, the first entry is 0.5⋅0.5+0.5⋅0.2=0.25+0.10=0.350.5 \cdot 0.5 + 0.5 \cdot 0.2 = 0.25 + 0.10 = 0.35. The answer is 0.350.35.

QuestionUsing the same matrix as before, find the stationary distribution.Show answer

Solve π1=0.5π1+0.2π2\pi_1 = 0.5 \pi_1 + 0.2 \pi_2, which gives 0.5π1=0.2π20.5 \pi_1 = 0.2 \pi_2, so π2=2.5π1\pi_2 = 2.5 \pi_1. With π1+π2=1\pi_1 + \pi_2 = 1 we get π1=13.5=27\pi_1 = \frac{1}{3.5} = \frac{2}{7} and π2=57\pi_2 = \frac{5}{7}. Check: the two-state formula gives π1=ba+b=0.20.5+0.2=27\pi_1 = \frac{b}{a + b} = \frac{0.2}{0.5 + 0.2} = \frac{2}{7}. The answer is (27,57)(\frac{2}{7}, \frac{5}{7}).

QuestionWhy does the Markov property make a model easier to store and compute with, and what does it cost in realism?Show answer

Under the Markov property, the full history never needs to be stored. The whole model is one transition matrix whose size depends on the number of states, not on how long the process has run. The cost is that the model assumes the present state holds all relevant information. If the true process depends on a longer history, the chain will miss patterns that a richer state would capture.

QuestionA chain reaches a state from which it can never leave. What does that do to the stationary distribution, and why?Show answer

That state is absorbing. Once the chain is there, it stays forever, so over time all probability flows into it. The stationary distribution puts all its mass on the absorbing state, and the other states get zero. Uniqueness can also fail, because each absorbing state gives its own stationary distribution, which is why the irreducibility condition matters.

QuestionIn a diffusion model, the forward process adds a little Gaussian noise at each step, and each step depends only on the previous image. Why is that a Markov chain, and why does the design choice matter?Show answer

Each noisy image depends only on the image one step before it, not on the earlier history, so the sequence has the Markov property. This matters because the reverse process can then be learned one step at a time, and the forward chain has a closed form for jumping to any step, which keeps training fast. The Markov structure is what turns a long noising procedure into a sequence of simple, learnable steps.

Sources