Markov chain

Also known as: Markov process

On this page 8

A Markov chain is a stochastic process over a set of states in which the probability of moving to the next state depends only on the current state, not on the sequence of states that preceded it. This memoryless property, called the Markov property, makes the Markov chain one of the foundational probabilistic models in artificial intelligence, underlying sampling algorithms, sequence models and reinforcement learning.

The term Markov process is often used as a synonym, although many authors reserve it for processes in continuous time or continuous state spaces. In machine learning, the discrete-time chain with a finite or countable state space is the most common form, and it serves as the basis for hidden Markov models, Markov chain Monte Carlo and Markov decision processes.

How a Markov chain evolves from state to state

A discrete-time Markov chain is fully specified by a state space, an initial distribution and a transition matrix. The transition matrix P holds the probability P[i][j] of moving from state i to state j in one step, and every row sums to exactly 1.

Formally, the Markov property states that P(X_{t+1} = j | X_t = i, X_{t-1}, ..., X_0) = P(X_{t+1} = j | X_t = i). Because of this property, the distribution after n steps is obtained by multiplying the initial distribution by P raised to the power n, which turns questions about long sequences into linear algebra.

The table below lists the components that define a chain and the role each one plays.

ComponentNotationRole
State spaceSSet of all possible states
Initial distributionπ0Probabilities of the starting state
Transition matrixPOne-step probabilities between states
n-step transitionP to the power nProbabilities after n steps
Stationary distributionπ with πP = πLong-run share of time in each state
Components of a discrete-time Markov chain

A chain is time-homogeneous when P does not change over time. Time-inhomogeneous chains, in which transition probabilities depend on t, appear in simulated annealing and in some sequence models, but most theory assumes homogeneity.

Key properties: irreducibility, periodicity and stationarity

The long-run behavior of a Markov chain is governed by a small set of structural properties. Together they determine whether the chain settles into a unique stationary distribution regardless of where it starts.

PropertyMeaningWhy it matters
IrreducibleEvery state can reach every other stateRules out isolated groups of states
AperiodicReturns are not locked to a fixed cyclePrevents oscillation between state groups
Positive recurrentExpected return time is finiteGuarantees a stationary distribution exists
ErgodicIrreducible, aperiodic and positive recurrentConvergence to a unique stationary distribution
ReversibleSatisfies detailed balanceSimplifies the design of MCMC samplers
Structural properties of Markov chains

For an ergodic chain, the distribution of states converges to π from any starting point, and time averages along a single long run equal expectations under π. This ergodic theorem is exactly what Markov chain Monte Carlo relies on to estimate posterior expectations from samples.

The speed of convergence is described by the mixing time, which is closely related to the spectral gap, the difference between 1 and the second-largest eigenvalue modulus of P. A small spectral gap means slow mixing and strongly correlated samples.

Variants of the Markov chain

Several model families extend the basic chain by changing the time index, the order of memory or what is observed. Each variant keeps the Markov property in some form while adapting it to a different modeling need.

VariantKey differenceTypical use
Higher-order chainNext state depends on the last k statesn-gram language models
Continuous-time chainTransitions occur at random timesQueueing and reliability models
Hidden Markov modelStates are latent, only emissions are observedSpeech, sequence labeling, genomics
Markov decision processTransitions depend on chosen actionsReinforcement learning
Absorbing chainSome states cannot be left once enteredCustomer churn and time to failure
Common variants and their use in machine learning

A higher-order chain can always be rewritten as a first-order chain over tuples of states, but the state space then grows exponentially with the order, which is the core data-sparsity problem of classic n-gram models.

A Markov chain models a single observed sequence with first-order dependence, whereas neighboring models in the Topic add latent variables, undirected structure or inference machinery. Knowing the boundaries avoids using a chain where a richer model is required.

ModelStructureMain question answered
Markov chainDirected sequence of observed statesHow do states evolve over time
Hidden Markov modelLatent chain plus observation modelWhich hidden states explain the data
Markov random fieldUndirected graph with local dependenciesHow are neighboring variables jointly distributed
Dynamic Bayesian networkFactored state across time slicesHow do many variables evolve together
Markov chain Monte CarloChain designed to target a distributionHow to sample from a complex posterior
Markov chain versus neighboring models

In MCMC, the logic is reversed: instead of estimating P from data, algorithms such as the Metropolis-Hastings algorithm and Gibbs sampling construct P so that its stationary distribution equals the posterior of interest.

Estimating and applying Markov chains in practice

Transition probabilities are usually estimated by maximum likelihood, which reduces to counting transitions between each pair of states and normalizing each row. With sparse data, additive smoothing or a Dirichlet prior prevents unseen transitions from receiving a probability of exactly zero.

  • Define states carefully, because a poorly chosen state space violates the Markov property in practice.
  • Test the memoryless assumption by comparing first-order and higher-order fits.
  • Check stationarity of P over time before using one matrix for forecasting.
  • Run convergence diagnostics, such as the Gelman-Rubin statistic, when the chain is used for sampling.

Limitations and common misconceptions

The main limitation is the short memory assumption. Natural language, user behavior and financial series often have long-range dependencies that a first-order chain cannot capture, which is why transformers replaced n-gram models for language modeling.

A frequent misconception is that the Markov property means future states are independent of the past. They are dependent, but conditionally independent given the present. Another mistake is assuming every chain converges; a periodic or reducible chain may never reach a unique long-run distribution.

In simple terms

A Markov chain is a way to describe things that move between situations by chance. The key rule is simple. Where you go next depends only on where you are now.

Think of a board game played with a die. Your next square depends on your current square and the roll. It does not matter how you got there. With that rule, computers can predict how often each situation will occur over a long time.

A practical example

A subscription streaming service wants to forecast how customers move between plans. Analysts define four states: free trial, basic, premium and canceled, with canceled as an absorbing state. From twelve months of billing records, they count monthly transitions and estimate a 4 by 4 transition matrix.

The matrix shows, for example, that about 6 percent of basic subscribers cancel each month, while premium subscribers cancel at roughly half that rate. Using the absorbing-chain formulas, the team computes the expected number of months before cancellation for each starting plan. The results guide a retention offer aimed at basic subscribers in their third month, where the estimated cancellation risk is highest.