Skip to content
mlmentorship

Markov chains

Stochastic processes where the future depends only on the present, not the past. Foundation of HMMs, MCMC, and many sequence models.

Published · 5 min read ·Core ·Foundation

Visual quick review

Visual first · depth when needed

See probability mass move toward a stationary split, then verify that equal counter-flow leaves that split unchanged.

Preparing the visual…

Summary

A Markov chain is a sequence of random variables such that

The conditional distribution of the future given the present is independent of the past. For finite state spaces, the dynamics are summarized by a transition matrix where .

Markov chains underlie:

  • Hidden Markov models (HMMs) for speech, biology, finance.
  • Markov chain Monte Carlo (MCMC) for Bayesian inference (Metropolis-Hastings, Gibbs).
  • PageRank (random walk on the web graph).
  • N-gram language models.
  • Reinforcement learning (Markov decision processes).
  • Diffusion models (forward and reverse Markov processes over noise levels).

The Markov property is the cleanest assumption that makes long-range stochastic systems tractable.

Stationary distribution

A distribution is stationary for if (treating as a row vector). It’s a left eigenvector of with eigenvalue 1.

For an irreducible (any state reachable from any other) and aperiodic chain, the stationary distribution exists, is unique, and the chain converges to it from any starting state:

For a finite reversible chain, the asymptotic rate of convergence is governed by the second-largest eigenvalue magnitude of (equivalently, its absolute spectral gap from 1 controls mixing).

Detailed balance and reversibility

A chain is reversible if there is a such that

Detailed balance implies is stationary (sum both sides over ). Metropolis-Hastings constructs a chain satisfying detailed balance with respect to a target distribution. This is the trick that makes its long-run samples follow the target.

Learning objective

A Markov chain moves probability mass until one split maps back to itself.

Mass moves toward 2/3 in A and 1/3 in B; at stationarity the two cross-flows balance.

Two-state Markov chain probability mass converges to a stationary distributionFour stacked bars show the marginal distribution moving from all A to point eight A, then point seven two A, then the stationary two-thirds A and one-third B split. A state diagram beside it shows equal detailed-balance flows of two fifteenths in both directions.Repeated transitions move the marginalrow vector update: mu(t+1) = mu(t) Ptimestate A massstate B massmu01.000 Amu10.8000.200mu20.7200.280pi2/3 A1/3 Bpi P = pi: the split stops changingAt stationarity, flows balancetransition matrix P = [[0.8, 0.2], [0.4, 0.6]]ApiA = 2/3BpiB = 1/3A to B: (2/3)(0.2) = 2/15B to A: (1/3)(0.4) = 2/15self-loops keep remaining mass in place
Read it this way: the bars are the distribution before each transition, not rows of the transition matrix. Repeated multiplication moves mass toward the stationary split. At pi, the flow from A to B exactly equals the flow from B to A, so the next step has nothing net to move. Original schematic checked against Levin and Peres and the Berkeley Data 140 textbook.

Common cases in ML

Use caseWhat is the Markov chain
HMMHidden state evolves as Markov chain
MCMCSampler defines a chain with target as stationary
PageRankRandom walk on web graph; = page rank vector
Diffusion modelSequence of noise levels (Gaussian)
MDP / RLState transitions given action
Language modelAutoregressive generation is Markov when the state contains the full retained prefix or context window; tokens alone are not generally first-order Markov

Higher-order chains

A chain where depends on the last states (-th order Markov) can be re-cast as first-order on the state space of -tuples. Trigram language models are 2nd-order Markov over tokens, equivalent to first-order over bigrams.

Common pitfalls

  • Assuming Markov when data has long-range dependence. Often a useful approximation but check by holding out structure.
  • Non-converging MCMC. A chain not yet at stationarity gives biased samples; use multiple chains and convergence diagnostics (, ESS).
  • Confusing transition matrix conventions. Some texts use ; others . Check whether acts on rows or columns.
  • Mistaking the stationary distribution for the marginal of . Marginal at finite depends on initial distribution; stationary is the limit.