Interactive lab · stochastic matrices
Markov Chain Lab
A Markov chain is a directed graph of states whose edges carry probabilities. Collect them in a matrix P with Pij = Pr(next state is j | current state is i), and every row of P sums to 1. Build a chain below, compare one random walk with the evolving distribution, then find the stationary distribution two ways.
How it works and what to try
How it works: In three steps. (1) Build a chain of 3–8 states from a preset or by editing P; the graph redraws as you type. (2) Step a single walker through the chain alongside the distribution πt, and compare both with 1,000 independent walkers and with the walker’s time average. (3) Find the stationary π as the dominant eigenvector of PT, by power iteration (with the Rayleigh quotient and the eigenvalues plotted in the complex plane) and by stepping through Gaussian elimination on (PT − I)πT = 0.
Try this: Load Two clusters, slow mixing and see how slowly power iteration converges when |λ2| is close to 1. Then try Period 2, where xk oscillates instead of converging, and Reducible, where the stationary distribution is not unique.
The chain and its stochastic matrix
Each arrow i → j is a non-zero entry Pij, drawn thicker when the probability is larger. Hover a state, or click into a row of the matrix, to see the transitions out of that state. Edit any entry to change the chain; everything further down updates.
If a row does not sum to 1 it is rescaled before use, so the graph and the computations below always use a stochastic matrix.
A trajectory versus the distribution
A trajectory is one realisation X0, X1, X2, …: at each step the walker draws its next state from the row of P for the state it is in, so two runs differ. The distribution πt, with πt(i) = Pr(Xt = i), is not random: it evolves deterministically by πt+1 = πtP. Both start from the same state, so π0 puts all its mass there.
The shaded disc in each state has area proportional to πt(i).
Top: the states visited by the single walker. Bottom: each πt(i), one line per state. The walk keeps jumping; the distribution settles.
The 1,000 walkers track πt at every step: the distribution is what an ensemble of trajectories looks like. The single walker's time average only approaches the stationary π in the long run (the ergodic theorem, for an irreducible chain).
The stationary distribution as an eigenvector
optionalOptional. This step is for students who already have a good background in linear algebra, in particular eigenvalues, eigenvectors and Gaussian elimination. Steps 1 and 2 stand on their own without it.
We define π as a row vector where each element is the probability of being in the corresponding state. A stationary distribution satisfies πP = π, so π is a left eigenvector of P with eigenvalue 1. Because P is stochastic, no eigenvalue has modulus above 1. So 1 is the dominant eigenvalue, and π is the dominant left eigenvector.
Left and right eigenvectors. A left eigenvector multiplies the matrix from the left, xP = λx. A right eigenvector multiplies it from the right, Pv = λv. The two kinds share the same eigenvalues but not the same vectors. For eigenvalue 1, the right eigenvector of P is the all-ones vector: P1 = 1, because every row sums to 1. That holds for every stochastic matrix and says nothing about the chain. The left eigenvector is π.
Standard linear algebra works with right eigenvectors, and so do library routines such as NumPy's linalg.eig and MATLAB's eig. To get π from them, pass the transpose, eig(P.T): a left eigenvector of P is a right eigenvector of PT. SciPy's scipy.linalg.eig(P, left=True) returns left eigenvectors directly.
Reading πP = π one column at a time
Entry j of πP is Σi π(i) Pij. Column j of P lists the probabilities of moving into j, so the equation for state j says that the mass arriving at j equals the mass already at j. Hover over a column to see that balance for one state.
Method 1: power iteration and the Rayleigh quotient
Start from any row vector x0 and repeat xk+1 = xkP / ‖xkP‖1. The Rayleigh quotient ρk = xkP xkT / xkxkT estimates the eigenvalue and tends to 1. The T here only turns a row into a column, so that each product is a number. If x0 is written as a combination of left eigenvectors, the part along each one other than π shrinks like |λi|k. So xk → π, at a rate set by |λ2|.
If you start from a probability vector, xk is exactly πk from step 2. The update xkP is the same formula as πt+1 = πtP, and the normalisation changes nothing because xkP already sums to 1. So “the distribution settles” and “the dominant left eigenvector emerges” are the same fact.
Dots: the error at step k. Dashed line: |λ2|k scaled to the first error. The dots follow its slope.
Method 2: solve the linear equations
Written out, π(P − I) = 0 is n equations, one per state. The equation for state j takes its coefficients from column j of P − I, and the table below puts each equation on its own line. The equations are linearly dependent: each row of P − I sums to 0, so adding all the equations gives 0 = 0, and one of them carries no information. Replace the last one with the normalisation π(A) + π(B) + … = 1 and solve by Gaussian elimination. Step through the operations below.