Technopedia

The technologist’s encyclopedia

Markov chains

A Markov chain is a stochastic process with the Markov property: given the present state, the future is independent of the past. It is the workhorse model for any system whose next state is a probabilistic function of the current one only — queues, web search ranking, genetic sequences, weather, MCMC sampling, and reinforcement learning.

When it is relevant. Use a Markov chain whenever you can (or want to assume) that the history of a process is summarized by its current state. If the next step depends on how long the system has been in its current state, a plain Markov chain is not enough and you must enlarge the state space (or use a semi-Markov / continuous-time model).

Terminology, precisely. “Markov chain” is usually reserved for discrete time t=0,1,2,…t = 0, 1, 2, \dots . The continuous-time version is a continuous-time Markov chain (see Poisson processes, whose exponential clocks drive the jumps). The state space SS may be discrete or continuous:

Everything below is for the discrete-time, discrete-state case, where the matrix PP is the central object.

Builds on Random variables and expectation and Conditional probability; hitting-time estimates are a martingale application.

Brief history

1. The Markov property and definition

Definition. A stochastic process (Xt)t≥0(X_t)_{t \ge 0} on a state space SS is a Markov chain if, for all states x0,…,xt−1,i,j∈Sx_0, \dots, x_{t-1}, i, j \in S ,

P(Xt+1=j∣Xt=i,Xt−1=xt−1,…,X0=x0)=P(Xt+1=j∣Xt=i).\mathbb{P}(X_{t+1} = j \mid X_t = i, X_{t-1} = x_{t-1}, \dots, X_0 = x_0) = \mathbb{P}(X_{t+1} = j \mid X_t = i).

The whole future is pinned down by the present state ii . By the chain rule of conditional probability, the joint law of a whole trajectory factorizes:

P(X0=x0,X1=x1,…,Xn=xn)=P(X0=x0) ∏k=1nP(Xk=xk∣Xk−1=xk−1).\mathbb{P}(X_0 = x_0, X_1 = x_1, \dots, X_n = x_n) = \mathbb{P}(X_0 = x_0)\, \prod_{k=1}^{n} \mathbb{P}(X_k = x_k \mid X_{k-1} = x_{k-1}).

This is the single most useful identity: a long, complicated path probability collapses into one initial probability times a product of one-step probabilities.

Details

Why the property is powerful. Without the Markov property, the probability of a path of length nn would require conditioning on the entire history (x0,…,xn−1)(x_0, \dots, x_{n-1}) — an object that grows with nn . The Markov property says that conditioning on the history is redundant: the last state xn−1x_{n-1} already carries all the information the future needs. That is what makes chains computable and simulable.

Concrete example: a weather chain. Let S={Sunny,Rainy}S = \{\text{Sunny}, \text{Rainy}\} . Suppose

The Markov assumption is that “tomorrow’s weather depends on today’s weather, not on the whole week so far.” The probability of the specific 3-day path Sunny → Sunny → Rainy is

P(Sunny)⋅0.8⋅0.2,\mathbb{P}(\text{Sunny}) \cdot 0.8 \cdot 0.2,

one initial probability times two one-step factors — no need to remember the day before.

The simulation above runs a 4-state chain. Each Step applies one transition; the bar chart tracks how often each state has been visited, and you can watch those empirical frequencies settle onto the theoretical stationary distribution π\pi (Section 3).

Authoritative sources.

Authoritative sources

Example problem.

Problem

A chain has states {A,B}\{A, B\} with P(A→B)=0.3\mathbb{P}(A \to B) = 0.3 , P(B→A)=0.4\mathbb{P}(B \to A) = 0.4 . Starting from AA , compute P(X0=A,X1=B,X2=A)\mathbb{P}(X_0 = A, X_1 = B, X_2 = A) .

Solution

Apply the factorization:

P(A)⋅P(B∣A)⋅P(A∣B)=1⋅0.3⋅0.4=0.12.\mathbb{P}(A) \cdot \mathbb{P}(B \mid A) \cdot \mathbb{P}(A \mid B) = 1 \cdot 0.3 \cdot 0.4 = 0.12.

The initial state is fixed at AA (probability 11 ), so only the two one-step factors matter.

2. Transition matrix and Chapman–Kolmogorov equations

Definition. For a discrete state space S={1,…,n}S = \{1, \dots, n\} , the transition matrix PP has entries

Pij=P(Xt+1=j∣Xt=i),P_{ij} = \mathbb{P}(X_{t+1} = j \mid X_t = i),

so each row sums to 11 (PP is row-stochastic). The nn -step transition probabilities are the matrix power PnP^{n} :

P(Xt+n=j∣Xt=i)=(Pn)ij.\mathbb{P}(X_{t+n} = j \mid X_t = i) = (P^{n})_{ij}.

The Chapman–Kolmogorov equations say that an (m+n)(m+n) -step move equals an mm -step move followed by an nn -step move, summed over the intermediate state:

Pij(m+n)=∑k∈SPik(m) Pkj(n).P^{(m+n)}_{ij} = \sum_{k \in S} P^{(m)}_{ik}\, P^{(n)}_{kj}.
Details

Reading the matrix. PijP_{ij} is the probability of leaving ii and arriving at jj in one step. Because from state ii you must go somewhere, ∑jPij=1\sum_j P_{ij} = 1 . A diagonal entry PiiP_{ii} is the probability of staying put.

Why PnP^n works. The (i,j)(i, j) entry of P2P^2 is ∑kPikPkj\sum_k P_{ik} P_{kj} : go from ii to some intermediate kk , then from kk to jj . This is exactly the Chapman–Kolmogorov equation with m=n=1m = n = 1 , and it is just the chain rule of conditional probability plus the law of total probability. Matrix multiplication is doing the “sum over the forgotten intermediate state” for you.

Concrete example. Take the weather chain of Section 1, with S={Sunny,Rainy}S = \{\text{Sunny}, \text{Rainy}\} and

P=(0.80.20.50.5).P = \begin{pmatrix} 0.8 & 0.2 \\ 0.5 & 0.5 \end{pmatrix}.

The probability of being Rainy two days from now, given it is Sunny today, is (P2)12(P^2)_{12} :

(P2)12=P11P12+P12P22=0.8⋅0.2+0.2⋅0.5=0.21.(P^2)_{12} = P_{11}P_{12} + P_{12}P_{22} = 0.8 \cdot 0.2 + 0.2 \cdot 0.5 = 0.21.

The intermediate day is Sunny with probability 0.80.8 (contributing 0.8⋅0.20.8 \cdot 0.2 ) or Rainy with probability 0.20.2 (contributing 0.2⋅0.50.2 \cdot 0.5 ).

Authoritative sources.

Authoritative sources

Example problem.

Problem

With the weather matrix above, compute the full matrix P2P^2 .

Solution P2=(0.80.20.50.5)2=(0.8⋅0.8+0.2⋅0.50.8⋅0.2+0.2⋅0.50.5⋅0.8+0.5⋅0.50.5⋅0.2+0.5⋅0.5)=(0.740.260.650.35).P^2 = \begin{pmatrix} 0.8 & 0.2 \\ 0.5 & 0.5 \end{pmatrix}^2 = \begin{pmatrix} 0.8\cdot0.8 + 0.2\cdot0.5 & 0.8\cdot0.2 + 0.2\cdot0.5 \\ 0.5\cdot0.8 + 0.5\cdot0.5 & 0.5\cdot0.2 + 0.5\cdot0.5 \end{pmatrix} = \begin{pmatrix} 0.74 & 0.26 \\ 0.65 & 0.35 \end{pmatrix}.

Each row still sums to 11 — P2P^2 is also a transition matrix, as it must be.

3. Stationary distributions and ergodicity

Definition. A probability vector π\pi (so ∑iπi=1\sum_i \pi_i = 1 , πi≥0\pi_i \ge 0 ) is a stationary distribution if

πP=π.\pi P = \pi.

If the chain is irreducible and aperiodic (Section 4), then π\pi exists, is unique, and the chain converges to it from any starting state:

lim⁡n→∞Pn=1 π,\lim_{n \to \infty} P^{n} = \mathbf{1}\, \pi,

i.e. every row of PnP^n tends to π\pi . This is the ergodic theorem for finite Markov chains.

Details

What “stationary” means. If the chain starts in π\pi — that is, P(X0=i)=πi\mathbb{P}(X_0 = i) = \pi_i — then it stays in π\pi forever: P(Xt+1=j)=∑iπiPij=(πP)j=πj\mathbb{P}(X_{t+1} = j) = \sum_i \pi_i P_{ij} = (\pi P)_j = \pi_j . The distribution is a fixed point of the update rule.

Existence and uniqueness. For a finite chain, irreducibility (every state reachable from every other) guarantees that a stationary distribution exists and is unique. Aperiodicity (no deterministic “clock” forcing returns to a fixed rhythm) guarantees that the chain actually converges to it rather than oscillating. The rate of convergence is governed by the second-largest eigenvalue of PP in magnitude — the Perron–Frobenius theory.

Concrete example. For the weather chain, solve πP=π\pi P = \pi with π1+π2=1\pi_1 + \pi_2 = 1 :

π1=0.8 π1+0.5 π2,π2=0.2 π1+0.5 π2.\pi_1 = 0.8\,\pi_1 + 0.5\,\pi_2, \qquad \pi_2 = 0.2\,\pi_1 + 0.5\,\pi_2.

From the first equation, 0.2 π1=0.5 π20.2\,\pi_1 = 0.5\,\pi_2 , so π1=2.5 π2\pi_1 = 2.5\,\pi_2 . With π1+π2=1\pi_1 + \pi_2 = 1 this gives π2=1/3.5≈0.286\pi_2 = 1/3.5 \approx 0.286 and π1≈0.714\pi_1 \approx 0.714 . So in the long run it is Sunny about 71%71\% of the time. You can verify in the simulation that the empirical bars settle near the theoretical π\pi .

Authoritative sources.

Authoritative sources

Example problem.

Problem

A 3-state chain has transition matrix

P=(0.50.5000.50.50.500.5).P = \begin{pmatrix} 0.5 & 0.5 & 0 \\ 0 & 0.5 & 0.5 \\ 0.5 & 0 & 0.5 \end{pmatrix}.

Find the stationary distribution π\pi .

Solution

Solve πP=π\pi P = \pi with π1+π2+π3=1\pi_1 + \pi_2 + \pi_3 = 1 :

  • π1=0.5 π1+0.5 π3⇒π1=π3\pi_1 = 0.5\,\pi_1 + 0.5\,\pi_3 \Rightarrow \pi_1 = \pi_3
  • π2=0.5 π1+0.5 π2⇒π2=π1\pi_2 = 0.5\,\pi_1 + 0.5\,\pi_2 \Rightarrow \pi_2 = \pi_1
  • π3=0.5 π2+0.5 π3⇒π3=π2\pi_3 = 0.5\,\pi_2 + 0.5\,\pi_3 \Rightarrow \pi_3 = \pi_2

All three are equal, so π1=π2=π3=1/3\pi_1 = \pi_2 = \pi_3 = 1/3 .

4. Classification of states

Definition. For a state ii , let fif_i be the probability that the chain, starting from ii , ever returns to ii .

Details

Recurrent vs transient, intuitively. A recurrent state is one you are doomed to revisit; a transient state is one you may visit a few times and then abandon forever. In a finite chain, irreducibility forces every state to be recurrent (you cannot “escape to infinity” when there are only finitely many states). Transience only appears in infinite chains, e.g. the simple random walk on Z3\mathbb{Z}^3 is transient.

The return-probability criterion. The cleanest test is fif_i : compute the probability of ever returning to ii after leaving it. If it is 11 , the state is recurrent; otherwise transient. Equivalently, a state is recurrent iff the expected number of visits to it (starting from itself) is infinite.

Periodicity, intuitively. A state with period d>1d > 1 can only be revisited at times that are multiples of dd — the chain is trapped on a dd -cycle. Example: a chain that alternates deterministically between two states has period 22 . Aperiodicity (d=1d = 1 ) is what lets the chain “forget its phase” and converge to a single stationary distribution; a periodic chain instead oscillates. A single self-loop (Pii>0P_{ii} > 0 ) is the easiest way to guarantee aperiodicity.

Concrete example. Consider a 3-state chain where 1→21 \to 2 and 2→12 \to 1 happen with probability 11 , and state 33 is reachable from 11 but never returns. Then 33 is transient (once you leave 33 you never come back, and you can even fail to reach it), while {1,2}\{1, 2\} form a closed, period-22 class.

Authoritative sources.

Authoritative sources

Example problem.

Problem

A chain has states {1,2,3}\{1, 2, 3\} with P12=1P_{12} = 1 , P21=1P_{21} = 1 , P31=0.5P_{31} = 0.5 , P33=0.5P_{33} = 0.5 . Classify each state and give the period of state 11 .

Solution

From 33 you can jump to 11 (and then wander in {1,2}\{1,2\} ) or stay at 33 ; but from {1,2}\{1,2\} you can never reach 33 . So 33 is transient (you may leave it and never return), while 11 and 22 are recurrent (closed class). State 11 is revisited only at even times (1→2→1→…1 \to 2 \to 1 \to \dots ), so its period is 22 .

5. Hitting times and expected hitting times

Definition. For a set of states A⊆SA \subseteq S , the hitting time is TA=inf⁡{t≥0:Xt∈A}T_A = \inf\{ t \ge 0 : X_t \in A \} . The expected hitting time from state ii is hi=Ei[TA]h_i = \mathbb{E}_i[T_A] . It satisfies the linear system

hi=0 (i∈A),hi=1+∑j∈SPij hj (i∉A).h_i = 0 \ (i \in A), \qquad h_i = 1 + \sum_{j \in S} P_{ij}\, h_j \ (i \notin A).
Details

Why the linear system. If i∈Ai \in A you have already arrived, so hi=0h_i = 0 . If i∉Ai \notin A , you spend one step (the leading 11 ) and then, having moved to jj with probability PijP_{ij} , still need hjh_j more steps on average. Taking expectation over the next state gives hi=1+∑jPijhjh_i = 1 + \sum_j P_{ij} h_j . This is a system of linear equations in the unknowns hih_i for i∉Ai \notin A , and it has a unique solution whenever AA is reachable from every state.

Gambler’s ruin — the canonical example. A gambler with ii dollars bets 11 dollar at a time, winning with probability pp and losing with probability q=1−pq = 1 - p , until reaching 00 (broke) or NN (goal). The states are {0,1,…,N}\{0, 1, \dots, N\} with Pi,i+1=pP_{i,i+1} = p , Pi,i−1=qP_{i,i-1} = q . The expected duration hih_i solves hi=1+p hi+1+q hi−1h_i = 1 + p\,h_{i+1} + q\,h_{i-1} with h0=hN=0h_0 = h_N = 0 . For the fair game p=q=1/2p = q = 1/2 the solution is the parabola hi=i(N−i)h_i = i(N - i) .

Connection to martingales. The fair gambler’s fortune is a martingale; the optional stopping theorem gives the probability of reaching NN before 00 in one line, while the hitting-time duration above comes from the squared fortune, which is a submartingale.

Authoritative sources.

Authoritative sources

Example problem.

Problem

A fair gambler starts with 22 dollars and plays until reaching 00 or 44 . What is the expected number of bets?

Solution

Here N=4N = 4 , i=2i = 2 , and the fair-game formula gives hi=i(N−i)=2⋅(4−2)=4h_i = i(N - i) = 2 \cdot (4 - 2) = 4 bets on average.

6. Applications

Definition. Markov chains are the engine behind a wide range of models and algorithms; the unifying idea is always “the next state is a probabilistic function of the current one.”

Details

Authoritative sources.

Authoritative sources

Quizz

Try it yourself

A 3-state chain has transition matrix

P=(0.50.500.20.40.400.50.5).P = \begin{pmatrix} 0.5 & 0.5 & 0 \\ 0.2 & 0.4 & 0.4 \\ 0 & 0.5 & 0.5 \end{pmatrix}.

(a) Find the stationary distribution π\pi . (b) Starting from state 11 , find the expected hitting time of state 33 .

Solution

(a) Solve πP=π\pi P = \pi with ∑πi=1\sum \pi_i = 1 :

  • π1=0.5 π1+0.2 π2⇒0.5 π1=0.2 π2⇒π2=2.5 π1\pi_1 = 0.5\,\pi_1 + 0.2\,\pi_2 \Rightarrow 0.5\,\pi_1 = 0.2\,\pi_2 \Rightarrow \pi_2 = 2.5\,\pi_1
  • π3=0.4 π2+0.5 π3⇒0.5 π3=0.4 π2⇒π3=0.8 π2=2 π1\pi_3 = 0.4\,\pi_2 + 0.5\,\pi_3 \Rightarrow 0.5\,\pi_3 = 0.4\,\pi_2 \Rightarrow \pi_3 = 0.8\,\pi_2 = 2\,\pi_1

So π1+2.5 π1+2 π1=1⇒5.5 π1=1⇒π1=2/11\pi_1 + 2.5\,\pi_1 + 2\,\pi_1 = 1 \Rightarrow 5.5\,\pi_1 = 1 \Rightarrow \pi_1 = 2/11 , π2=5/11\pi_2 = 5/11 , π3=4/11\pi_3 = 4/11 .

(b) Let hi=Ei[T3]h_i = \mathbb{E}_i[T_3] . Then h3=0h_3 = 0 and

h1=1+0.5 h1+0.5 h2,h2=1+0.2 h1+0.4 h2+0.4⋅0.h_1 = 1 + 0.5\,h_1 + 0.5\,h_2, \qquad h_2 = 1 + 0.2\,h_1 + 0.4\,h_2 + 0.4\cdot 0.

From the first: 0.5 h1=1+0.5 h2⇒h1=2+h20.5\,h_1 = 1 + 0.5\,h_2 \Rightarrow h_1 = 2 + h_2 . From the second: 0.6 h2=1+0.2 h1⇒h2=53+13h10.6\,h_2 = 1 + 0.2\,h_1 \Rightarrow h_2 = \tfrac{5}{3} + \tfrac{1}{3}h_1 . Substituting: h1=2+53+13h1⇒23h1=113⇒h1=5.5h_1 = 2 + \tfrac{5}{3} + \tfrac{1}{3}h_1 \Rightarrow \tfrac{2}{3}h_1 = \tfrac{11}{3} \Rightarrow h_1 = 5.5 steps.