St · Statistics & Probability

How to Calculate Probabilities in a Markov Chain

Published By the 9to5Science editorial team

At a glance

  1. 1. Define the states

    List every possible state and decide what one time step represents.

  2. 2. Build the transition matrix

    Place each probability of moving from a current state to a next state in the appropriate matrix entry.

  3. 3. Choose the initial distribution

    Represent the starting state or uncertainty about the starting state as a probability vector.

  4. 4. Advance the chain

    Multiply by the transition matrix once per time step, or use a matrix power.

  5. 5. Extract the desired probability

    Read the relevant component or sum the components for the target states.

A Markov chain models a process that moves between states in discrete time. Its defining property is that the distribution of the next state depends on the current state, not on the full history:

Pr⁡(Xn+1=j∣Xn=i,Xn−1,…,X0)=Pr⁡(Xn+1=j∣Xn=i).\Pr(X_{n+1}=j\mid X_n=i,X_{n-1},\ldots,X_0)=\Pr(X_{n+1}=j\mid X_n=i).

The phrase “calculate the probability” can mean several things: the probability of a state after a fixed number of steps, the probability of one particular path, the probability of ever reaching a state, or the long-run proportion of time spent in each state. The transition matrix gives the first two directly; the other questions require related equations.

Transition matrices

Suppose the chain has states 1,2,…,k1,2,\ldots,k. Using the common row-vector convention, define the transition matrix PP by

Pij=Pr⁡(Xn+1=j∣Xn=i).P_{ij}=\Pr(X_{n+1}=j\mid X_n=i).

Thus, row ii describes where the chain can go when it is currently in state ii. Every entry must satisfy 0≤Pij≤10\le P_{ij}\le 1, and every row must sum to 11:

∑j=1kPij=1.\sum_{j=1}^k P_{ij}=1.

If the current-state distribution is the row vector

πn=[Pr⁡(Xn=1)Pr⁡(Xn=2)⋯Pr⁡(Xn=k)],\boldsymbol{\pi}_n= \begin{bmatrix} \Pr(X_n=1)&\Pr(X_n=2)&\cdots&\Pr(X_n=k) \end{bmatrix},

then the next distribution is

πn+1=πnP.\boldsymbol{\pi}_{n+1}=\boldsymbol{\pi}_nP.

After nn steps,

πn=π0Pn.\boldsymbol{\pi}_n=\boldsymbol{\pi}_0P^n.

If the chain starts in state ii with certainty, π0\boldsymbol{\pi}_0 has a 11 in position ii and zeros elsewhere. The probability of being in state jj after nn steps is the jjth component of π0Pn\boldsymbol{\pi}_0P^n.

Worked example: state after three steps

Consider a two-state weather chain with states Sunny (SS) and Rainy (RR). Suppose

  • after a sunny day, the next day is sunny with probability 0.80.8 and rainy with probability 0.20.2;
  • after a rainy day, the next day is sunny with probability 0.40.4 and rainy with probability 0.60.6.

With the state order (S,R)(S,R),

P=[0.80.20.40.6].P= \begin{bmatrix} 0.8&0.2\\ 0.4&0.6 \end{bmatrix}.

Assume today is sunny, so

π0=[10].\boldsymbol{\pi}_0=\begin{bmatrix}1&0\end{bmatrix}.

The required probability that it rains three days from today is the second component of π0P3\boldsymbol{\pi}_0P^3.

First calculate the powers of PP:

P2=[0.80.20.40.6][0.80.20.40.6]=[0.720.280.560.44],P^2= \begin{bmatrix} 0.8&0.2\\ 0.4&0.6 \end{bmatrix} \begin{bmatrix} 0.8&0.2\\ 0.4&0.6 \end{bmatrix} = \begin{bmatrix} 0.72&0.28\\ 0.56&0.44 \end{bmatrix},

and

P3=P2P=[0.6880.3120.6240.376].P^3=P^2P= \begin{bmatrix} 0.688&0.312\\ 0.624&0.376 \end{bmatrix}.

Therefore,

π3=π0P3=[10][0.6880.3120.6240.376]=[0.6880.312].\boldsymbol{\pi}_3=\boldsymbol{\pi}_0P^3 =\begin{bmatrix}1&0\end{bmatrix} \begin{bmatrix} 0.688&0.312\\ 0.624&0.376 \end{bmatrix} =\begin{bmatrix}0.688&0.312\end{bmatrix}.

The probability of rain three days from today is therefore 0.3120.312, or 31.2%.

The entry (Pn)ij(P^n)_{ij} has a useful direct interpretation: it is the probability of being in state jj after nn steps, given that the chain started in state ii.

Probability of one particular path

If you want the probability of a specified sequence of states, multiply the transition probabilities along the path. For example, in the weather chain, the path

S→R→R→SS\to R\to R\to S

has probability

Pr⁡(S,R,R,S∣X0=S)=PSRPRRPRS=(0.2)(0.6)(0.4)=0.048.\Pr(S,R,R,S\mid X_0=S) =P_{SR}P_{RR}P_{RS} =(0.2)(0.6)(0.4)=0.048.

So this particular three-transition path has probability 4.8%4.8\%.

Do not confuse this with the probability of being sunny after three steps. Many different paths end in SS, and their probabilities must be added. Matrix multiplication performs that summation automatically.

If the starting state is uncertain

Suppose the weather is sunny with probability 0.70.7 and rainy with probability 0.30.3. Then

π0=[0.70.3].\boldsymbol{\pi}_0=\begin{bmatrix}0.7&0.3\end{bmatrix}.

The distribution after three days is

π3=π0P3=[0.70.3][0.6880.3120.6240.376]=[0.66880.3312].\boldsymbol{\pi}_3=\boldsymbol{\pi}_0P^3 =\begin{bmatrix}0.7&0.3\end{bmatrix} \begin{bmatrix}0.688&0.312\\ 0.624&0.376 \end{bmatrix} =\begin{bmatrix}0.6688&0.3312\end{bmatrix}.

Thus the probability of rain after three steps is 0.33120.3312. An uncertain initial state is handled by averaging over the possible starting states through the initial distribution vector.

Hitting or reaching probabilities

“Will the chain ever reach state AA?” is different from “where is it after nn steps?” For a finite chain with absorbing or target states, define

hi=Pr⁡(eventually reach A∣X0=i).h_i=\Pr(\text{eventually reach }A\mid X_0=i).

Then set hA=1h_A=1. If another state BB cannot reach AA, set hB=0h_B=0. For every other state, use first-step analysis:

hi=∑jPijhj.h_i=\sum_j P_{ij}h_j.

These simultaneous linear equations produce the eventual hitting probabilities. A matrix power answers a fixed-time question; it does not by itself answer an “ever” question.

Long-run probabilities

A stationary distribution is a probability vector π\boldsymbol{\pi} satisfying

πP=π,∑iπi=1.\boldsymbol{\pi}P=\boldsymbol{\pi}, \qquad \sum_i\pi_i=1.

When a finite chain is irreducible and aperiodic, the distribution often converges to this stationary distribution regardless of the initial state. This describes long-run behavior, not necessarily the probability at a particular finite time.

For the weather example, write π=(p,1−p)\boldsymbol{\pi}=(p,1-p). Solving πP=π\boldsymbol{\pi}P=\boldsymbol{\pi} gives

p=0.8p+0.4(1−p),p=0.8p+0.4(1-p),

so p=2/3p=2/3. The stationary distribution is therefore (2/3,1/3)(2/3,1/3): in the long run, about two-thirds of days are sunny and one-third rainy.

Common mistakes

  1. Reversing the matrix convention. With row vectors, use πn+1=πnP\boldsymbol{\pi}_{n+1}=\boldsymbol{\pi}_nP. With column vectors, the formula becomes πn+1=Pπn\boldsymbol{\pi}_{n+1}=P\boldsymbol{\pi}_n, but the matrix entries must be arranged consistently.
  2. Using a single transition probability for multiple steps. The probability of rain tomorrow is not generally the probability of rain two days from now. Use P2P^2 or sum over the possible intermediate states.
  3. Multiplying all possible paths when asking for a final state. Multiply along each path, then add the probabilities of all paths ending in the target state.
  4. Confusing stationary and finite-time probabilities. A stationary distribution describes equilibrium behavior; it is not automatically the distribution after one or two steps.
  5. Ignoring the initial distribution. The same transition matrix can give different answers depending on where the chain starts.

References

  1. Markov Chains, J. R. Norris
  2. Introduction to Probability Models, Sheldon M. Ross

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.