Markov Chain Stationary Explorer

Transition matrix simulator with k-step evolution and stationary-state intuition.

Loading interactive simulation...

Lesson

The theory — Markov Chain Stationary Explorer

A distribution is stationary when one more step changes nothing: πP = π. It is a fixed point of the transition matrix — the chain still moves between states, but the proportions stop shifting.

What each symbol means

P
the transition matrix. Entry in row i, column j is the probability of moving from state i to state j, so every row must sum to 1 — which the grid above checks for you.
p(0)
the initial distribution: where the chain starts. The default (1, 0, 0) means it begins in state 1 with certainty.
p(k)
the distribution after k steps, obtained by multiplying by P once per step.
π
the stationary distribution — the one that satisfies πP = π.
k
how many steps to take, set above. The trajectory table shows every distribution from p(0) to p(k).

Where the formula comes from

  1. Write what stationarity demands: after one step the distribution is unchanged, so πP = π.
  2. Rearranged, that is π(P − I) = 0 — a system of linear equations, one per state. On its own it has infinitely many solutions, because any multiple of a solution is also one.
  3. Add the condition that makes it a probability distribution, Σπ = 1, and the answer is pinned down. For the default matrix it is exactly 8/13, 3/13, 2/13 — the 0.615385, 0.230769, 0.153846 reported above.

How to read what you see

Three panels. p(k) is where the chain actually is after your k steps; π is where it is heading, with the iteration count and whether it converged; and distance to stationarity measures the gap between them — 0.08893 at the default k = 8, which is why the note says the chain has not fully mixed yet. The trajectory table below shows every step, starting from (1, 0, 0).

Assumes
A finite set of states and probabilities that never change with time. Each row summing to 1 is not a formatting rule but the statement that the chain must go somewhere on every step.
Breaks when
The π above is labelled an estimate for a reason: it is reached by stepping the chain repeatedly — 83 iterations for the default matrix — and stopped when successive distributions stop moving. That is a numerical judgement, not a proof. The exact answer here is the set of rationals 8/13, 3/13, 2/13, and the printed decimals are those rounded to six places.

PageRank is this same calculation, run on the whole web 🖖

The stationary distribution is not only a textbook exercise — Google was founded on one. Treat every web page as a state and every link as a transition, and the stationary distribution of that enormous chain says how often a reader clicking links forever would land on each page. That is PageRank. The famous damping factor of 0.85, which sends the surfer to a random page 15% of the time, is not a heuristic tweak either: it exists to guarantee that every state can reach every other, which is exactly the condition that makes the stationary distribution unique and reachable in the first place. Without it, dead-end pages and closed loops would trap the walk and break the answer.

What the long run settles into 🖖

Each row of the transition matrix is just a set of odds: given you are in one state, how likely you jump to each other one on the next step. Multiply your current probability vector by P once per step and the numbers drift toward a fixed mix, the stationary distribution π. That mix tells you the fraction of time the system spends in each state over the long haul — try the weather sample and watch p(k) settle.

A unique target it never reaches 🖖

Set the two-state matrix to [[0,1],[1,0]] — a coin that always flips. It has a perfectly valid, unique stationary distribution π = (0.5, 0.5), yet start at (1, 0) and p(k) bounces 1,0 → 0,1 → 1,0 forever, never converging. Such periodic chains are exactly why the explorer can report converged: no; guaranteed convergence needs an aperiodic chain, not just a unique π.

Practice

Check yourself

Predict the answer first, then use the controls above to find out. Reveal only after you have committed to a guess — that is what makes it practice.

  1. The default starts the chain in state 1 with certainty, p(0) = (1, 0, 0). Change it to (0, 0, 1) so it starts in state 3 instead. Predict which readouts move and which do not.

    Show answer
    p(k) and the distance move; π and the iteration count do not. From state 1, eight steps reach 0.659852, 0.218714, 0.121436, a distance of 0.08893. From state 3 they reach 0.522304, 0.255274, 0.222421, a distance of 0.18616 — further out, because state 3 is the rarest destination. Both times π reads 0.615385, 0.230769, 0.153846 after the same 83 iterations. Where the chain starts decides how far along it is, never where it is heading: πP = π mentions the matrix and nothing else.
  2. Back to (1, 0, 0), and now raise the step count. How many steps before the distance to stationarity reads 0 — and has the chain then arrived?

    Show answer
    k = 80 prints 0, and no, it has not. At k = 40 the distance is already 0.000024; the six decimals the panel prints simply run out before the gap does. This chain approaches π geometrically and never reaches it in finitely many steps, so the 0 is the display rounding — the same reason π itself is labelled an estimate. Compare k = 8, where the distance is 0.08893 and the note still says the chain has not mixed.

Problem solved in full

  1. Eight steps of a three-state chain started in state 1 5 steps

    A three-state chain started entirely in state 1, run for eight steps. Work out where it is heading, and how far it still has to go.

    1. One step is a vector-matrix product: the chance of being in state j next is the sum over i of being in i now and moving i → j.

    2. Eight steps of that gives the distribution the panel prints. State 1 still holds two thirds of the probability, which is a lot given the chain started there with certainty.

    3. The destination is the stationary distribution — the one that a step leaves unchanged. Solving πP = π with the probabilities summing to 1 gives exact fractions, eighths and thirteenths, not decimals.

    4. Comparing the two gives the distance the panel reports, and it is not small: after eight steps the chain is still about 0.089 away in total.

    5. That gap shrinks geometrically, governed by the second-largest eigenvalue of P. Geometric decay means it halves in a fixed number of steps and never reaches zero in finitely many.

    Answer

    The tool prints 0.65985, 0.218714, 0.121436 after eight steps, still 0.08893 from stationarity. The stationary distribution is exactly (8/13, 3/13, 2/13), and the fact worth keeping is that it does not depend on where you started: those same three fractions are the limit from every starting distribution, which is what makes them a property of the transition matrix rather than of this particular run. That independence is what makes stationary distributions useful: PageRank, MCMC sampling and queue occupancy all rely on the long-run answer being a property of the transition rules alone. Only the time taken to get there depends on the start.

References (1)

Example problems

  • ergodic 3-state - Eight steps in and the readout still says 0.08893 away in L1. The stationary distribution it settles on — 0.615385, 0.230769, 0.153846 — is exactly 8/13, 3/13 and 2/13: a matrix written in tenths has a fixed point in thirteenths, because π comes out of solving πP = π rather than out of the entries you typed.
  • weather 2-state - 0.666667 and 0.333333, and for a two-state chain you never have to iterate for them. π is the two off-diagonal probabilities swapped and normalised: 0.4 / (0.2 + 0.4) = 2/3. From a 50/50 start, ten steps land within 0.000035 of it.
  • slow-mixing 3-state - Thirty steps here leave an L1 distance of 0.081995 — further from stationarity than the weather chain reached in ten, by a factor of more than two thousand. The diagonal is why: 0.97, 0.94, 0.96, so the chain almost never leaves the state it is in. It took 493 power iterations against the weather chain's 25, and π is 4/11, 4/11, 3/11.