Exercise: Hidden Markov Models by hand
Goal
After this exercise you will be able to:
- Identify the four ingredients of an HMM (states, transition matrix \(A\), emission matrix \(B\), initial distribution \(\boldsymbol{\pi}\)) in a biology problem.
- Tell the generative story of a 2-state HMM and compute the probability of one specific hidden path together with its observations — by hand.
- Compute a small likelihood by summing over a few paths.
- Reason about decoding (which of two paths is more likely).
- Check your by-hand numbers in R.
The by-hand path-probability calculation is the single most important skill of this unit, so do the pen-and-paper parts before you touch R.
Background
A Hidden Markov Model has a hidden Markov chain over states (the chain from Markov chains) plus an emission at every step. We use:
- hidden states with transition matrix \(A\), where \(A_{ij}=P(z_{t+1}=j\mid z_t=i)\) and each row sums to 1;
- emission matrix \(B\), where \(B_{ik}=P(x_t=k\mid z_t=i)\), one row per state, one column per symbol, each row sums to 1;
- initial distribution \(\boldsymbol{\pi}\) with \(\pi_i=P(z_1=i)\).
The joint probability of a hidden path \(\mathbf{z}=(z_1,\dots,z_T)\) together with the observations \(\mathbf{x}=(x_1,\dots,x_T)\) is the product along the generative story:
\[ P(\mathbf{x},\mathbf{z}) = \pi_{z_1}\, B_{z_1, x_1} \prod_{t=2}^{T} A_{z_{t-1}, z_t}\, B_{z_t, x_t}. \]
In words: start probability, then alternate emit (\(B\)) and move (\(A\)), ending on an emission. The likelihood of the data alone sums this over every possible path: \(P(\mathbf{x})=\sum_{\mathbf{z}}P(\mathbf{x},\mathbf{z})\).
The model for this exercise
States \(\{H, L\}\): H = “GC-rich (CpG-island-like)”, L = “normal background”. Symbols are the DNA 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}, \qquad B= \left[\begin{array}{cccc} 0.1 & 0.4 & 0.4 & 0.1 \\ 0.3 & 0.2 & 0.2 & 0.3 \end{array}\right] \begin{array}{c} H\\ L \end{array} \]
(The columns of \(B\) are, in order, \(A, C, G, T\).)
By hand
Do these on paper. Show every factor in your products, not just the final number.
Problem 1 — read the model
- What does \(B_{H,G}=0.4\) mean in words? What does \(A_{L,L}=0.7\) mean?
- Verify that every row of \(A\) and every row of \(B\) sums to 1, and that \(\boldsymbol{\pi}\) sums to 1.
- Which state is more likely to emit a
C? Which state tends to produce long runs of the same state? (Look at the diagonal of \(A\).)
Problem 2 — probability of one path with its emissions
Observed sequence \(\mathbf{x}=(G, C, A)\). Consider the hidden path \(\mathbf{z}=(H, H, L)\).
- Write out the product \(P(\mathbf{x},\mathbf{z})\) factor by factor, labelling each factor as a start, an emission, or a transition.
- Evaluate it to a number.
Problem 3 — compare two paths (decoding intuition)
Same observations \(\mathbf{x}=(G, C, A)\). Now consider the all-high path \(\mathbf{z}'=(H, H, H)\).
- Compute \(P(\mathbf{x}, \mathbf{z}')\).
- Which of \(\mathbf{z}=(H,H,L)\) and \(\mathbf{z}'=(H,H,H)\) is the more likely explanation of the data? This is the kind of comparison the Viterbi algorithm makes (over all paths at once).
Problem 4 — a small likelihood (summing over paths)
Take the shorter observation \(\mathbf{x}=(G, C)\) (length 2).
- List all \(2^2=4\) hidden paths.
- Compute \(P(\mathbf{x}, \mathbf{z})\) for each path.
- Add them up to get the likelihood \(P(\mathbf{x}=(G,C))\). (This is what the forward algorithm computes, but for length 2 we can just enumerate.)
Explore
The demo below generates 120 bases from the model above (the Lecture HMM preset). The strip true z shows the hidden states (orange = \(H\), grey = \(L\)), the tiles show the bases (dark = C or G). Step runs the generative story one choice at a time: first a state, then a base. Hide states greys out the true path. Viterbi adds the most likely path as a second strip, and Posterior draws \(P(z_t = H \mid \mathbf{x})\) for every position, computed from the whole sequence by the forward–backward algorithm (the dashed line is 0.5). Reveal truth shows the true path again and the fraction of positions each method got right. Moving a slider re-simulates with the same random numbers, and decoding always uses the current (true) parameters.
E1. With the Lecture HMM, press Clear, then Step six times, so that three states and three bases are shown. Write down the path \(\mathbf{z}\) and the bases \(\mathbf{x}\), and compute \(P(\mathbf{x},\mathbf{z})\) by hand as in Problem 2. Check your answer against the readout. Press Step twice more: which two factors are added, and how does this compare with the path \((H,H,L)\) of Problem 2?
E2. Press Generate all, tick Hide states, then press Viterbi and tick Posterior. Count the predicted islands (orange runs in the Viterbi strip; a run that continues on the next row counts once). Now press Reveal truth. How many islands are in the true path, and what fraction of positions do Viterbi and the posterior get right? Find the positions where the Viterbi strip and the posterior (above or below the dashed line) disagree. What is the true state there?
E3. Set both stay probabilities, \(A(H\to H)\) and \(A(L\to L)\), to 0.95 and look at the true path, the Viterbi path and the accuracy. Then choose Not sticky (\(A(H\to H)=0.55\), \(A(L\to L)=0.5\)). What happens to the islands and to the Viterbi path? What rule is Viterbi following for this model?
E4. Choose Low contrast (GC content 0.55 in \(H\) and 0.45 in \(L\)). What does Viterbi predict now, and how far does the posterior move from the dashed line? Then set both GC sliders to 0.50. Around which value does the posterior stay, and where does that value come from?
E1. The path starts \(\mathbf{z}=(H,H,H)\) with bases \(\mathbf{x}=(G,G,C)\): \[P(\mathbf{x},\mathbf{z}) = \pi_H\,B_{H,G}\,A_{H,H}\,B_{H,G}\,A_{H,H}\,B_{H,C} = 0.5\times0.4\times0.8\times0.4\times0.8\times0.4 = 0.02048,\] shown as 0.0205. The fourth state is \(L\) and it emits an A, which adds the factors \(A_{H,L}=0.2\) and \(B_{L,A}=0.3\): \(0.02048\times0.2\times0.3 = 0.00123\). As in Problem 2, leaving \(H\) costs a factor 0.2, and an A is three times more likely from \(L\) than from \(H\).
E2. Viterbi predicts 5 islands; the true path has 14 runs of \(H\), many of them short. Viterbi is right at 74% of the positions, the posterior at 81%. (For comparison: 68% of the positions are truly \(H\), so “everything is \(H\)” would already score 68%.) The two disagree at 10 positions, in three short stretches (within bases 39–44, 71–74 and 111–114). At all of them Viterbi says \(H\) while the posterior is below 0.5, and 9 of the 10 are truly \(L\). Viterbi picks the single best path, and leaving and re-entering \(H\) costs two small transition factors, so it bridges short A/T stretches inside an island. The posterior judges each position on its own.
E3. With stay probabilities 0.95 the true path has 3 long islands (38, 24 and 21 bases); Viterbi finds 2 (77 and 20 bases) and gets 87% of the positions right (posterior 94%). With Not sticky the true path breaks into 29 short runs, and Viterbi into 27 short pieces; both Viterbi and the posterior get 75% right. Viterbi now simply calls every C or G as \(H\) and every A or T as \(L\): when the states hardly stick, the neighbours say almost nothing, so each base is judged alone. (Each setting draws its own path from the same random numbers, so the sequences differ a little. Averaged over 200 seeds, Viterbi gets about 72% right for the Lecture HMM, 85% with stay probabilities 0.95, and 71% for Not sticky.)
E4. With low contrast Viterbi calls all 120 positions \(H\), so its accuracy is just the fraction of \(H\) positions, 68%. The posterior stays between 0.47 and 0.70: the bases carry little information. With both GC contents at 0.50 the two rows of \(B\) are identical, the bases carry no information at all, and the posterior stays between 0.50 and 0.60. It starts at \(\pi_H = 0.5\) and settles at 0.6, the long-run fraction of time the hidden chain spends in \(H\): \(A_{L,H}/(A_{H,L}+A_{L,H}) = 0.3/(0.2+0.3) = 0.6\), the stationary distribution of \(A\) (Markov chains in R).
In R
Use R only to verify the by-hand numbers. We are not using the HMM package yet (that is Fitting HMMs in R) — here we just multiply the probabilities ourselves so the arithmetic is transparent.
# The model
pi <- c(H = 0.5, L = 0.5)
A <- matrix(c(0.8, 0.2,
0.3, 0.7), nrow = 2, byrow = TRUE,
dimnames = list(c("H", "L"), c("H", "L")))
# columns are A, C, G, T
B <- matrix(c(0.1, 0.4, 0.4, 0.1,
0.3, 0.2, 0.2, 0.3), nrow = 2, byrow = TRUE,
dimnames = list(c("H", "L"), c("A", "C", "G", "T")))
# Problem 2: path (H, H, L) with observations (G, C, A)
p_HHL <- pi["H"] * B["H", "G"] * A["H", "H"] * B["H", "C"] *
A["H", "L"] * B["L", "A"]
p_HHL
# expected: 0.00384
# Problem 3: path (H, H, H) with the same observations
p_HHH <- pi["H"] * B["H", "G"] * A["H", "H"] * B["H", "C"] *
A["H", "H"] * B["H", "A"]
p_HHH
# compare p_HHH with p_HHL: which path is more likely?# Problem 4: likelihood of x = (G, C) by enumerating all 4 paths
states <- c("H", "L")
obs <- c("G", "C")
joint <- function(z1, z2) {
pi[z1] * B[z1, obs[1]] * A[z1, z2] * B[z2, obs[2]]
}
paths <- expand.grid(z1 = states, z2 = states, stringsAsFactors = FALSE)
paths$prob <- mapply(joint, paths$z1, paths$z2)
paths
sum(paths$prob) # the likelihood P(x = (G, C))The functions used: matrix (note byrow = TRUE and dimnames so we can index by state/base names), expand.grid to list all paths, and mapply to apply the joint-probability function to each path.
Hints
For a sequence of length \(T\), the product \(P(\mathbf{x},\mathbf{z})\) has exactly one \(\boldsymbol{\pi}\) factor, \(T\) emission factors (one per position), and \(T-1\) transition factors (one per gap between positions). For \(T=3\) that is \(1 + 3 + 2 = 6\) factors.
Lay it out left to right as the generative story: \[ \pi_{z_1}\;\to\; B_{z_1,x_1}\;\to\; A_{z_1,z_2}\;\to\; B_{z_2,x_2} \;\to\; A_{z_2,z_3}\;\to\; B_{z_3,x_3}. \] Start, emit, move, emit, move, emit. Always finish on an emission.
\(A_{ij}\) is the entry in row \(i\) (current state), column \(j\) (next state). \(B_{ik}\) is row \(i\) (state), column \(k\) (emitted symbol). For \(B\), the columns are ordered \(A, C, G, T\), so e.g. \(B_{H,G}\) is row \(H\), third column \(=0.4\).
For decoding you do not need the likelihood. Just compute the joint probability of each candidate path and compare the numbers — the larger one is the more likely explanation. (Viterbi does this comparison over all paths simultaneously and efficiently.)
A single path probability is \(P(\mathbf{x},\mathbf{z})\). The likelihood \(P(\mathbf{x})\) is the sum of those over every possible path. With 2 states and length 2 there are \(2^2=4\) paths: \((H,H), (H,L), (L,H), (L,L)\).
Solutions
Problem 1 — read the model
\(B_{H,G}=0.4\): given the hidden state is \(H\) (GC-rich), the probability of emitting a
Gis 0.4. \(A_{L,L}=0.7\): given the chain is in state \(L\), the probability the next state is again \(L\) is 0.7.Rows of \(A\): \(0.8+0.2=1\), \(0.3+0.7=1\). Rows of \(B\): \(0.1+0.4+0.4+0.1=1\), \(0.3+0.2+0.2+0.3=1\). \(\boldsymbol{\pi}\): \(0.5+0.5=1\). All valid.
Cis more likely from \(H\) (\(B_{H,C}=0.4 > B_{L,C}=0.2\)). State \(H\) tends to produce longer runs, since \(A_{H,H}=0.8 > A_{L,L}=0.7\) (the larger diagonal entry is “stickier”).
Problem 2 — one path with its emissions
Path \(\mathbf{z}=(H,H,L)\), observations \(\mathbf{x}=(G,C,A)\):
\[ P(\mathbf{x},\mathbf{z}) = \underbrace{\pi_H}_{0.5}\; \underbrace{B_{H,G}}_{0.4}\; \underbrace{A_{H,H}}_{0.8}\; \underbrace{B_{H,C}}_{0.4}\; \underbrace{A_{H,L}}_{0.2}\; \underbrace{B_{L,A}}_{0.3} \] \[ = 0.5 \times 0.4 \times 0.8 \times 0.4 \times 0.2 \times 0.3 = \mathbf{0.003840}. \]
Problem 3 — compare two paths
Path \(\mathbf{z}'=(H,H,H)\), same observations: \[ P(\mathbf{x},\mathbf{z}') = \pi_H\,B_{H,G}\,A_{H,H}\,B_{H,C}\,A_{H,H}\,B_{H,A} = 0.5\times0.4\times0.8\times0.4\times0.8\times0.1 = \mathbf{0.005120}. \]
Compare: \(P(\mathbf{x},\mathbf{z}')=0.00512 > P(\mathbf{x},\mathbf{z})=0.00384\). So the all-high path \((H,H,H)\) is the more likely explanation of \((G,C,A)\). Intuition: staying in \(H\) is cheap (transition 0.8 vs 0.2), and even though \(H\) emits A with only 0.1, that loss is outweighed by the much higher transition probability of staying put.
Problem 4 — likelihood of \(\mathbf{x}=(G,C)\)
The four paths and their joint probabilities \(P=\pi_{z_1}\,B_{z_1,G}\,A_{z_1,z_2}\,B_{z_2,C}\):
| path | \(\pi\) | emit \(G\) | move | emit \(C\) | product |
|---|---|---|---|---|---|
| \((H,H)\) | 0.5 | 0.4 | 0.8 | 0.4 | \(0.0640\) |
| \((H,L)\) | 0.5 | 0.4 | 0.2 | 0.2 | \(0.0080\) |
| \((L,H)\) | 0.5 | 0.2 | 0.3 | 0.4 | \(0.0120\) |
| \((L,L)\) | 0.5 | 0.2 | 0.7 | 0.2 | \(0.0140\) |
Likelihood: \[ P(\mathbf{x}=(G,C)) = 0.0640 + 0.0080 + 0.0120 + 0.0140 = \mathbf{0.098}. \]
R verification
p_HHL
# H
# 0.00384
p_HHH
# H
# 0.00512 # the all-H path is more likely, matching Problem 3
paths
# z1 z2 prob
# 1 H H 0.064
# 2 L H 0.012
# 3 H L 0.008
# 4 L L 0.014
sum(paths$prob)
# [1] 0.098 # matches the by-hand likelihoodWhere this leads
You computed path probabilities, a likelihood, and a path comparison by hand for a tiny model. Real genomes have millions of positions and far too many paths to enumerate. Fitting HMMs in R introduces the CRAN HMM package, which does all of this efficiently: forward for the likelihood, viterbi to decode the most likely island map, and baumWelch to learn \(A\), \(B\), \(\boldsymbol{\pi}\) from unlabelled sequence. After the linear-algebra block, these probabilistic-model ideas return throughout the course — in the Bayesian-statistics block (latent variables and inference), and again when we segment and annotate single-cell data.