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.

Exercise: Fitting HMMs in R to find CpG islands

Goal

After this exercise you will be able to:

  • Build a 2-state HMM in R with the CRAN HMM package (initHMM).
  • Simulate genome-like sequence with simHMM and recover its likelihood with forward.
  • Decode the most likely hidden-state path with viterbi and read it as a predicted CpG-island map.
  • Learn the parameters \(A\) and \(B\) from unlabelled sequence with baumWelch, and recognise local optima and label switching.
  • Verify one of the by-hand path probabilities from Hidden Markov models against simHMM’s logic.

This is an R-heavy day. Run every block, read the output, and answer the questions in the comments before opening the solutions.

Background

We reuse the model from Hidden Markov models. States \(\{H, L\}\): H = “GC-rich (CpG-island-like)”, L = “normal background”. Symbols are the bases \(\{A, C, G, T\}\).

\[ \boldsymbol{\pi}=\begin{bmatrix}0.5\\0.5\end{bmatrix},\quad A=\begin{bmatrix}0.8&0.2\\0.3&0.7\end{bmatrix}\ (HL\times HL),\quad B=\begin{bmatrix}0.1&0.4&0.4&0.1\\0.3&0.2&0.2&0.3\end{bmatrix}\ (ACGT). \]

The HMM package maps one function onto each idea from Hidden Markov models: initHMM builds the model, simHMM generates data, forward gives the likelihood, viterbi decodes the best path, and baumWelch learns the parameters. Install it once with install.packages("HMM").

Setup

install.packages("HMM")   # run once
library(HMM)

states  <- c("H", "L")
symbols <- c("A", "C", "G", "T")

transProbs <- matrix(c(0.8, 0.2,
                       0.3, 0.7), nrow = 2, byrow = TRUE,
                     dimnames = list(states, states))

emissionProbs <- matrix(c(0.1, 0.4, 0.4, 0.1,
                          0.3, 0.2, 0.2, 0.3), nrow = 2, byrow = TRUE,
                        dimnames = list(states, symbols))

hmm <- initHMM(States = states, Symbols = symbols,
               startProbs = c(0.5, 0.5),
               transProbs = transProbs,
               emissionProbs = emissionProbs)
hmm   # inspect: check rows of transProbs and emissionProbs each sum to 1

In R

Task 1 — simulate and inspect

set.seed(1)
sim <- simHMM(hmm, length = 200)

obs   <- sim$observation     # the bases you "see"
truth <- sim$states          # the TRUE hidden path (hidden in real life)

head(obs,   20)
head(truth, 20)

# Q1a: what fraction of the simulated bases are G or C overall?
mean(obs %in% c("G", "C"))

# Q1b: among positions whose TRUE state is H, what fraction are G or C?
#      compare with the L positions. Does it match the emission matrix?
mean(obs[truth == "H"] %in% c("G", "C"))
mean(obs[truth == "L"] %in% c("G", "C"))

Task 2 — likelihood with forward

f <- forward(hmm, obs)        # log forward probabilities, states x positions
dim(f)                        # 2 x 200

# total log-likelihood = log-sum-exp of the final column
logsumexp <- function(v) { m <- max(v); m + log(sum(exp(v - m))) }
ll <- logsumexp(f[, length(obs)])
ll
# Q2: this is a LOG probability. Why is the raw probability P(obs) so tiny
#     that we must work in logs? (Recall how fast your by-hand products in Hidden Markov models shrank.)

Task 3 — decode the islands with viterbi

path <- viterbi(hmm, obs)

# Q3a: decoding accuracy against the known truth
mean(path == truth)

# Q3b: read off predicted CpG islands as maximal runs of "H"
runs <- rle(path)
runs

# turn runs into island coordinates (start, end, length) for the H runs
ends   <- cumsum(runs$lengths)
starts <- ends - runs$lengths + 1
islands <- data.frame(state = runs$values, start = starts, end = ends,
                      length = runs$lengths)
islands[islands$state == "H", ]
# Q3c: how many predicted islands are there, and how long is the longest?

Task 4 — learn the parameters with baumWelch

Now pretend you never knew \(A\) and \(B\). Learn them from obs alone, starting from a deliberately imperfect but asymmetric guess. EM needs this small symmetry break: identical state rows remain identical under its updates.

guess <- initHMM(states, symbols,
                 startProbs    = c(0.5, 0.5),
                 transProbs    = matrix(c(0.6, 0.4,
                                          0.4, 0.6), 2, byrow = TRUE,
                                        dimnames = list(states, states)),
                 emissionProbs = matrix(c(0.3, 0.2, 0.2, 0.3,
                                          0.2, 0.3, 0.3, 0.2), 2, byrow = TRUE,
                                        dimnames = list(states, symbols)))

bw <- baumWelch(guess, obs, maxIterations = 100)

round(bw$hmm$transProbs,    2)   # learned A
round(bw$hmm$emissionProbs, 2)   # learned B
# Q4a: which learned state ended up GC-heavy? Is it called "H" or "L"?
#      (This is LABEL SWITCHING — the name is arbitrary.)
# Q4b: decode with the LEARNED model and compare accuracy to Task 3.
path_learned <- viterbi(bw$hmm, obs)
mean(path_learned == truth)    # raw labels may be swapped
gc_state <- rownames(bw$hmm$emissionProbs)[
  which.max(rowSums(bw$hmm$emissionProbs[, c("C", "G")]))]
path_aligned <- ifelse(path_learned == gc_state, "H", "L")
mean(path_aligned == truth)    # compare after naming the GC-rich state H

Task 5 — local optima (a pitfall)

# Try a different starting guess and see if baumWelch lands somewhere else.
guess2 <- initHMM(states, symbols,
                  startProbs    = c(0.5, 0.5),
                  transProbs    = matrix(c(0.9, 0.1,
                                           0.1, 0.9), 2, byrow = TRUE,
                                         dimnames = list(states, states)),
                  emissionProbs = matrix(c(0.4, 0.1, 0.1, 0.4,
                                           0.1, 0.4, 0.4, 0.1), 2, byrow = TRUE,
                                         dimnames = list(states, symbols)))
bw2 <- baumWelch(guess2, obs, maxIterations = 100)
round(bw2$hmm$emissionProbs, 2)
# Q5: do bw and bw2 agree (up to label switching)? What does this tell you
#     about trusting a single baumWelch run?

Hints

initHMM takes States, Symbols, startProbs, transProbs, emissionProbs. Use byrow = TRUE and dimnames so the matrices read like the page, and double-check every row sums to 1 — a row summing to 0.99 is a silent bug. See the package reference manual.

simHMM returns a list: $observation (the emitted symbols, your data) and $states (the true hidden path). You only get $states because it is a simulation; on real data the states are unknown. Use set.seed() so your run is reproducible.

forward returns a matrix of log forward probabilities (rows = states, columns = positions). The data log-likelihood is the log-sum-exp of the last column. Working in logs avoids numerical underflow — the same reason your products from Hidden Markov models got tiny so fast.

viterbi returns one state label per position. rle (run-length encoding) collapses consecutive equal labels into runs; the runs whose value is "H" are your predicted CpG islands. cumsum(runs$lengths) gives each run’s end coordinate.

baumWelch returns $hmm (the fitted model) and $difference (convergence trace). The fitted state called "H" is not guaranteed to be the GC-rich one — decide which is the island state by reading bw$hmm$emissionProbs (the row with high C and G probability), not by its name. As an alternative package with more flexible emissions, see depmixS4.

Solutions

Task 1 — simulate and inspect

set.seed(1)
sim <- simHMM(hmm, length = 200)
obs   <- sim$observation
truth <- sim$states

mean(obs %in% c("G", "C"))
# [1] 0.59        # overall GC content, a mix of both states

mean(obs[truth == "H"] %in% c("G", "C"))
# [1] 0.795082    # H emits C or G with prob 0.4 + 0.4 = 0.8
mean(obs[truth == "L"] %in% c("G", "C"))
# [1] 0.2692308   # a finite-sample estimate of the L probability 0.4

Q1a/Q1b: the expected per-state GC fractions are \(0.8\) for \(H\) and \(0.4\) for \(L\). This sequence gives about \(0.795\) and \(0.269\): the estimates fluctuate, especially for \(L\), which has only 78 positions here. The overall fraction is \(0.59\), weighted by how much time the chain spends in each state.

Task 2 — likelihood

f  <- forward(hmm, obs)
logsumexp <- function(v) { m <- max(v); m + log(sum(exp(v - m))) }
ll <- logsumexp(f[, length(obs)])
ll
# [1] -272.9257   (a large negative log-probability)

Q2: \(P(\mathbf{x})\) sums over hidden paths, each contributing a product of 200 emission and 199 transition factors plus a start probability. Here it is about \(3\times10^{-119}\). That is small but still representable as a double at this length; working with the log is the numerically stable convention and prevents underflow for longer sequences. This is exactly why the by-hand products in Hidden Markov models collapsed toward zero so quickly.

Task 3 — decoding

path <- viterbi(hmm, obs)
mean(path == truth)
# [1] 0.755        # most positions decoded correctly; not 100% because
#                  # emissions are genuinely ambiguous

runs <- rle(path)
ends   <- cumsum(runs$lengths)
starts <- ends - runs$lengths + 1
islands <- data.frame(state = runs$values, start = starts, end = ends,
                      length = runs$lengths)
islands[islands$state == "H", ]
#   state start end length
# 2     H     4  20     17
# 4     H    31  40     10
# 6     H    44  52      9
# 8     H    60 101     42
# ... (10 islands in total)

Q3: decoding is good but imperfect because a single base never determines the state; Viterbi uses the surrounding context. With the stated seed there are 10 predicted islands; the longest spans 42 bases (60–101). Changing the seed changes these results.

Task 4 — learning

bw <- baumWelch(guess, obs, maxIterations = 100)
round(bw$hmm$transProbs, 2)
round(bw$hmm$emissionProbs, 2)
#      H    L
# H 0.71 0.29
# L 0.30 0.70
#      A    C    G    T
# H 0.31 0.14 0.17 0.38
# L 0.08 0.59 0.30 0.03

path_learned <- viterbi(bw$hmm, obs)
mean(path_learned == truth)
# [1] 0.23        # learned labels are swapped relative to the true labels
gc_state <- rownames(bw$hmm$emissionProbs)[
  which.max(rowSums(bw$hmm$emissionProbs[, c("C", "G")]))]
path_aligned <- ifelse(path_learned == gc_state, "H", "L")
mean(path_aligned == truth)
# [1] 0.77        # compare with 0.755 using the true model

Q4a: whichever learned row has high C+G probability is the island state, regardless of whether the package labelled it "H" or "L" — that is label switching. Here the learned "L" state is GC-rich. Q4b: after aligning the labels, decoding is 77% accurate, compared with 75.5% using the true model. On this finite sequence the learned model scores slightly better against the simulated truth; this is not a guarantee for new sequences.

Task 5 — local optima

bw2 <- baumWelch(guess2, obs, maxIterations = 100)
round(bw2$hmm$emissionProbs, 2)

Q5: the two runs usually find essentially the same solution up to label switching, but not always — EM only reaches a local optimum, so a poor start can land elsewhere. The lesson: run baumWelch from several initialisations, compare the resulting log-likelihoods (via forward), and keep the best — never trust a single run blindly.

Where this leads

You have run the full HMM workflow on genome-like data: build, simulate, score, decode into an island map, and learn parameters from unlabelled sequence — and you have seen the pitfalls (local optima, label switching, choosing the number of states). This closes the linear-algebra block.

The ideas continue through the course. In week 2, PCA replaces discrete hidden states with continuous latent directions found via the eigenvectors of Eigenvalues & eigenvectors and Eigenvalues in R. The Bayesian-statistics block generalises today’s “infer the hidden from the observed” with priors and full posteriors. And single-cell analysis will segment and annotate cells much as you segmented and annotated a genome here. You now hold the complete linear-algebra toolkit the rest of the course rests on.