Part 2Math for LLMs

Markov Chains: Part 2 - Common Mistakes To Appendix Y Connections To Physics

Probability Theory / Markov Chains

Private notes
0/8000

Notes stay private to your browser until account sync is configured.

Part 2
28 min read18 headingsSplit lesson page

Lesson overview | Previous part | Next part

Markov Chains: Part 10: Common Mistakes to Appendix Y: Connections to Physics

10. Common Mistakes

#MistakeWhy It's WrongFix
1Confusing stationarity with convergenceStationarity (πP=π\pi P = \pi) is a property of a distribution; convergence (μ(n)π\mu^{(n)} \to \pi) is about the chain's trajectory. The chain may start at π\pi and be stationary without "converging" (it's already there).Distinguish: stationarity is the destination; convergence is the journey.
2Assuming irreducibility implies unique stationary distributionAn irreducible chain on an infinite state space may be null-recurrent (no normalisable stationary distribution), e.g., SRW on Z\mathbb{Z}.Unique stationary distribution requires irreducibility + positive recurrence.
3Forgetting aperiodicity for convergenceAn irreducible positive-recurrent but periodic chain has a unique stationary distribution but Pij(n)P^{(n)}_{ij} does NOT converge - it oscillates.Convergence to stationarity requires ergodicity = irreducible + positive recurrent + aperiodic.
4Treating detailed balance as necessary for stationarityDetailed balance (πiPij=πjPji\pi_i P_{ij} = \pi_j P_{ji}) is sufficient but not necessary for πP=π\pi P = \pi. Many chains (e.g., cyclic chains) have stationary distributions without satisfying detailed balance.Detailed balance \Rightarrow stationarity, but stationarity ⇏\not\Rightarrow detailed balance.
5Confusing the transition matrix orientationSome books write PijP_{ij} as probability from jj to ii (column stochastic); others write it as probability from ii to jj (row stochastic). This flips πP=π\pi P = \pi vs. Pπ=πP\pi = \pi.Fix a convention: row stochastic means rows sum to 1 and πP=π\pi P = \pi (left eigenvector).
6Ignoring burn-in in MCMCThe first BB MCMC samples come from a distribution close to μ(0)\mu^{(0)} (initial distribution), not π\pi. Including them biases posterior estimates.Always discard a burn-in period. Use R^\hat{R} and ESS to assess convergence.
7Confusing mixing time with burn-inMixing time tmixt_{\text{mix}} is a property of the chain (how long until the worst-case distribution is close to π\pi). Burn-in is a practical heuristic. They are related but not equal.Burn-in should be at least tmixt_{\text{mix}}; more if starting far from π\pi.
8Assuming high acceptance rate = well-mixed MCMCA chain that always accepts (tiny step size in MH) explores very slowly; acceptance rate ~1 with step size -> 0 gives high acceptance but poor mixing.Target ~23% acceptance in high dimensions (optimal for Gaussian targets).
9Using PnP^n to compute stationary distribution for large nnMatrix exponentiation PnP^n converges to the rank-1 matrix 1π\mathbf{1}\pi, but floating-point errors accumulate.Use power iteration on the distribution vector: π(k+1)=π(k)P\pi^{(k+1)} = \pi^{(k)} P, which is numerically stable.
10Misidentifying the state space for LLM generationA bigram LM has states = vocabulary (size VV). A full autoregressive LM has states = entire context = exponential in context length.The Markov chain for a transformer is on the context window, not the vocabulary alone.
11Confusing transient and absorbingA transient state will eventually be left for good; an absorbing state is one from which the chain never leaves. Every absorbing state is recurrent (trivially), not transient.Transient: P(return)<1P(\text{return}) < 1; absorbing: Pii=1P_{ii}=1. Absorbing states are recurrent.
12Expecting MCMC samples to be independentMCMC samples are correlated (autocorrelated) - consecutive samples are not independent. ESS < T measures effective independence.Report ESS, not raw sample count. Use thinning to reduce autocorrelation if needed.

11. Exercises

Exercise 1 * - Transition Matrix and Distribution Evolution

A weather model has two states: Sunny (1) and Rainy (2), with transition matrix P=(0.80.20.40.6)P = \begin{pmatrix}0.8 & 0.2 \\ 0.4 & 0.6\end{pmatrix}.

(a) Starting from μ(0)=(1,0)\mu^{(0)} = (1, 0) (certainly sunny), compute μ(1),μ(5),μ(20)\mu^{(1)}, \mu^{(5)}, \mu^{(20)}.

(b) Compute the 3-step transition matrix P3P^3. What is P(X3=2X0=1)P(X_3=2 \mid X_0=1)?

(c) Find the stationary distribution π\pi by solving πP=π\pi P = \pi with π1+π2=1\pi_1 + \pi_2 = 1.

(d) Verify that μ(n)π\mu^{(n)} \to \pi as nn \to \infty by computing μ(n)πTV\|\mu^{(n)} - \pi\|_{\text{TV}} for n=1,5,10,20n = 1, 5, 10, 20.


Exercise 2 * - State Classification

Consider the Markov chain on {1,2,3,4,5}\{1,2,3,4,5\} with transition matrix:

P=(010000.500.500000100000100010)P = \begin{pmatrix} 0 & 1 & 0 & 0 & 0 \\ 0.5 & 0 & 0.5 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 1 \\ 0 & 0 & 0 & 1 & 0 \end{pmatrix}

(a) Draw the transition diagram. Identify all communicating classes.

(b) Classify each state as recurrent or transient. Justify using the return probability criterion.

(c) For the recurrent states, determine their period. Are any states aperiodic?

(d) Starting from state 1, what is the probability of eventually reaching state 4?


Exercise 3 * - Computing Stationary Distributions

(a) Implement stationary_power_iteration(P, n_iter=1000) that starts from a uniform distribution and returns π(n)\pi^{(n)} after nn iterations. Apply to the 3×33\times3 chain with Pij=1/3P_{ij} = 1/3 for all i,ji,j and a 4×44\times4 random row-stochastic matrix.

(b) Verify your result satisfies πP=π\pi P = \pi to tolerance 10610^{-6}.

(c) For a doubly-stochastic matrix (rows AND columns sum to 1), prove the stationary distribution is uniform without solving any equations. Verify numerically.

(d) For the birth-death chain with pi=0.6p_i = 0.6, qi=0.4q_i = 0.4 on {0,1,2,3,4}\{0,1,2,3,4\} with reflecting barriers, compute π\pi analytically and verify against power iteration.


Exercise 4 ** - Detailed Balance and Birth-Death Chains

(a) Verify that the two-state chain P=(1ppq1q)P = \begin{pmatrix}1-p&p\\q&1-q\end{pmatrix} satisfies detailed balance with π=(q/(p+q),p/(p+q))\pi = (q/(p+q), p/(p+q)).

(b) Implement a M/M/1M/M/1 queue birth-death chain with λ=0.6,μ=1.0\lambda=0.6, \mu=1.0 on states {0,1,,20}\{0,1,\ldots,20\} with an absorbing boundary at 20. Compute the stationary distribution and verify πn(1ρ)ρn\pi_n \approx (1-\rho)\rho^n with ρ=0.6\rho = 0.6.

(c) For the cyclic chain 12311\to2\to3\to1 with P12=P23=P31=1P_{12}=P_{23}=P_{31}=1, find the stationary distribution. Does it satisfy detailed balance? Explain why not.

(d) Show that if PP is doubly stochastic, then detailed balance holds with the uniform distribution iff PP is also symmetric.


Exercise 5 ** - Mixing Time and Spectral Gap

(a) For the two-state chain in Exercise 1, compute the eigenvalues of PP. What is the spectral gap?

(b) Using the spectral gap bound, upper-bound the mixing time tmix(0.01)t_{\text{mix}}(0.01).

(c) Compute the TV distance Pt(1,)πTV\|P^t(1,\cdot) - \pi\|_{\text{TV}} for t=1,2,5,10,20t = 1, 2, 5, 10, 20 and plot the convergence. At what tt does it drop below 0.010.01?

(d) For the lazy version P=(I+P)/2P' = (I+P)/2, redo parts (a)-(c). How does the spectral gap change? How does the mixing time change?


Exercise 6 ** - Metropolis-Hastings

(a) Implement Metropolis-Hastings with a Gaussian random walk proposal q(θθ)=N(θ,σ2)q(\theta'|\theta) = \mathcal{N}(\theta, \sigma^2) for target π(θ)exp(θ4/4+θ2/2)\pi(\theta) \propto \exp(-\theta^4/4 + \theta^2/2) (double-well potential). Run for T=50000T=50000 steps with σ=0.5\sigma=0.5.

(b) Verify your sampler satisfies detailed balance: for two states θ1,θ2\theta_1, \theta_2, check π(θ1)K(θ1,θ2)π(θ2)K(θ2,θ1)\pi(\theta_1)\,K(\theta_1,\theta_2) \approx \pi(\theta_2)\,K(\theta_2,\theta_1) numerically where KK is the MH transition kernel.

(c) Compute the acceptance rate and effective sample size (ESS). How does ESS change as σ\sigma varies over {0.1,0.5,1.0,2.0,5.0}\{0.1, 0.5, 1.0, 2.0, 5.0\}?

(d) Estimate Eπ[θ2]\mathbb{E}_\pi[\theta^2] from your samples. Compare to the true value (computed numerically).


Exercise 7 *** - PageRank

(a) Implement PageRank power iteration for the 5-node graph with adjacency matrix:

A=(0110000110000111000101000)A = \begin{pmatrix}0&1&1&0&0\\0&0&1&1&0\\0&0&0&1&1\\1&0&0&0&1\\0&1&0&0&0\end{pmatrix}

with damping factor α=0.85\alpha=0.85. Handle dangling nodes.

(b) Verify that your PageRank vector satisfies πP=π\pi P = \pi to tolerance 10810^{-8}.

(c) Add a new node 6 that links to all existing nodes, and all existing nodes link to node 6. How does the PageRank of node 1 change? Why?

(d) Implement personalised PageRank where the teleportation vector is non-uniform: (0.4,0.2,0.1,0.1,0.1,0.1)(0.4, 0.2, 0.1, 0.1, 0.1, 0.1). Compare to standard PageRank.


Exercise 8 *** - HMM Forward Algorithm and Viterbi

An HMM models DNA sequences with 2 hidden states (CpG island: HH, non-CpG: LL) and 4 observations (A, C, G, T).

(a) Given transition matrix THH=0.5,THL=0.5,TLH=0.4,TLL=0.6T_{HH}=0.5, T_{HL}=0.5, T_{LH}=0.4, T_{LL}=0.6 and emission matrices (HMM notebook provides values), implement the forward algorithm and compute P(X1:5=ACGTT)P(X_{1:5} = \text{ACGTT}) under equal initial probabilities.

(b) Implement the Viterbi algorithm to find the most likely hidden state sequence for the same observation.

(c) Verify the forward probabilities satisfy iαt(i)=P(X1:t)\sum_i \alpha_t(i) = P(X_{1:t}) at each tt.

(d) Compare Viterbi decoding to the most-probable-state-per-position (posterior decoding using the backward algorithm). When do they differ?


12. Why This Matters for AI (2026 Perspective)

ConceptAI/ML ApplicationImpact
Transition matrix / stationary dist.PageRank web graph; LLM token distributionFoundation for all sequential reasoning
Ergodic theoremMCMC posterior estimation; time-averaging in RLJustifies replacing expectation with time average
Perron-FrobeniusPageRank convergence proof; convergence of power iterationGuarantees unique ranking vectors exist
Detailed balance / reversibilityCorrectness of MH, Gibbs, SGLDAll Bayesian MCMC algorithms rely on this
Spectral gap / mixing timeMCMC convergence rate; warm-start certificationExplains why MCMC works (or fails) in practice
Metropolis-HastingsBayesian neural network posteriors; sampling EBMsFoundation of probabilistic inference in ML
Gibbs samplingLDA topic models; Boltzmann machine inferenceTractable posterior sampling via conditionals
HMC / NUTSStan, PyMC, posterior sampling in Bayesian DLGold standard for Bayesian inference on Rd\mathbb{R}^d
MDP / Bellman equationRL algorithms (PPO, SAC, DQN, RLHF)Every RL agent solves a Markov chain problem
HMM forward-backwardSpeech recognition, gene prediction, legacy NLPEfficient inference on latent sequence models
CTMC / generator matrixLLM serving queues; continuous-time RLLatency modelling and event-driven simulation
Diffusion as CTMCDDPM/DDIM forward process; score-matchingConnects generative modelling to Markov chain theory
Power iterationPageRank; dominant eigenvector computationO(N2)O(N^2) algorithm that scales to trillion-node graphs
Lazy chains / warm startsMCMC initialisation strategyReduces burn-in and improves sample efficiency

13. Conceptual Bridge

Markov chains occupy the central position in the probability curriculum: they are the first class of stochastic processes with nontrivial structure - neither completely independent (iid) nor completely dependent. The Markov property gives just enough memory to model real sequential phenomena while remaining mathematically tractable.

Looking backward: Markov chains build on everything in Chapters 1-6. The transition matrix is a row-stochastic matrix - linear algebra from Chapters 2-3. The stationary distribution is an eigenvector problem solved by Perron-Frobenius. Computing stationary distributions uses the conditional probability machinery from Section03. The ergodic theorem is a strengthening of the law of large numbers (Section06). The Metropolis-Hastings acceptance ratio uses Bayes' theorem (Section03). Mixing times use concentration inequalities (Section05) through the spectral gap. The stochastic process framework of filtrations and stopping times (Section06) provides the rigorous foundation.

Looking forward: Markov chains are the gateway to Chapter 7 (Statistics), where MCMC enables Bayesian inference with intractable posteriors; Chapter 8 (Optimisation), where Markov chain mixing theory explains why SGD finds flat minima; and Chapter 9 (Information Theory), where the entropy of a stationary distribution connects to compression. Markov Decision Processes - the reinforcement learning setting - are a direct extension requiring optimisation over policies.

The deep connection: The central theorem of Markov chain theory - ergodicity implies μ(n)π\mu^{(n)} \to \pi - is the probabilistic analogue of the contraction mapping theorem from analysis: repeated application of the transition operator contracts all distributions toward the unique fixed point π\pi. The spectral gap quantifies the contraction rate, just as the Lipschitz constant does in the deterministic setting. This structural parallel runs through SGD convergence theory, where the gradient operator acts as a stochastic contraction.

MARKOV CHAINS IN THE CURRICULUM
========================================================================

  Section01 Random Variables -------------------------------------+
  Section02 Distributions --------------------------------------+  |
  Section03 Joint Distributions  ---------------------------+   |  |
  Section04 Expectation & Moments ----------------------+   |   |  |
  Section05 Concentration Inequalities -------------+   |   |   |  |
  Section06 Stochastic Processes ---------------+   |   |   |   |  |
                                           v   v   v   v  v  v
                              +---------------------------------+
                              |   Section07  MARKOV  CHAINS           |
                              |                                 |
                              |  Markov property                |
                              |  Transition matrices            |
                              |  Stationary distributions       |
                              |  Mixing times, spectral gap     |
                              |  MCMC (MH, Gibbs, HMC)         |
                              |  PageRank, MDP, HMM             |
                              +--------------+------------------+
                                             |
               +-----------------------------+--------------------+
               v                             v                    v
    Ch7: Statistics               Ch8: Optimisation        Ch9: Info Theory
    (MCMC for Bayes,              (SGD mixing theory,      (Entropy of \\pi,
     posterior inference)          Langevin dynamics)       KL from stationarity)
               v                             v
    RL/RLHF: MDP                  Diffusion models
    (policy eval,                 (DDPM as CTMC,
     value functions)              reverse Markov)

========================================================================

Appendix A: Harmonic Functions and the Dirichlet Problem

A function h:SRh : \mathcal{S} \to \mathbb{R} is harmonic for a Markov chain at state ii if:

h(i)=jPijh(j)=(Ph)(i)h(i) = \sum_j P_{ij} h(j) = (Ph)(i)

That is, h(i)h(i) equals the expected value of hh one step later. Harmonic functions are the "conserved quantities" of Markov chains - if h(X0)h(X_0) is the starting value, then h(Xn)h(X_n) forms a martingale.

Maximum principle: For an irreducible chain on a finite state space, the only bounded harmonic functions are constants. This is the probabilistic analogue of the maximum principle for harmonic functions in PDE theory.

Dirichlet problem: On a chain with absorbing boundary S\partial \mathcal{S} and interior S\mathcal{S}^\circ, the function h(i)=Ei[f(Xτ)]h(i) = E_i[f(X_{\tau_\partial})] (expected boundary value at hitting time) is the unique solution to:

h(i)=(Ph)(i) for iS,h(i)=f(i) for iSh(i) = (Ph)(i) \text{ for } i \in \mathcal{S}^\circ, \quad h(i) = f(i) \text{ for } i \in \partial\mathcal{S}

Application - gambler's ruin: Let h(i)=P(reach NX0=i)h(i) = P(\text{reach } N \mid X_0=i). Then hh is harmonic on {1,,N1}\{1,\ldots,N-1\} with boundary conditions h(0)=0h(0)=0, h(N)=1h(N)=1. For the simple random walk: h(i)=i/Nh(i) = i/N.

For AI: Value functions in reinforcement learning are harmonic: Vπ(s)=aπ(as)[r(s,a)+γsP(ss,a)Vπ(s)]V^\pi(s) = \sum_a \pi(a|s)[r(s,a) + \gamma \sum_{s'}P(s'|s,a)V^\pi(s')] is a Bellman equation - a discrete-time analogue of the Dirichlet problem with discount factor γ\gamma.


Appendix B: Spectral Theory for Non-Reversible Chains

For non-reversible chains, eigenvalues of PP can be complex (though all have λ1|\lambda| \leq 1). The Jordan form replaces the diagonal eigendecomposition.

Pseudo-spectrum: For nearly-reversible chains, the pseudo-spectrum can be much larger than the spectrum, causing transient amplification before eventual convergence.

Non-reversible speedup: Some non-reversible chains mix faster than any reversible chain with the same stationary distribution. The "lifted" or "skewed" chains in the literature achieve mixing time O(1/h2)O(1/h^2) vs. O(1/h)O(1/h) for reversible chains, where hh is the Cheeger constant.

Lifting: A common technique to construct non-reversible fast-mixing chains: add a "direction" variable d{+1,1}d \in \{+1,-1\} to the state, creating a Markov chain on S×{+,}\mathcal{S} \times \{+,-\} that preferentially moves in one direction while still having π\pi as marginal stationary distribution.


Appendix C: Advanced MCMC Methods

C.1 Stochastic Gradient Langevin Dynamics (SGLD)

For Bayesian inference on large datasets, standard MH requires evaluating logπ(θ)\log\pi(\theta) at the full dataset - too expensive. SGLD combines SGD with Gaussian noise:

θt+1=θtηt2L~(θt)+ηtξt,ξtN(0,I)\theta_{t+1} = \theta_t - \frac{\eta_t}{2}\nabla \tilde{L}(\theta_t) + \sqrt{\eta_t}\,\xi_t, \quad \xi_t \sim \mathcal{N}(0,I)

where L~(θ)=Nniminibatchlogp(xiθ)logp(θ)\tilde{L}(\theta) = -\frac{N}{n}\sum_{i \in \text{minibatch}} \log p(x_i|\theta) - \log p(\theta) is the mini-batch stochastic gradient.

For step sizes satisfying tηt=\sum_t \eta_t = \infty and tηt2<\sum_t \eta_t^2 < \infty (Robbins-Monro conditions), the chain converges to samples from the posterior. At small but fixed step size η\eta, the chain samples from an approximate posterior within O(η)O(\eta) of the true posterior.

For AI: SGLD underlies Bayesian deep learning at scale. It explains why SGD with learning rate decay has regularisation properties: the decaying noise variance means the chain converges to a tighter distribution around the posterior mode.

C.2 Parallel Tempering (Replica Exchange)

For multimodal targets, single chains can get stuck in one mode. Parallel tempering runs KK chains at different temperatures β1<β2<<βK\beta_1 < \beta_2 < \cdots < \beta_K (with βK=1\beta_K = 1 being the target). Periodically, adjacent chains swap states with MH acceptance probability:

a=min(1,πβi(xj)πβj(xi)πβi(xi)πβj(xj))a = \min\left(1,\, \frac{\pi_{\beta_i}(x_j)\pi_{\beta_j}(x_i)}{\pi_{\beta_i}(x_i)\pi_{\beta_j}(x_j)}\right)

High-temperature chains mix fast (flat distribution) and pass samples to low-temperature chains through swaps. This enables exploration of multiple modes.

C.3 Slice Sampling

Slice sampling introduces a uniform auxiliary variable uUniform(0,π(θ))u \sim \text{Uniform}(0, \pi(\theta)), creating a joint distribution on (θ,u)(\theta, u) that is uniform on the "slice" {(θ,u):u<π(θ)}\{(\theta, u) : u < \pi(\theta)\}. Marginalising out uu recovers π(θ)\pi(\theta).

The slice sampler alternates between updating uu (trivial given θ\theta) and updating θ\theta (uniform on the slice level set). No tuning parameter; acceptance rate = 1; effective for univariate distributions.


Appendix D: Worked Examples

D.1 Google PageRank Computation (Mini Example)

Consider a 4-page web: page 1 links to 2,3; page 2 links to 4; page 3 links to 2; page 4 links to 1,3.

Transition matrix (with no damping for clarity):

P=(01/21/20000101001/201/20)P = \begin{pmatrix}0&1/2&1/2&0\\0&0&0&1\\0&1&0&0\\1/2&0&1/2&0\end{pmatrix}

Stationary distribution (solve πP=π\pi P = \pi, πi=1\sum\pi_i=1): π=(4/13,3/13,4/13,2/13)\pi = (4/13, 3/13, 4/13, 2/13) - verified by direct computation.

With damping α=0.85\alpha=0.85: P=0.85P+0.15J/4P' = 0.85 P + 0.15 \cdot J/4 where JJ is the all-ones matrix. Power iteration converges in ~30 iterations.

D.2 MCMC on a Bimodal Distribution

Target: π(θ)e(θ3)2/2+e(θ+3)2/2\pi(\theta) \propto e^{-(\theta-3)^2/2} + e^{-(\theta+3)^2/2} (mixture of two Gaussians at ±3\pm 3).

With Gaussian random walk MH (σ=0.5\sigma=0.5): chain mixes within one mode; inter-modal jumps are rare because the barrier between modes is O(10)O(10) nats. Acceptance rate ~60%, but ESS/T ~0.02 (poor mixing).

With σ=3\sigma=3: chain jumps between modes; acceptance rate ~15%, ESS/T ~0.10 (better mixing).

This illustrates the exploration-exploitation tradeoff in MCMC proposal design.

D.3 HMM Example: Weather from Ice Core

Two hidden states: Ice Age (I), Warm Period (W). Observations: high (hh) or low (ll) dust in ice core.

Transition: PII=0.7,PIW=0.3,PWI=0.4,PWW=0.6P_{II}=0.7, P_{IW}=0.3, P_{WI}=0.4, P_{WW}=0.6. Emission: P(hI)=0.8,P(lI)=0.2,P(hW)=0.2,P(lW)=0.8P(h|I)=0.8, P(l|I)=0.2, P(h|W)=0.2, P(l|W)=0.8.

For observation sequence h,l,h,h,lh, l, h, h, l: the Viterbi algorithm gives hidden sequence I,W,I,I,WI, W, I, I, W (high dust = ice age, low dust = warm period).

Forward algorithm gives likelihood P(obs sequence)0.0035P(\text{obs sequence}) \approx 0.0035.


Appendix E: Key Theorems and Proofs

E.1 Proof: Stationary Distribution is Unique for Finite Irreducible Chains

Theorem. A finite irreducible Markov chain has a unique stationary distribution π\pi with πi>0\pi_i > 0 for all ii.

Proof. Consider the NN-dimensional simplex Δ={μ:μi0,μi=1}\Delta = \{\mu : \mu_i \geq 0, \sum\mu_i = 1\}. The map T:μμPT: \mu \mapsto \mu P is a continuous map from Δ\Delta to Δ\Delta. By Brouwer's fixed point theorem, TT has at least one fixed point π\pi.

For uniqueness: suppose π\pi and ν\nu are two stationary distributions. Since the chain is irreducible and finite, all states are positive recurrent, so πi=1/μi>0\pi_i = 1/\mu_i > 0 where μi\mu_i is the mean return time. The mean return time is unique (it equals 1/πi1/\pi_i and also 1/νi1/\nu_i if ν\nu is stationary), so πi=νi\pi_i = \nu_i for all ii.

For πi>0\pi_i > 0: by irreducibility, from any state jj there exists a path to ii. The stationary mass at each state in this path is >0> 0 (by positivity of transitions and stationarity), propagating to give πi>0\pi_i > 0.

E.2 Proof: Detailed Balance Implies Stationarity

Theorem. If πiPij=πjPji\pi_i P_{ij} = \pi_j P_{ji} for all i,ji,j, then πP=π\pi P = \pi.

Proof. For each jj:

(πP)j=iπiPij=iπjPji=πjiPji=πj1=πj(\pi P)_j = \sum_i \pi_i P_{ij} = \sum_i \pi_j P_{ji} = \pi_j \sum_i P_{ji} = \pi_j \cdot 1 = \pi_j

where we used detailed balance in the second equality and the row-sum property of stochastic matrices in the fourth.

E.3 Proof: Convergence via Coupling (Sketch)

Theorem. Let PP be ergodic. For any μ,ν\mu, \nu and coupling (Xt,Yt)(X_t, Y_t) with X0μX_0 \sim \mu, Y0νY_0 \sim \nu, define τ=min{t:Xt=Yt}\tau = \min\{t : X_t = Y_t\}. Then:

μPtνPtTVP(τ>t)\|\mu P^t - \nu P^t\|_{\text{TV}} \leq P(\tau > t)

Proof. For any event AA:

μPt(A)νPt(A)=P(XtA)P(YtA)=P(XtA,τt)+P(XtA,τ>t)P(YtA,τt)P(YtA,τ>t)\mu P^t(A) - \nu P^t(A) = P(X_t \in A) - P(Y_t \in A) = P(X_t \in A, \tau \leq t) + P(X_t \in A, \tau > t) - P(Y_t \in A, \tau \leq t) - P(Y_t \in A, \tau > t)

On {τt}\{\tau \leq t\}: Xt=YtX_t = Y_t (coalesce), so P(XtA,τt)=P(YtA,τt)P(X_t \in A, \tau \leq t) = P(Y_t \in A, \tau \leq t). Therefore:

μPt(A)νPt(A)=P(XtA,τ>t)P(YtA,τ>t)P(τ>t)|\mu P^t(A) - \nu P^t(A)| = |P(X_t \in A, \tau > t) - P(Y_t \in A, \tau > t)| \leq P(\tau > t)

Taking supremum over AA gives the result.


Appendix F: Python Recipes for Markov Chains

import numpy as np

# -- 1. Power iteration for stationary distribution
def stationary(P, tol=1e-12, max_iter=10000):
    pi = np.ones(P.shape[0]) / P.shape[0]
    for _ in range(max_iter):
        pi_new = pi @ P
        if np.max(np.abs(pi_new - pi)) < tol:
            return pi_new
        pi = pi_new
    return pi

# -- 2. Simulate a Markov chain
def simulate_chain(P, x0, n_steps):
    N = P.shape[0]
    x = x0
    trajectory = [x]
    for _ in range(n_steps):
        x = np.random.choice(N, p=P[x])
        trajectory.append(x)
    return np.array(trajectory)

# -- 3. Metropolis-Hastings for 1D target
def metropolis_hastings(log_pi, n_steps, sigma=0.5, x0=0.0):
    x = x0
    samples = []
    for _ in range(n_steps):
        x_prime = x + sigma * np.random.randn()
        log_a = log_pi(x_prime) - log_pi(x)
        if np.log(np.random.rand()) < log_a:
            x = x_prime
        samples.append(x)
    return np.array(samples)

# -- 4. PageRank
def pagerank(A, alpha=0.85, tol=1e-8):
    N = A.shape[0]
    out_degree = A.sum(axis=1, keepdims=True)
    # Fix dangling nodes
    dangling = (out_degree == 0).flatten()
    out_degree[dangling] = 1
    P = A / out_degree
    P[dangling] = 1 / N
    teleport = np.ones((N, N)) / N
    P_hat = alpha * P + (1 - alpha) * teleport
    pi = np.ones(N) / N
    for _ in range(1000):
        pi_new = pi @ P_hat
        if np.max(np.abs(pi_new - pi)) < tol:
            return pi_new
        pi = pi_new
    return pi

# -- 5. HMM forward algorithm
def hmm_forward(obs, T, E, pi0):
    """obs: list of observations, T: NxN transition, E: NxM emission, pi0: initial."""
    N = T.shape[0]
    alpha = np.zeros((len(obs), N))
    alpha[0] = pi0 * E[:, obs[0]]
    for t in range(1, len(obs)):
        alpha[t] = (alpha[t-1] @ T) * E[:, obs[t]]
    return alpha, alpha[-1].sum()  # alpha matrix and likelihood

Appendix G: Connections to Other Areas

Spectral graph theory: For a random walk on an undirected graph GG, the transition matrix P=D1AP = D^{-1}A (where DD is the degree matrix and AA is the adjacency matrix) has eigenvalues in [1,1][-1,1]. The spectral gap of the Laplacian L=ID1/2AD1/2L = I - D^{-1/2}AD^{-1/2} equals the spectral gap of the walk. Well-connected (expander) graphs have large spectral gaps and fast-mixing random walks.

Information theory: The relative entropy (KL divergence) between the chain's distribution and stationarity decreases monotonically: DKL(μ(n)π)DKL(μ(n+1)π)D_{\text{KL}}(\mu^{(n)} \| \pi) \geq D_{\text{KL}}(\mu^{(n+1)} \| \pi). This is the data processing inequality applied to the Markov transition. The rate of KL decrease is related to the spectral gap.

Ergodic theory: The Markov chain ergodic theorem is a special case of Birkhoff's ergodic theorem in abstract measure-preserving systems. The stationarity condition πP=π\pi P = \pi says π\pi is an invariant measure for the measure-preserving map T:ΩΩT : \Omega \to \Omega on the path space.

Linear programming: Computing the stationary distribution satisfying πP=π\pi P = \pi, π0\pi \geq 0, π1=1\|\pi\|_1 = 1 is a linear system. The LP relaxation of combinatorial problems can sometimes be solved by finding stationary distributions of associated Markov chains.

Quantum computing: Quantum walks are the quantum analogue of random walks on graphs, with amplitudes instead of probabilities. They can achieve quadratic speedups over classical random walks for certain search and sampling problems.


Appendix H: Self-Assessment Checklist

Before moving to Chapter 7 (Statistics), verify you can:

  • Write the transition matrix for any small Markov chain from a verbal description
  • Compute μ(n)=μ(0)Pn\mu^{(n)} = \mu^{(0)}P^n for n=1,2,5n = 1, 2, 5 by hand or code
  • Classify all states of a small chain as communicating/recurrent/transient/absorbing
  • Solve πP=π\pi P = \pi for a 2×22\times2 or 3×33\times3 stochastic matrix
  • State Perron-Frobenius and explain why the stationary distribution is unique
  • Verify detailed balance for a given (π,P)(\pi, P) pair
  • Compute the spectral gap from eigenvalues and give a mixing time bound
  • Implement Metropolis-Hastings for a 1D target and compute acceptance rate
  • Implement power iteration for PageRank with dangling node handling
  • Implement the HMM forward algorithm and compute the likelihood of an observation sequence
  • Explain why MCMC sample autocorrelation reduces the effective sample size
  • State the ergodic theorem and explain why it justifies MCMC estimation

Appendix I: Detailed Proofs - Perron-Frobenius and Mixing

I.1 Perron-Frobenius Theorem (Complete Proof)

We prove the Perron-Frobenius theorem for primitive stochastic matrices.

Step 1: Eigenvalue 1 exists. For any stochastic matrix PP, P1=1P\mathbf{1} = \mathbf{1} (column vector of ones). So 1TP=1T\mathbf{1}^T P = \mathbf{1}^T iff PP is doubly stochastic. More relevantly: by the Brouwer fixed-point theorem on ΔN1\Delta^{N-1}, the map ππP\pi \mapsto \pi P has a fixed point - the stationary distribution. This fixed point is a left eigenvector with eigenvalue 1.

Step 2: λ1|\lambda| \leq 1 for all eigenvalues. Let λ\lambda be an eigenvalue with (right) eigenvector vv (Pv=λvPv = \lambda v). Pick vmax=maxiviv_{\max} = \max_i |v_i|. Then λvmax=λvi=(Pv)i=jPijvjjPijvjvmax|\lambda||v_{\max}| = |\lambda v_{i^*}| = |(Pv)_{i^*}| = |\sum_j P_{i^*j}v_j| \leq \sum_j P_{i^*j}|v_j| \leq v_{\max}. So λ1|\lambda| \leq 1.

Step 3: λ=1|\lambda| = 1 implies λ=1\lambda = 1 for primitive PP. For a primitive matrix, Pm>0P^m > 0 (all entries strictly positive) for some mm. Suppose λ=1|\lambda| = 1, λ1\lambda \neq 1, with Pv=λvPv = \lambda v and vmax=1v_{\max} = 1. The equality case in Step 2 requires vj=1|v_j| = 1 for all jj and all rows of PP to "align" the phases of vjv_j. But for Pm>0P^m > 0, all rows of PmP^m are strictly positive, and the alignment condition forces λm=1\lambda^m = 1 and all vjv_j equal - meaning v=c1v = c\mathbf{1} and λ=1\lambda = 1. Contradiction.

Step 4: Convergence. Since λ=1\lambda = 1 is the unique eigenvalue on the unit circle for primitive PP, all other eigenvalues λk\lambda_k satisfy λk1δ|\lambda_k| \leq 1 - \delta for some δ>0\delta > 0. The spectral decomposition gives Pn=1π+k2λknϕkψkTP^n = \mathbf{1}\pi + \sum_{k \geq 2} \lambda_k^n \phi_k \psi_k^T where λkn0|\lambda_k|^n \to 0 at rate (1δ)n(1-\delta)^n.

I.2 Coupling Proof of Convergence (Complete)

Theorem. Let PP be ergodic with stationary distribution π\pi. For any xSx \in \mathcal{S}:

Pn(x,)πTVP(τ>n)\|P^n(x,\cdot) - \pi\|_{\text{TV}} \leq P(\tau > n)

where τ\tau is the coalescence time of the Markov coupling (two chains started at xx and π\sim\pi).

Proof. Let (Xn,Yn)(X_n, Y_n) be a coupling with X0=xX_0 = x and Y0πY_0 \sim \pi, where both chains use the same transition PP and coalesce when they meet. Since Y0πY_0 \sim \pi and π\pi is stationary, YnπY_n \sim \pi for all nn.

For any event AA:

Pn(x,A)π(A)=P(XnA)P(YnA)P^n(x, A) - \pi(A) = P(X_n \in A) - P(Y_n \in A)

On {τn}\{\tau \leq n\}: Xn=YnX_n = Y_n (they have coalesced), so their indicators for AA are equal.

Pn(x,A)π(A)=E[1XnA1YnA]P^n(x, A) - \pi(A) = E[\mathbf{1}_{X_n \in A} - \mathbf{1}_{Y_n \in A}] =E[(1XnA1YnA)1τ>n]= E[(\mathbf{1}_{X_n \in A} - \mathbf{1}_{Y_n \in A})\mathbf{1}_{\tau > n}] E[1τ>n]=P(τ>n)\leq E[\mathbf{1}_{\tau > n}] = P(\tau > n)

Since this holds for any AA and for AcA^c by symmetry, taking the supremum gives the TV bound.

I.3 Spectral Gap Lower Bound via Conductance (Cheeger Inequality)

Definition. The conductance (Cheeger constant) of a chain is:

h=minSS,π(S)1/2iS,jSπiPijπ(S)h = \min_{S \subseteq \mathcal{S}, \pi(S) \leq 1/2} \frac{\sum_{i \in S, j \notin S} \pi_i P_{ij}}{\pi(S)}

Conductance measures the minimum probability flux crossing any "bottleneck" cut in the chain.

Cheeger Inequality: For a reversible chain:

h22gap2h\frac{h^2}{2} \leq \text{gap} \leq 2h

The lower bound is the harder direction and gives: gaph2/2\text{gap} \geq h^2/2. In words: a large bottleneck (small hh) implies a small spectral gap (slow mixing). Conversely, if hh is bounded below, the chain mixes in O(1/h2)O(1/h^2) steps (vs. O(1/h)O(1/h) for the tighter bound gap2h\text{gap} \leq 2h).

For AI: The Cheeger inequality provides the tightest practical bounds for MCMC convergence. For neural network posteriors, the conductance can be estimated using gradient information - a well-conditioned posterior (no sharp barriers) has high conductance.


Appendix J: Markov Chains and Entropy

J.1 Entropy Production

The relative entropy (KL divergence) from the chain's distribution to stationarity is:

H(μ(n)π)=iμi(n)logμi(n)πiH(\mu^{(n)} \| \pi) = \sum_i \mu^{(n)}_i \log \frac{\mu^{(n)}_i}{\pi_i}

Data processing inequality (for Markov chains): H(μ(n+1)π)H(μ(n)π)H(\mu^{(n+1)} \| \pi) \leq H(\mu^{(n)} \| \pi)

This follows from the data processing inequality: applying the stochastic map PP cannot increase KL divergence. Equality holds iff μ(n)=π\mu^{(n)} = \pi.

For reversible chains: The relative entropy decays at rate governed by the log-Sobolev constant. Chains with larger log-Sobolev constants (tighter functional inequalities) decay faster.

J.2 Entropy of the Stationary Distribution

The Shannon entropy of π\pi is H(π)=iπilogπiH(\pi) = -\sum_i \pi_i \log \pi_i. For a uniform distribution (doubly stochastic PP), H(π)=logNH(\pi) = \log N is maximised. For PageRank, the entropy of π\pi measures how "concentrated" web traffic is - low entropy means a few pages dominate.

Maximum entropy interpretation: The stationary distribution of a reversible chain with detailed balance can be interpreted as the maximum entropy distribution subject to the constraint that the detailed balance equations hold. This connects Markov chains to the exponential family (Section02).

J.3 Kullback-Leibler and MCMC Acceptance

The Metropolis-Hastings acceptance ratio has an information-theoretic interpretation:

a(θ,θ)=min(1,π(θ)q(θθ)π(θ)q(θθ))=min(1,elogπ(θ)logπ(θ)logq(θθ)+logq(θθ))a(\theta, \theta') = \min\left(1, \frac{\pi(\theta')q(\theta|\theta')}{\pi(\theta)q(\theta'|\theta)}\right) = \min\left(1, e^{\log\pi(\theta')-\log\pi(\theta)-\log q(\theta'|\theta)+\log q(\theta|\theta')}\right)

The exponent is the log-ratio of probability densities - related to the KL divergence between the proposal and target. When the proposal qq matches π\pi well, most proposals are accepted.


Appendix K: Continuous-Time Chains and Generators

K.1 Semigroup Theory

The family of transition matrices {P(t)}t0\{P(t)\}_{t \geq 0} forms a Markov semigroup: P(0)=IP(0) = I, P(s+t)=P(s)P(t)P(s+t) = P(s)P(t), and tP(t)t \mapsto P(t) is continuous in tt. The generator Q=ddtP(t)t=0Q = \frac{d}{dt}P(t)\big|_{t=0} characterises the infinitesimal behaviour.

Exponential formula: P(t)=eQtP(t) = e^{Qt} where the matrix exponential can be computed via eigendecomposition of QQ. If QQ has eigenvalues 0=μ1μ2μN0 = \mu_1 \geq \mu_2 \geq \cdots \geq \mu_N (all real and non-positive for reversible chains), then:

P(t)=keμktϕkψkTP(t) = \sum_k e^{\mu_k t} \phi_k \psi_k^T

Convergence to stationarity is at rate eμ2te^{|\mu_2|t} where μ2=μ2|\mu_2| = -\mu_2 is the spectral gap of QQ.

K.2 Reversible CTMCs

A CTMC is reversible with respect to π\pi if πiQij=πjQji\pi_i Q_{ij} = \pi_j Q_{ji} for all iji \neq j. The spectral theory for reversible CTMCs is identical to that for reversible DTMCs: real eigenvalues, orthonormal eigenfunctions, exponential convergence.

Detailed balance for CTMCs:

πiqij=πjqji for all ij\pi_i q_{ij} = \pi_j q_{ji} \text{ for all } i \neq j

This is the continuous-time analogue of the discrete detailed balance. Metropolis-Hastings in continuous time (the Metropolis algorithm for particle simulations) satisfies this by construction.


Appendix L: Theory Problems

Problem L.1. Prove that for a doubly stochastic matrix PP (rows and columns both sum to 1), the uniform distribution is stationary. Give an example showing the converse fails.

Problem L.2. Let PP be irreducible with stationary distribution π\pi. Prove that πi>0\pi_i > 0 for all ii by constructing a path from any jj with πj>0\pi_j > 0 to ii.

Problem L.3. (Coupling inequality) Let PP be ergodic. Construct a coupling of two copies of the chain, one started at xx and one at the stationary distribution π\pi. Use the coupling inequality to prove Pn(x,)πTVP(τmeet>n)\|P^n(x,\cdot) - \pi\|_{\text{TV}} \leq P(\tau_{\text{meet}} > n).

Problem L.4. For the symmetric random walk on {0,1,,N}\{0,1,\ldots,N\} with reflecting barriers, show the spectral gap is 1cos(π/N)π2/(2N2)1 - \cos(\pi/N) \approx \pi^2/(2N^2) for large NN. What is the mixing time?

Problem L.5. (Metropolis-Hastings correctness) Let K(x,y)K(x,y) be the MH transition kernel with proposal qq and target π\pi. Show that πxK(x,y)=πyK(y,x)\pi_x K(x,y) = \pi_y K(y,x) (detailed balance), and conclude π\pi is stationary.

Problem L.6. For a birth-death chain on {0,1,,N}\{0,1,\ldots,N\} with rates pi=pp_i = p and qi=qq_i = q (constant), find the stationary distribution. For what values of pp does the chain have a proper stationary distribution on {0,1,2,}\{0,1,2,\ldots\} (infinite chain)?

Problem L.7. The lazy walk on a graph GG sets Pii=1/2P_{ii} = 1/2 (stay with prob 1/2) and Pij=1/(2di)P_{ij} = 1/(2d_i) for neighbours jj (where did_i is the degree). Show the lazy walk has all non-negative eigenvalues and therefore converges monotonically to stationarity in TV distance.

Problem L.8. (PageRank convergence rate) For the damped PageRank matrix P=αP0+(1α)J/NP = \alpha P_0 + (1-\alpha)J/N with damping α\alpha, show all eigenvalues except the dominant one have magnitude α\leq \alpha. Conclude the PageRank power iteration converges in O(log(1/ε)/log(1/α))O(\log(1/\varepsilon)/\log(1/\alpha)) iterations.


Appendix M: Notation Summary

SymbolMeaning
S\mathcal{S}State space
PijP_{ij}Transition probability from state ii to state jj
Pij(n)P^{(n)}_{ij}nn-step transition probability ((Pn)ij(P^n)_{ij})
μ(n)\mu^{(n)}Distribution at time nn; row vector
π\piStationary distribution; row vector with πP=π\pi P = \pi
fif_iReturn probability to state ii: P(Ti<X0=i)P(T_i < \infty \mid X_0=i)
μi\mu_iMean return time to state ii; πi=1/μi\pi_i = 1/\mu_i
d(i)d(i)Period of state ii: gcd{n1:Pii(n)>0}\gcd\{n \geq 1 : P^{(n)}_{ii} > 0\}
μνTV\|\mu - \nu\|_{\text{TV}}Total variation distance between μ\mu and ν\nu
tmix(ε)t_{\text{mix}}(\varepsilon)Mixing time to ε\varepsilon-accuracy
gap(P)\text{gap}(P)Spectral gap: $1 -
hhCheeger constant (conductance)
QQGenerator matrix of a CTMC; Qij=qijQ_{ij} = q_{ij}, Qii=qiQ_{ii} = -q_i
P(t)=eQtP(t) = e^{Qt}CTMC transition matrix at time tt
a(θ,θ)a(\theta,\theta')MH acceptance probability
τmeet\tau_{\text{meet}}Coupling time (coalescence time of two chains)
αt(i)\alpha_t(i)HMM forward variable: P(X1,,Xt,Zt=i)P(X_1,\ldots,X_t, Z_t=i)
δt(j)\delta_t(j)Viterbi variable: maxz1:t1P(X1,,Xt,Zt=j,z1:t1)\max_{z_{1:t-1}} P(X_1,\ldots,X_t, Z_t=j, z_{1:t-1})

Appendix N: Advanced ML Applications

N.1 RLHF as a Constrained MDP

Reinforcement learning from human feedback (RLHF) trains a language model policy πθ\pi_\theta to maximise:

Eπθ[rϕ(x,y)]βDKL(πθπref)\mathbb{E}_{\pi_\theta}[r_\phi(x, y)] - \beta \cdot D_{\text{KL}}(\pi_\theta \| \pi_{\text{ref}})

where rϕr_\phi is a learned reward model, πref\pi_{\text{ref}} is the reference (SFT) policy, and β\beta controls the KL penalty.

This is a constrained Markov chain optimisation problem: the state space is all possible prefixes (contexts), the action space is the vocabulary, and the KL divergence penalty ensures the learned chain πθ\pi_\theta doesn't deviate too far from the reference chain πref\pi_{\text{ref}} in transition structure.

The optimal policy has an explicit closed form:

π(yx)=πref(yx)erϕ(x,y)/βZ(x)\pi^*(y|x) = \pi_{\text{ref}}(y|x) \cdot \frac{e^{r_\phi(x,y)/\beta}}{Z(x)}

where Z(x)Z(x) is a normalising constant. This is a Gibbs distribution with the reward as energy function - exactly the target distribution for MCMC.

Implication: The RLHF optimal policy can be sampled by constructing a Markov chain with the above stationary distribution. Recent work uses Langevin-type MCMC to directly sample from π\pi^* without RL.

N.2 Diffusion Sampling as Time-Reversed CTMC

The score-based generative modelling framework (Song et al., 2021) writes the forward noising process as a CTMC/SDE and generates by running the reversed process. The key identity is Anderson's reverse-time SDE:

dx=[f(x,t)g(t)2xlogpt(x)]dt+g(t)dBˉtdx = [f(x,t) - g(t)^2 \nabla_x \log p_t(x)]\,dt + g(t)\,d\bar{B}_t

where Bˉt\bar{B}_t is a Brownian motion running backwards in time. The score function xlogpt(x)\nabla_x \log p_t(x) is the generator of the reversed Markov chain. Learning this score function via denoising score matching allows sampling by running the reverse process.

Discrete diffusion: For categorical distributions (text), the VP-SDE is replaced by a CTMC on the vocabulary with generator Q(t)Q(t). MDLM and other discrete diffusion models use the forward chain q(xtx0)q(x_t|x_0) = masked tokens, Uniform transitions, or absorbing-state corruption, then learn the reverse chain.

N.3 MCMC for LLM Sampling

Standard autoregressive LLM decoding (top-kk, nucleus) can be viewed as Markov chain sampling from a specific transition matrix. Recent speculative decoding methods (Chen et al., 2023; Leviathan et al., 2023) use a smaller "draft" model as a proposal and the large model as the acceptance criterion - this is precisely independence Metropolis-Hastings:

  • Proposal: q(xn+1context)=small_LM(xn+1context)q(x_{n+1}|\text{context}) = \text{small\_LM}(x_{n+1}|\text{context})
  • Acceptance: a=min(1,large_LM/small_LM)a = \min(1, \text{large\_LM}/\text{small\_LM})
  • Guarantees: the accepted tokens have the same distribution as large-LM-only sampling

This is MCMC applied to language model decoding - Markov chain theory directly yields correctness of speculative decoding.

N.4 Graph Neural Networks and Random Walks

Graph Neural Networks (GNNs) can be understood through the lens of Markov chains. A single GNN layer performs:

hv(k+1)=f(hv(k),AGGREGATE({hu(k):uN(v)}))h_v^{(k+1)} = f\left(h_v^{(k)},\, \text{AGGREGATE}\left(\{h_u^{(k)} : u \in \mathcal{N}(v)\}\right)\right)

For the simple mean-aggregation GNN: hv(k+1)=σ(Whv(k)+W1dvuvhu(k))h_v^{(k+1)} = \sigma(W h_v^{(k)} + W'\frac{1}{d_v}\sum_{u \sim v} h_u^{(k)}). The aggregation step is applying the random walk transition matrix D1AD^{-1}A to the feature matrix. kk layers of GNN = applying the random walk kk times - the receptive field grows as the kk-step neighbourhood.

Over-smoothing: After many layers, all node features converge to the stationary distribution of the random walk (uniform for regular graphs). This is over-smoothing - the GNN loses discriminative power because the Markov chain mixes. Mixing time bounds from Section6 directly bound the number of useful GNN layers.

N.5 Token Position Encoding as Markov Chain

The self-attention mechanism with rotary position encoding (RoPE) can be viewed as computing a position-dependent Markov chain over tokens. Each attention head defines a stochastic matrix over token positions (the attention weights), and information flows along this chain.

Attention as ergodic averaging: In multi-head attention, each head computes softmax(QKT/d)V\text{softmax}(QK^T/\sqrt{d})V. The softmax weights form a row-stochastic matrix - a one-step Markov transition. The output is the expected value of VV under this distribution. Stacking attention layers is like composing Markov kernels.

Information bottleneck: The rank of the attention matrix limits the "state space" of the Markov chain. Low-rank attention (as in linear attention / Performer) corresponds to a Markov chain with restricted state space - mixing may be faster but with less expressivity.


Appendix O: Review Problems

Review Problem 1. Let PP be a 3×33\times3 stochastic matrix with P11=0.5,P12=0.3,P13=0.2,P21=0.1,P22=0.7,P23=0.2,P31=0.3,P32=0.3,P33=0.4P_{11}=0.5, P_{12}=0.3, P_{13}=0.2, P_{21}=0.1, P_{22}=0.7, P_{23}=0.2, P_{31}=0.3, P_{32}=0.3, P_{33}=0.4. (a) Is this chain irreducible? (b) Find the stationary distribution. (c) Starting from state 1, compute the distribution after 10 steps. (d) Compare to stationarity.

Review Problem 2. Consider a random walk on a cycle of length NN (states {0,1,,N1}\{0,1,\ldots,N-1\}, move clockwise or counterclockwise with equal probability 1/21/2). (a) What is the stationary distribution? (b) What is the period? (c) Compute the mixing time using the spectral gap (eigenvalues: cos(2πk/N)\cos(2\pi k/N) for k=0,,N1k=0,\ldots,N-1). (d) How does mixing time scale with NN?

Review Problem 3. (MCMC) You want to sample from π(x)xa1ebx\pi(x) \propto x^{a-1}e^{-bx} (a Gamma distribution) using MH with log-normal proposal q(xx)=LogNormal(logx,σ2)q(x'|x) = \text{LogNormal}(\log x, \sigma^2). (a) Write down the acceptance ratio. (b) Is qq symmetric? (c) Will this chain have π\pi as stationary distribution?

Review Problem 4. (PageRank) The web has 3 pages: 1 links to 2 and 3; 2 links to 1; 3 links to 1. With damping α=0.85\alpha=0.85, write the PageRank transition matrix and find π\pi by solving a linear system. Which page has the highest PageRank?

Review Problem 5. (HMM) For an HMM with 2 hidden states, 2 observations, transition T=(0.60.40.30.7)T = \begin{pmatrix}0.6&0.4\\0.3&0.7\end{pmatrix}, emission E=(0.90.10.20.8)E = \begin{pmatrix}0.9&0.1\\0.2&0.8\end{pmatrix}, initial π0=(0.5,0.5)\pi_0 = (0.5, 0.5): (a) Compute P(obs=(0,1,0))P(\text{obs}=(0,1,0)) using the forward algorithm. (b) Find the Viterbi path.


Appendix P: Common Distributions in MCMC Targets

When π(θ)exp(f(θ))\pi(\theta) \propto \exp(-f(\theta)), the gradient f\nabla f drives Langevin dynamics. Key shapes:

Gaussian: f(θ)=12θTΣ1θf(\theta) = \frac{1}{2}\theta^T\Sigma^{-1}\theta. Gradient: Σ1θ\Sigma^{-1}\theta. Langevin mixes in O(κ(Σ))O(\kappa(\Sigma)) steps. HMC mixes in O(κ(Σ)1/2)O(\kappa(\Sigma)^{1/2}) steps (advantage for ill-conditioned targets).

Logistic posterior: f(θ)=iyixiTθ+ilog(1+exiTθ)+12σ2θ2f(\theta) = -\sum_i y_i x_i^T\theta + \sum_i \log(1+e^{x_i^T\theta}) + \frac{1}{2\sigma^2}\|\theta\|^2. Strongly log-concave; SGLD mixes in polynomial time.

Mixture of Gaussians: π=kwkN(μk,Σk)\pi = \sum_k w_k \mathcal{N}(\mu_k, \Sigma_k). Multi-modal; standard MCMC can get stuck between modes. Parallel tempering or simulated annealing needed for good mixing.

Heavy-tailed: f(θ)=νlog(1+θ2/ν)f(\theta) = \nu\log(1 + \|\theta\|^2/\nu) (Student-t). Sub-quadratic growth means the tails are heavy. HMC can struggle; NUTS with dual averaging handles this.

Boltzmann (energy-based models): f(θ)Eϕ(x)f(\theta) \propto -E_\phi(x) for neural network energy EϕE_\phi. Sampling requires running MCMC; contrastive divergence (CD-kk) uses short MCMC chains as an approximation.


Appendix Q: Further Reading

  1. Levin, Peres, Wilmer - Markov Chains and Mixing Times (AMS, 2nd ed., 2017) - the definitive reference for mixing times and coupling; freely available online

  2. Norris, J.R. - Markov Chains (Cambridge, 1997) - rigorous treatment of discrete and continuous-time chains with clean proofs

  3. Brooks, Gelman, Jones, Meng - Handbook of Markov Chain Monte Carlo (CRC Press, 2011) - comprehensive MCMC reference; includes HMC, NUTS, diagnostics

  4. Betancourt, M. - "A Conceptual Introduction to Hamiltonian Monte Carlo" (arXiv, 2017) - outstanding intuitive treatment of HMC geometry

  5. Page et al. - "The PageRank Citation Ranking: Bringing Order to the Web" (Stanford Technical Report, 1999) - original PageRank paper

  6. Rabiner, L.R. - "A Tutorial on Hidden Markov Models and Selected Applications in Speech Recognition" (Proc. IEEE, 1989) - classic HMM reference

  7. Song et al. - "Score-Based Generative Modeling through Stochastic Differential Equations" (ICLR 2021) - connects diffusion models to CTMC theory

  8. Welling & Teh - "Bayesian Learning via Stochastic Gradient Langevin Dynamics" (ICML 2011) - introduces SGLD for large-scale Bayesian inference

  9. Sutton & Barto - Reinforcement Learning: An Introduction (MIT Press, 2nd ed., 2018) - MDPs, Bellman equations, TD-learning; free online


Appendix R: Spectral Methods for Large-Scale Markov Chains

R.1 Lanczos Algorithm for Large Transition Matrices

For transition matrices arising in PageRank (N1010N \sim 10^{10} web pages), direct eigendecomposition is infeasible. The power iteration and Lanczos algorithm enable computing the top eigenvectors using only matrix-vector products.

Power iteration for stationary distribution:

  • Memory: O(N)O(N) for the distribution vector
  • Per-iteration cost: O(nnz)O(\text{nnz}) (number of non-zeros in PP) for sparse matrices
  • Convergence in O(log(1/ε)/log(1/λ2))O(\log(1/\varepsilon) / \log(1/|\lambda_2|)) iterations

For the web graph, λ2α=0.85|\lambda_2| \leq \alpha = 0.85 (damping), so ~70 iterations give ε=108\varepsilon = 10^{-8} precision.

Randomised SVD: For low-rank approximation of PnP^n (needed for long-range transition probability estimation), randomised methods can approximate the top-rr singular vectors in O(Nrlogr)O(N r \log r) time - much cheaper than full SVD.

R.2 Multi-Scale Markov Chains

For chains with multiple timescales (fast-mixing within clusters, slow-mixing between clusters), aggregation-disaggregation methods provide efficient algorithms:

  1. Aggregation: Lump states within each cluster into a single meta-state. Compute the inter-cluster transition matrix Pˉ\bar{P}.
  2. Disaggregation: Compute the within-cluster stationary distributions π(c)\pi^{(c)} for each cluster cc.
  3. Combine: πi=πˉcπi(c)\pi_i = \bar{\pi}_c \cdot \pi^{(c)}_i for state ii in cluster cc.

This decomposition is exact when the chain is "nearly lumpable" (within-cluster transitions are much faster than between-cluster). It's the foundation of multi-scale MCMC methods.

For AI: LLM attention patterns have multi-scale structure: attention heads attend to nearby tokens (fast mixing) and distant context (slow mixing). This multi-scale structure can be exploited for efficient long-context modelling.

R.3 Reversibilisation

Any Markov chain can be made reversible by considering the Doob hh-transform or by multiplicative reversibilisation:

P~ij=12(Pij+πjPji/πi)\tilde{P}_{ij} = \frac{1}{2}(P_{ij} + \pi_j P_{ji} / \pi_i)

The resulting P~\tilde{P} is reversible with the same stationary distribution π\pi. Reversibilisation can speed up or slow down convergence depending on the chain - it destroys the directional bias that non-reversible chains use for faster mixing.


Appendix S: Continuous-State Markov Chains

S.1 Harris Chains

For Markov chains on continuous state spaces (like MCMC on Rd\mathbb{R}^d), the discretestate ergodic theory extends with modifications. A Markov chain with transition kernel K(x,)K(x, \cdot) is:

  • ϕ\phi-irreducible if there exists a σ\sigma-finite measure ϕ\phi such that for any Borel set AA with ϕ(A)>0\phi(A) > 0, we have n=1Kn(x,A)>0\sum_{n=1}^\infty K^n(x, A) > 0 for all xx.
  • Harris recurrent if it is ϕ\phi-irreducible and returns to any set AA with ϕ(A)>0\phi(A) > 0 with probability 1.
  • Positive Harris recurrent if the chain is Harris recurrent and has a unique invariant probability measure π\pi.

A positive Harris recurrent, aperiodic chain is called a Harris ergodic chain, and the ergodic theorem holds: time averages converge to π\pi-expectations.

For MCMC: The Metropolis-Hastings chain on Rd\mathbb{R}^d with Gaussian proposal and a proper target density π\pi is Harris ergodic under mild regularity conditions. This justifies MCMC estimators for continuous posteriors.

S.2 Geometric Ergodicity

A Markov chain is geometrically ergodic if there exist constants C<C < \infty and ρ<1\rho < 1 such that:

Kn(x,)πTVC(x)ρnfor all x\|K^n(x, \cdot) - \pi\|_{\text{TV}} \leq C(x)\,\rho^n \quad \text{for all } x

Geometric ergodicity implies a central limit theorem for MCMC estimators:

n(1nt=1nf(Xt)Eπ[f])dN(0,σf2)\sqrt{n}\left(\frac{1}{n}\sum_{t=1}^n f(X_t) - \mathbb{E}_\pi[f]\right) \xrightarrow{d} \mathcal{N}(0, \sigma_f^2)

where σf2=Varπ(f)+2k=1Covπ(f(X0),f(Xk))\sigma_f^2 = \text{Var}_\pi(f) + 2\sum_{k=1}^\infty \text{Cov}_\pi(f(X_0), f(X_k)) is the asymptotic variance. This is the Markov chain CLT - it justifies asymptotic confidence intervals for MCMC estimates.

For AI: Geometric ergodicity of the Langevin algorithm for log-concave targets gives polynomial bounds on the number of gradient evaluations needed for ε\varepsilon-accurate posterior estimates. This is the theoretical basis for Bayesian deep learning via SGLD.


Appendix T: Markov Chain Games and Nash Equilibria

T.1 Stochastic Games

A stochastic game (Shapley, 1953) is an MDP with multiple agents. Two-player zero-sum stochastic games have:

  • State space S\mathcal{S}; action spaces A1,A2\mathcal{A}_1, \mathcal{A}_2
  • Transition P(ss,a1,a2)P(s'|s, a_1, a_2)
  • Reward r(s,a1,a2)r(s, a_1, a_2) (player 1 maximises, player 2 minimises)

At a Nash equilibrium, each player's policy is a best response to the other's. The value function satisfies a minimax Bellman equation:

V(s)=mina2maxa1[r(s,a1,a2)+γsP(ss,a1,a2)V(s)]V^*(s) = \min_{a_2} \max_{a_1} \left[r(s,a_1,a_2) + \gamma \sum_{s'} P(s'|s,a_1,a_2) V^*(s')\right]

For AI: Multi-agent RL (MARL) with competing agents (e.g., AlphaGo, Libratus for poker) uses stochastic game theory. RLHF with adversarial reward models is a stochastic game between the policy and the worst-case reward model.

T.2 Markov Perfect Equilibrium

In dynamic games, a Markov perfect equilibrium (MPE) is a Nash equilibrium where strategies depend only on the current state (Markov property on the strategy). MPE is the game-theoretic analogue of the optimal policy in MDPs.

Computing MPE requires solving a system of coupled Bellman equations - one per player. For two-player zero-sum games, this reduces to linear programming (minimax theorem). For general-sum games, this is PPAD-complete.


Appendix U: Temporal Difference Learning and Martingales

Connecting back to Section06 Stochastic Processes Section3.7, the TD(0) algorithm is:

V(st)V(st)+αt[rt+γV(st+1)V(st)]V(s_t) \leftarrow V(s_t) + \alpha_t [r_t + \gamma V(s_{t+1}) - V(s_t)]

The update direction δt=rt+γV(st+1)V(st)\delta_t = r_t + \gamma V(s_{t+1}) - V(s_t) is the TD error. Under the true value function VV^*, E[δtst]=0\mathbb{E}[\delta_t | s_t] = 0 (martingale difference). TD learning is a stochastic approximation algorithm for finding the fixed point of the Bellman operator TπV=rπ+γPπVT^\pi V = r^\pi + \gamma P^\pi V.

Convergence proof via martingale theory: Write Vt=V+εtV_t = V^* + \varepsilon_t (error from true value). Then:

εt+1(st)=(1αt)εt(st)+αt[γPπεt(st)+Mt+1]\varepsilon_{t+1}(s_t) = (1 - \alpha_t)\varepsilon_t(s_t) + \alpha_t[\gamma P^\pi \varepsilon_t(s_t) + M_{t+1}]

where Mt+1=rt+γV(st+1)V(st)M_{t+1} = r_t + \gamma V^*(s_{t+1}) - V^*(s_t) is a martingale difference noise term. Under the Robbins-Monro conditions αt=\sum\alpha_t = \infty, αt2<\sum\alpha_t^2 < \infty, the ODE method (Borkar, 2008) shows εt0\varepsilon_t \to 0 a.s.


Appendix V: Practical MCMC Checklist for AI/ML

Before running MCMC:

  • Identify the target: is π(θD)\pi(\theta | \mathcal{D}) proper? Is logπ\log\pi evaluable efficiently?
  • Choose algorithm: log-concave -> Langevin/HMC; multimodal -> parallel tempering; discrete -> Gibbs
  • Set proposal scale: tune to ~23% acceptance (MH) or ~65% (HMC)
  • Run multiple chains (4\geq 4) from diverse starting points

During MCMC:

  • Monitor trace plots for signs of non-stationarity (trends, sticking)
  • Track acceptance rate: should be in target range
  • Compute R^\hat{R} after every 1000 iterations; stop when R^1.01\hat{R} \leq 1.01

After MCMC:

  • Discard burn-in (first 50% as rule of thumb)
  • Compute ESS per parameter; target ESS 400\geq 400 for reliable estimates
  • Check posterior predictive checks: do simulated data match observed data?
  • Report: posterior mean, posterior std, 95% credible interval, ESS, R^\hat{R}

Common pathologies:

  • Divergent transitions (HMC): posterior geometry has sharp ridges; reparameterise
  • Low ESS: chain mixes slowly; increase step size, use HMC, or apply preconditioning
  • R^>1.1\hat{R} > 1.1: chains haven't agreed; run longer, use better initialisation
  • Bimodal trace plots: chain stuck between modes; use parallel tempering

Appendix W: Worked Computation - Two-State Chain

Full Analysis

Let P=(0.70.30.50.5)P = \begin{pmatrix}0.7 & 0.3 \\ 0.5 & 0.5\end{pmatrix}, starting from μ(0)=(1,0)\mu^{(0)} = (1, 0).

Step 1: Eigenvalues. Characteristic polynomial: det(PλI)=(0.7λ)(0.5λ)0.3×0.5=λ21.2λ+0.20=(λ1)(λ0.2)=0\det(P - \lambda I) = (0.7-\lambda)(0.5-\lambda) - 0.3 \times 0.5 = \lambda^2 - 1.2\lambda + 0.20 = (\lambda-1)(\lambda-0.2) = 0. Eigenvalues: λ1=1,λ2=0.2\lambda_1=1, \lambda_2=0.2.

Step 2: Stationary distribution. From πP=π\pi P = \pi: 0.7π1+0.5π2=π1π2=0.6π10.7\pi_1 + 0.5\pi_2 = \pi_1 \Rightarrow \pi_2 = 0.6\pi_1. With π1+π2=1\pi_1+\pi_2=1: π=(5/8,3/8)\pi = (5/8, 3/8).

Step 3: nn-step distribution. Using the spectral decomposition:

Pn=π+(0.2)n(correction term)P^n = \pi + (0.2)^n \cdot \text{(correction term)}

Explicitly: Pn=(5/83/85/83/8)+(0.2)n(3/83/85/85/8)P^n = \begin{pmatrix}5/8 & 3/8 \\ 5/8 & 3/8\end{pmatrix} + (0.2)^n\begin{pmatrix}3/8 & -3/8 \\ -5/8 & 5/8\end{pmatrix}

So μ(n)=μ(0)Pn=(5/8+3/8(0.2)n, 3/83/8(0.2)n)\mu^{(n)} = \mu^{(0)}P^n = (5/8 + 3/8 \cdot (0.2)^n,\ 3/8 - 3/8 \cdot (0.2)^n).

Step 4: TV distance. μ(n)πTV=123/8(0.2)n+123/8(0.2)n=3/8(0.2)n\|\mu^{(n)} - \pi\|_{\text{TV}} = \frac{1}{2}|{3/8 \cdot (0.2)^n}| + \frac{1}{2}|{3/8 \cdot (0.2)^n}| = 3/8 \cdot (0.2)^n.

At n=5n=5: 3/8×0.00032=0.000123/8 \times 0.00032 = 0.00012 - essentially converged. Spectral gap =10.2=0.8= 1 - 0.2 = 0.8, so mixing is fast.

Step 5: Verify detailed balance.

π1P12=5/8×0.3=0.1875,π2P21=3/8×0.5=0.1875\pi_1 P_{12} = 5/8 \times 0.3 = 0.1875, \quad \pi_2 P_{21} = 3/8 \times 0.5 = 0.1875 \checkmark

The chain is reversible with this stationary distribution.


Appendix X: Advanced Topics

X.1 Geometric Random Walks for Volume Computation

The ball walk and Gaussian walk are Markov chains used to estimate the volume of convex bodies. Starting from a point xx in a convex body KK, the chain moves to a uniformly random point within a ball of radius rr centred at xx (rejecting if outside KK). The stationary distribution is uniform on KK.

Mixing time analysis: tmix=O(n2poly(1/ε))t_{\text{mix}} = O(n^2 \text{poly}(1/\varepsilon)) for the ball walk, where nn is the dimension. This gives a polynomial-time randomised algorithm for volume computation (Dyer-Frieze-Kannan theorem).

For AI: High-dimensional sampling problems (Bayesian inference with d109d \sim 10^9 parameters) require understanding how mixing time scales with dimension. Geometric random walks provide the fundamental tools.

X.2 Markov Chain Tree Theorem

For a strongly connected directed graph with transition matrix PP, the stationary distribution can be computed via spanning trees:

πi=τijτj\pi_i = \frac{\tau_i}{\sum_j \tau_j}

where τi\tau_i is the sum of weights of all spanning trees directed toward node ii (arborescences rooted at ii). This is the Matrix-Tree theorem (Kirchhoff 1847) extended to directed graphs.

Implication: The stationary distribution has a combinatorial formula in terms of the graph structure. This provides an alternative to eigendecomposition that can be faster for sparse graphs.

X.3 Lumping and Aggregation

A partition {C1,,Cm}\{C_1,\ldots,C_m\} of S\mathcal{S} is lumpable with respect to PP if for any two states i,ji, j in the same class CkC_k, the aggregated transitions CmPi=CmPj\sum_{\ell \in C_m} P_{i\ell} = \sum_{\ell \in C_m} P_{j\ell} for all classes CmC_m. In this case, the quotient chain Pˉ\bar{P} on {C1,,Cm}\{C_1,\ldots,C_m\} is a well-defined Markov chain.

Lumping enables dimensionality reduction: replace a large chain with a smaller aggregated chain. The stationary distribution of the lumped chain aggregates that of the original.

For AI: Grouping similar states (e.g., tokens with similar embeddings) can define a lumped chain that captures the high-level dynamics of the LLM's token distribution, enabling efficient analysis of LLM behaviour at scale.


Appendix Y: Connections to Physics

Y.1 Statistical Mechanics and Gibbs Measures

The Gibbs distribution π(x)eH(x)/(kBT)\pi(x) \propto e^{-H(x)/(k_BT)} (Boltzmann distribution) is the equilibrium distribution of a physical system with Hamiltonian HH at temperature TT. This is the canonical target distribution for MCMC in physics.

The Metropolis algorithm was originally designed to sample from Gibbs distributions of particle systems (Metropolis et al., 1953). MCMC methods in ML directly descended from statistical mechanics - including the energy-based models (EBMs) that predate modern deep learning.

Temperature annealing: Starting at high TT (flat, easy-to-sample distribution) and gradually cooling to T=0T=0 (greedy optimisation) is simulated annealing - a probabilistic optimisation algorithm. The connection between MCMC sampling and optimisation at T0T \to 0 is the bridge between Bayesian inference and MAP estimation.

Y.2 Detailed Balance and Thermodynamic Equilibrium

Detailed balance in physics is called microscopic reversibility or time-reversal symmetry. A physical system is at thermodynamic equilibrium iff its microscopic dynamics satisfy detailed balance. Systems out of equilibrium (driven by energy flows) violate detailed balance and maintain directed probability currents.

Non-equilibrium steady states: Some biological systems (motor proteins, gene regulatory networks) maintain non-reversible Markov chains in steady state - driven by ATP hydrolysis. These are "out-of-equilibrium" in the thermodynamic sense. Non-reversible MCMC is inspired by this physics.

Y.3 Renormalization Group and Multi-Scale Chains

The renormalization group (Wilson, 1971) is a technique in physics for coarse-graining: replacing fine-grained microscopic degrees of freedom with effective coarse-grained ones. This is mathematically analogous to the lumping/aggregation of Markov chains.

In ML, knowledge distillation (training a small model to mimic a large model) is a form of renormalization: the small model's transition matrix approximates the "effective" dynamics of the large model at a coarser scale.


Skill Check

Test this lesson

Answer 4 quick questions to lock in the lesson and feed your adaptive practice queue.

--
Score
0/4
Answered
Not attempted
Status
1

Which module does this lesson belong to?

2

Which section is covered in this lesson content?

3

Which term is most central to this lesson?

4

What is the best way to use this lesson for real learning?

Your answers save locally first, then sync when account storage is available.
Practice queue