Markov Chains
A Markov chain is a random process whose next state depends on the present state but not on the earlier history. This "memoryless given the present" property is discrete-time cousin of the exponential waiting-time idea. Instead of one random variable, a Markov chain is a sequence moving through a state space.
Figure: A finite-state machine represents computation as transitions among discrete states. Image: Wikimedia Commons, Macguy314, reworked by Perhelion, public domain.
MIT 18.440 introduces finite-state Markov chains through examples such as weather models, then discusses ergodicity and stationarity. The main long-run question is whether the chain forgets its starting state and settles into stable state frequencies. Transition matrices make the calculations concrete.
Definitions
Let the finite state space be
A sequence is a Markov chain if there are transition probabilities such that
The matrix
is the transition matrix. Each row sums to :
If the distribution of is represented as a row vector , then
A distribution is stationary if
A finite Markov chain is ergodic in the lecture's sense if some power of its transition matrix has all positive entries. This condition implies the chain can eventually move from any state to any other state with positive probability and avoids periodic obstructions.
Key results
The -step transition probabilities are entries of :
This follows from repeated conditioning and matrix multiplication:
which sums over the possible intermediate state after one step.
If the initial distribution is stationary, then every later distribution is the same:
For an ergodic finite Markov chain, there is a unique stationary distribution , and the distribution of converges to regardless of the starting state:
The stationary distribution is not necessarily uniform. It reflects both how often states are entered and how long the chain tends to remain there.
Expected hitting times are often computed by first-step equations. If is the expected time to hit a target state starting from , then
for non-target states, with on target states.
The Markov property does not say the process has no memory in an absolute sense. It says the current state contains all information from the past that is relevant for predicting the next state. If the state variable is chosen poorly, the Markov property may fail. For example, a model of relationship status as simply "single" or "married" may be unrealistic if the duration of the current relationship affects the chance of transition; adding duration to the state can sometimes restore a Markov description.
Transition matrices can be read row by row. Row is the probability distribution of the next state, conditional on being in state now. Matrix powers combine steps. The entry is the probability of being in state three steps later, starting from , after summing over all possible two-step intermediate paths.
Stationarity is an equilibrium for distributions, not necessarily for individual paths. A chain started from still moves randomly from state to state, but the marginal distribution at each time remains . This is like shuffling mass among states in such a way that the total amount of mass in each state is restored after each transition.
Ergodicity is what turns stationarity into long-run prediction. If a chain is reducible, different starting classes may lead to different limiting behavior. If it is periodic, the distribution can oscillate rather than settle. The lecture's condition that some power of has all positive entries rules out both problems in the finite setting.
Markov chains also connect to the law of large numbers. In an ergodic finite chain, the fraction of time spent in each state converges to the stationary probability of that state. This is not the independent-sample law of large numbers, because the states are dependent, but it expresses a similar long-run averaging principle for a dependent process.
Visual
This Markov-chain architecture shows both a concrete weather state machine and the linear-algebra pipeline behind it. The transition matrix turns current distributions into future distributions, while flow balance gives the stationary distribution for the two-state example. The absorbing-chain subgraph adds the standard block form and fundamental matrix used when probability mass eventually enters terminal states.
| Concept | Formula | Meaning |
|---|---|---|
| Markov property | next depends only on current state | history is summarized by present |
| Transition matrix | chance to move to | |
| Distribution update | one-step evolution | |
| Stationarity | unchanged by transition | |
| Ergodicity | some has positive entries | long-run forgetting of start |
The weather diagram shows two different kinds of persistence. A rainy day has probability of being followed by another rainy day, while a sunny day has probability of being followed by another sunny day. This asymmetry is why the stationary distribution puts more mass on sun. Stationarity is not found by averaging the four transition probabilities; it is found by balancing the long-run flow of probability between the states.
In a two-state chain, the stationary equations can often be read as a flow balance. The long-run probability flow from rain to sun is , and the flow from sun to rain is . At equilibrium these flows match. This gives in the worked example, the same equation obtained from .
When the state space is larger, the same ideas remain but the algebra becomes linear algebra. Solving for stationarity means finding a left eigenvector of with eigenvalue and normalizing it to sum to one. Numerical computation is often straightforward, but interpretation still requires checking whether the chain is irreducible, aperiodic, and appropriate for the modeled system. A correct matrix calculation cannot rescue a poor state description.
Worked example 1: stationary weather distribution
Problem: A weather chain has states for rainy and for sunny. If today is rainy, tomorrow is rainy with probability and sunny with probability . If today is sunny, tomorrow is rainy with probability and sunny with probability . Find the stationary distribution.
Method:
- With state order , the transition matrix is
- Let . Stationarity means
- The first coordinate equation is
- Rearrange:
- Use :
- Then
Checked answer: the long-run fraction of sunny days is , larger than rainy because sunny days are more persistent.
Worked example 2: expected waiting time to sun
Problem: In the same weather chain, starting from a rainy day, what is the expected number of days until the first sunny day?
Method:
- Let be the expected waiting time to hit starting from .
- Let because we are already sunny.
- From , after one day the chain is sunny with probability and rainy again with probability .
- First-step equation:
- Substitute :
- Solve:
Checked answer: rainy persists with probability each day, so the wait for sun is geometric with success probability and mean days.
Code
import numpy as np
P = np.array([[0.5, 0.5],
[0.2, 0.8]])
# Stationary distribution solves (P.T - I) pi = 0 with sum pi = 1.
A = np.vstack([P.T - np.eye(2), np.ones(2)])
b = np.array([0.0, 0.0, 1.0])
pi, *_ = np.linalg.lstsq(A, b, rcond=None)
print("stationary distribution:", pi)
mu = np.array([1.0, 0.0]) # start rainy
for _ in range(20):
mu = mu @ P
print("distribution after 20 days:", mu)
h_r = 1 / 0.5
print("expected days from rain to sun:", h_r)
Common pitfalls
- Confusing independence with the Markov property. Markov chains usually have dependent consecutive states.
- Multiplying transition matrices in the wrong orientation. This page uses row distributions and .
- Assuming the stationary distribution is uniform because the state space is finite.
- Forgetting to impose when solving .
- Assuming every finite chain converges to a stationary distribution from every start. Periodicity and reducibility can obstruct convergence.