Further reading: not part of the course. This material is not included in the course, but it is relevant if you want to learn more about the topic.

Hidden Markov Models

Further reading — Hidden Markov models

Quantitative biology and integrative omics

About this lecture

This is the last and hardest topic of the linear-algebra block. By the end you will be able to:

  • Explain why a visible Markov chain (Markov chains) is not enough when the thing you care about is hidden.
  • Name the four ingredients of a Hidden Markov Model (HMM): hidden states, a transition matrix, emission probabilities, and an initial distribution.
  • Tell the generative story of an HMM and read off the probability of a single hidden path with its observations.
  • State the three classic problems — likelihood, decoding, learning — and which algorithm solves each.
  • Compute the probability of one specific path for a tiny 2-state HMM by hand.

Notation: hidden states with transition matrix A, emission matrix B, initial distribution \boldsymbol{\pi}; hidden sequence \mathbf{z}, observations \mathbf{x}.

Recap: a visible Markov chain (Markov chains)

A Markov chain has states and a transition matrix A whose rows sum to 1. The Markov property: the next state depends only on the current one.

Example over DNA bases — suppose we modelled which base follows which:

A=\begin{array}{c c} & \begin{array}{cccc} \ \ A\ \ & \ \ C\ \ & \ \ G\ \ & \ \ T\ \ \end{array}\\ \begin{array}{c} A\\ C\\ G\\ T \end{array} & \left[\begin{array}{cccc} 0.3 & 0.2 & 0.3 & 0.2\\ 0.2 & 0.3 & 0.3 & 0.2\\ 0.2 & 0.3 & 0.3 & 0.2\\ 0.3 & 0.2 & 0.2 & 0.3 \end{array}\right] \end{array}

Crucially, in this chain the state IS the letter you read. You observe the state directly.

The biology: where is the CpG island?

Genomes are not uniform. Some stretches are GC-rich (“CpG-island-like”, often near gene starts); the rest is normal background.

You are handed a sequence:

... A T G C G C G C G G C C G C A T A T A A T T A C A ...
        |________ GC-rich? ________|  |__ normal? __|

You can see the letters (A, C, G, T). You cannot see the label “GC-rich” vs “normal” — that label is the biology you actually want.

The letters are emissions. The hidden region label is the hidden state.

One letter cannot reveal the hidden state

If the label “GC-rich vs normal” were written in the sequence, we would just read it — a plain Markov chain over labels (Markov chains).

But it is not written down. We only get the letters. Two different hidden states can emit the same letter:

  • A “GC-rich” region emits a G often.
  • A “normal” region emits a G too, just less often.

So a single G does not tell you the state. The state is hidden; we must infer it from the pattern of emissions over a whole stretch.

A hidden chain emits one visible letter per position

An HMM tells a generative story in two coupled layers:

hidden:   z1  ->  z2  ->  z3  ->  z4     (a Markov chain: GC-rich / normal)
           |       |       |       |
           v       v       v       v
visible:  x1      x2      x3      x4      (emitted bases: A C G T)
  • The hidden layer is a Markov chain over states (here: GC-rich, normal), governed by transition matrix A — the same chain we built in Markov chains.
  • At each hidden state, you draw one emission (a base) from that state’s emission distribution, governed by matrix B.

You only ever see the bottom row. The top row is hidden.

The four ingredients of an HMM

  1. Hidden states — the things you cannot observe. Here: \{\text{GC-rich},\ \text{normal}\}.
  2. Transition matrix A — how the hidden chain moves, rows sum to 1. A_{ij}=P(z_{t+1}=j \mid z_t=i). This is the Markov chain from Markov chains.
  3. Emission matrix B — what each hidden state emits, rows sum to 1. B_{ik}=P(x_t=k \mid z_t=i) (one row per state, one column per symbol).
  4. Initial distribution \boldsymbol{\pi} — where the chain starts, \pi_i=P(z_1=i), entries sum to 1.

An HMM is the triple (A, B, \boldsymbol{\pi}) plus the lists of states and symbols.

A tiny concrete 2-state HMM

States \{H, L\} — H = “GC-rich (high)”, L = “normal (low)”. Symbols: the bases \{A, C, G, T\}.

\boldsymbol{\pi}=\begin{bmatrix} 0.5 \\ 0.5 \end{bmatrix}, \qquad A=\begin{array}{cc} & \begin{array}{cc} H & L \end{array}\\ \begin{array}{c} H\\ L \end{array} & \left[\begin{array}{cc} 0.8 & 0.2 \\ 0.3 & 0.7 \end{array}\right] \end{array}

B=\begin{array}{c} \begin{array}{cccc} \ A\ & \ C\ & \ G\ & \ T\ \end{array}\\ \begin{array}{c} \end{array} \left[\begin{array}{cccc} 0.1 & 0.4 & 0.4 & 0.1 \\ 0.3 & 0.2 & 0.2 & 0.3 \end{array}\right] \end{array} \begin{array}{c} H\\ L \end{array}

Read B: state H emits C or G with prob 0.4 each (GC-rich!); state L spreads probability more evenly.

The generative story, step by step

To generate a sequence of length 3 from this HMM:

  1. Draw z_1 from \boldsymbol{\pi}. Say z_1 = H.
  2. Emit x_1 from row H of B. Say x_1 = G (prob 0.4).
  3. Move: draw z_2 from row H of A. Say z_2 = H (prob 0.8).
  4. Emit x_2 from row H of B. Say x_2 = C (prob 0.4).
  5. Move: draw z_3 from row H of A. Say z_3 = L (prob 0.2).
  6. Emit x_3 from row L of B. Say x_3 = A (prob 0.3).

We observe only \mathbf{x} = (G, C, A). The path \mathbf{z} = (H, H, L) is hidden.

A path’s probability is a product of its steps

The HMM is a product of independent choices, so multiply along the story. For path \mathbf{z}=(H,H,L) and observations \mathbf{x}=(G,C,A):

P(\mathbf{x},\mathbf{z}) = \underbrace{\pi_H}_{\text{start}}\, \underbrace{B_{H,G}}_{\text{emit}}\, \underbrace{A_{H,H}}_{\text{move}}\, \underbrace{B_{H,C}}_{\text{emit}}\, \underbrace{A_{H,L}}_{\text{move}}\, \underbrace{B_{L,A}}_{\text{emit}}

= 0.5 \times 0.4 \times 0.8 \times 0.4 \times 0.2 \times 0.3 = 0.003840.

The pattern: start prob → (emit, transition, emit, transition, …, emit). One \boldsymbol{\pi}, then alternate B and A, ending on an emission.

Three classic problems

Given an HMM and an observed sequence \mathbf{x}, three questions:

  1. Likelihood — how probable is \mathbf{x} under the model, P(\mathbf{x})? Summed over all hidden paths. → Forward algorithm.
  2. Decoding — what is the single most likely hidden path \mathbf{z}^\*? → Viterbi algorithm. (This is what predicts the islands.)
  3. Learning — given only unlabelled \mathbf{x}, estimate (A, B, \boldsymbol{\pi}). → Baum–Welch (an EM algorithm).

This unit: the concepts. Fitting HMMs in R: all three in R.

The likelihood adds up over every hidden path

The path (H,H,L) is one way to produce \mathbf{x}=(G,C,A). But (H,H,H), (L,H,L), … could also produce it.

The likelihood of the data is the sum over every hidden path:

P(\mathbf{x}) = \sum_{\text{all paths } \mathbf{z}} P(\mathbf{x}, \mathbf{z}).

For 2 states and length 3 that is 2^3 = 8 paths — fine by hand. For a real chromosome (2^{\text{millions}} paths) it is hopeless by brute force; the forward algorithm does it efficiently by reusing partial sums.

Viterbi finds the most likely hidden path

Decoding asks for the single best hidden path:

\mathbf{z}^\* = \arg\max_{\mathbf{z}} \; P(\mathbf{z}\mid \mathbf{x}).

The Viterbi algorithm finds it efficiently (same trick as forward, but keeping the best path instead of the total).

For islands: run Viterbi, then read off the runs of H — those are your predicted CpG islands. This turns a probabilistic model into a concrete genome annotation.

Try it: generate and decode a CpG island

Parameters are learned by counting, or by Baum–Welch

So far we assumed the numbers in A, B, \boldsymbol{\pi}. Where do they come from?

  • If you have labelled data (sequences with known island annotations): just count transitions and emissions and normalise. Easy.
  • If you only have raw sequence (no labels — the usual case): Baum–Welch iterates “guess the hidden states, re-estimate the parameters, repeat” until it converges. It is the EM algorithm for HMMs.

Caveat: Baum–Welch finds a local optimum — the starting guess matters, and results need a sanity check.

An HMM adds hidden states and emissions to a Markov chain

Visible Markov chain (Markov chains) Hidden Markov Model (today)
States observed directly hidden — must be inferred
Parameters transition matrix A A and emission matrix B
You see the states only the emissions \mathbf{x}
Core question how does the chain evolve? what hidden states explain what I see?
Biology base-to-base model island vs background segmentation

An HMM = a Markov chain (from Markov chains) on the hidden layer plus an emission layer.

Summary

  • A genome’s island vs background label is hidden; you see only the bases. That is exactly an HMM.
  • An HMM = hidden states + transition matrix A (the chain from Markov chains) + emission matrix B + initial distribution \boldsymbol{\pi}.
  • Generative story: start from \boldsymbol{\pi}, then alternate emit (B) and move (A). The probability of one path-with-emissions is the product of those factors.
  • Three problems: likelihood (forward), decoding (Viterbi), learning (Baum–Welch).

Fitting HMMs in R: fit and decode these HMMs in R with the HMM package on genome-like sequence.