Markov chains: discrete time & transition matrices

Week 2 — Markov chains

Quantitative biology and integrative omics

About this lecture

A DNA sequence reads ...A C G T A A C .... What is the probability the next base is a G, given the current base is a C? A model that answers this is a Markov chain. These have extremely many uses in biology.

  • States and the Markov property (the future depends only on the present).
  • The transition matrix P — and why its rows sum to 1.
  • Drawing the state diagram.
  • One step as a matrix–vector product \mathbf{x}_{t+1} = \mathbf{x}_t P; many steps as powers of P.
  • How to estimate P from a real sequence by counting transitions.

Here we first cover the concept + formulation. In Markov chains in R we solve these chains in R and find their long-run behaviour.

The next base depends only on the current base

Imagine walking along a genome one base at a time. At each position you are in one of four states: \{\,\mathtt{A},\ \mathtt{C},\ \mathtt{G},\ \mathtt{T}\,\}

A first-order Markov model of the sequence says:

the probability of the next base depends only on the current base — not on the whole history before it.

  • Current base C → some chance of A, C, G, T next.
  • It does not matter what came three bases ago.

This “memorylessness” is the Markov property. It is an assumption (a model) — real genomes have longer-range structure — but a remarkably useful one.

States and the Markov property

A Markov chain is a sequence of random states X_0, X_1, X_2, \dots over a finite set of states, where

P(X_{t+1} = j \mid X_t = i,\ X_{t-1}, \dots, X_0) = P(X_{t+1} = j \mid X_t = i).

In words: only the present state matters. The number P(X_{t+1}=j \mid X_t=i) is the transition probability from state i to state j, written p_{ij}.

A second biology example — cell states: \{\,\text{stem},\ \text{progenitor},\ \text{differentiated}\,\} At each time step a cell may stay or move to another state, depending only on where it is now.

The transition matrix P

Collect every p_{ij} into a square matrix. For DNA, P is 4\times4 over A,C,G,T:

P = \begin{array}{c c} & \begin{array}{cccc}\ \mathtt{A} & \ \mathtt{C} & \ \mathtt{G} & \ \mathtt{T}\end{array}\\ \begin{array}{c}\mathtt{A}\\ \mathtt{C}\\ \mathtt{G}\\ \mathtt{T}\end{array} & \left(\begin{array}{cccc} p_{AA} & p_{AC} & p_{AG} & p_{AT}\\ p_{CA} & p_{CC} & p_{CG} & p_{CT}\\ p_{GA} & p_{GC} & p_{GG} & p_{GT}\\ p_{TA} & p_{TC} & p_{TG} & p_{TT} \end{array}\right) \end{array}

Our convention: entry in row i, column j is p_{ij} = probability of going from i (row) to j (column).

Rows sum to 1

From the current state i, the next base must be one of A, C, G, T. So the probabilities leaving state i are a complete probability distribution:

\sum_{j} p_{ij} = 1 \qquad \text{for every row } i.

A matrix that is (i) non-negative and (ii) has every row summing to 1 is called a stochastic matrix (or row-stochastic).

P = \begin{pmatrix} 0.3 & 0.2 & 0.2 & 0.3\\ 0.1 & 0.4 & 0.4 & 0.1\\ 0.1 & 0.4 & 0.4 & 0.1\\ 0.3 & 0.2 & 0.2 & 0.3 \end{pmatrix} \quad\leftarrow\ \text{each row adds to } 1

A state diagram holds the same information as the matrix

A Markov chain is often drawn as a directed graph: one node per state, one arrow per non-zero transition, labelled with its probability.

A tiny 2-state weather-style example for cells — resting (R) vs dividing (D):

P = \begin{pmatrix} 0.9 & 0.1\\ 0.5 & 0.5 \end{pmatrix}

  • Arrow R→D has weight 0.1
  • The self-loop R→R has weight 0.9
  • Row R: 0.9+0.1=1. As expected! ✓
  • The diagram and the matrix carry exactly the same information.

A probability vector tracks the share in each state

Instead of tracking one walker, track a probability distribution over states. Write it as a row vector:

\mathbf{x}_t = \big(\,x_t^{A},\ x_t^{C},\ x_t^{G},\ x_t^{T}\,\big), \qquad \sum_k x_t^{k} = 1.

  • \mathbf{x}_t = (1,0,0,0) means “currently in state A for sure.”
  • \mathbf{x}_t = (0.25,0.25,0.25,0.25) means “equally likely to be any base.”

Think of it as the composition of a huge pool of independent walkers: the fraction in each state.

Multiplying by P moves the distribution one step

To advance one step, multiply the state row vector by P (vector on the left):

\mathbf{x}_{t+1} = \mathbf{x}_t\, P.

Each new entry is a dot product (Matrices as transformations) — the chance of landing in state j, summed over where you came from:

x_{t+1}^{\,j} = \sum_i x_t^{\,i}\, p_{ij}.

Worked 2-state step with P=\begin{pmatrix}0.9&0.1\\0.5&0.5\end{pmatrix} and \mathbf{x}_0=(1,0) (start resting):

\mathbf{x}_1 = (1,0)P = \big(1\cdot0.9 + 0\cdot0.5,\ \ 1\cdot0.1 + 0\cdot0.5\big) = (0.9,\ 0.1).

Many steps: powers of P

Apply the rule repeatedly:

\mathbf{x}_2 = \mathbf{x}_1 P = (\mathbf{x}_0 P)P = \mathbf{x}_0 P^2, \qquad \mathbf{x}_t = \mathbf{x}_0\, P^{t}.

So t steps = the t-th power of P. Continuing the example:

\mathbf{x}_2 = (0.9,0.1)P = (0.9\cdot0.9 + 0.1\cdot0.5,\ \ 0.9\cdot0.1 + 0.1\cdot0.5) = (0.86,\ 0.14).

The entry (P^t)_{ij} is the probability of going from state i to state j in exactly t steps.

Try it: 200 walkers on the R/D chain

Estimating P from data: count transitions

Where does P come from? Count consecutive pairs in an observed sequence. For A C G T A A C:

pair A→C C→G G→T T→A A→A A→C

Tally each ordered pair into a 4\times4 count matrix N, where N_{ij} = number of times i is immediately followed by j. Then normalise each row:

\hat p_{ij} = \frac{N_{ij}}{\sum_k N_{ik}}.

This is the maximum-likelihood estimate: the observed fraction of i-followed-by-j among all transitions out of i.

Estimating P: a tiny worked count

Sequence A C G T A A C has 6 transitions (one per adjacent pair):

A→C, C→G, G→T, T→A, A→A, A→C

Count matrix N (rows = from, cols = to):

N = \begin{array}{cc} & \begin{array}{cccc}\mathtt{A}&\mathtt{C}&\mathtt{G}&\mathtt{T}\end{array}\\ \begin{array}{c}\mathtt{A}\\\mathtt{C}\\\mathtt{G}\\\mathtt{T}\end{array} &\left(\begin{array}{cccc} 1 & 2 & 0 & 0\\ 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 1\\ 1 & 0 & 0 & 0 \end{array}\right) \end{array}

Row A has total 3 → \hat p_{A\bullet} = (1/3,\ 2/3,\ 0,\ 0). Rows C, G, T each have a single count → those rows are (0,0,1,0), (0,0,0,1), (1,0,0,0).

Summary

Concept Key fact Direction / form
State one of A,C,G,T (or a cell state) —
Markov property future depends only on present P(X_{t+1}\mid X_t,\dots)=P(X_{t+1}\mid X_t)
Transition prob. p_{ij} = from i to j row i, column j
Stochastic matrix every row sums to 1 \sum_j p_{ij}=1
One step \mathbf{x}_{t+1}=\mathbf{x}_t P row vector on the left
t steps \mathbf{x}_t = \mathbf{x}_0 P^t powers of P
Estimate P count pairs, normalise rows \hat p_{ij}=N_{ij}/\sum_k N_{ik}

Next (Markov chains in R): build P in R, take powers, and find the stationary distribution \boldsymbol\pi where \boldsymbol\pi P = \boldsymbol\pi.