Inferring what we can’t see
We learned to make our work reproducible, with git and pixi
Today, back to probabilistic models and algorithms!
How can we learn about things we can’t see, from the clues they leave behind?
| What we see | What’s hidden |
|---|---|
A genome: ...ACGGTCAGGTAAG... |
Where the genes, exons, and regulatory regions are |
| A protein sequence | Which family it belongs to, and which parts are its domains |
| A GPS track of a bison | Whether it’s resting, foraging, or traveling |
| A sound recording | The words being spoken |
| A noisy signal to your phone | The bits that were sent |
A hidden Markov model (HMM) handles all of these, with the same model and the same algorithms
Where was the casino cheating?
Red: loaded die; blue: fair die
An HMM has three parts
Plus start probabilities for the first roll
The hidden states are a Markov chain, just like a site in our substitution models, but along a sequence instead of through time; we just can’t see it!
A path is a sequence of hidden states, one per roll
\[ P(\texttt{266}, \text{Fair Loaded Loaded}) = \underbrace{0.5}_{\text{start F}} \underbrace{\tfrac{1}{6}}_{\text{F rolls 2}} \underbrace{0.05}_{\text{F}\rightarrow\text{L}} \underbrace{0.5}_{\text{L rolls 6}} \underbrace{0.9}_{\text{L}\rightarrow\text{L}} \underbrace{0.5}_{\text{L rolls 6}} \approx 0.00094 \]
With many rolls, this gets tiny, so we use logs (just like tree likelihoods):
\[ \log P(x, \pi) = \log s_{\pi_1} + \log e_{\pi_1}(x_1) + \sum_{i = 2}^{n} \Big[ \log a_{\pi_{i - 1} \pi_i} + \log e_{\pi_i}(x_i) \Big] \]
Each roll could be fair or loaded
| Rolls | Possible paths |
|---|---|
| 3 | \(2^3 = 8\) |
| 30 | \(2^{30} \approx 10^9\) |
| 300 | \(2^{300} \approx 2 \times 10^{90}\) |
| 1,000,000 bases of DNA | \(2^{1{,}000{,}000}\) |
More paths than atoms in the universe (\(\approx 10^{80}\)), just like trees; we can’t try them all!
Key idea: The best path that ends in state \(k\) at roll \(i\) came through some state \(j\) at roll \(i - 1\), along the best path that ends in \(j\)
So, we only need the best path ending in each state, at each roll
We fill in a table with one row per state and one column per roll
| Fills in a table… | At each cell… | Then… | |
|---|---|---|---|
| Alignment | one cell per pair of positions | takes the best of 3 moves | traces back |
| Viterbi | one cell per state and position | takes the best previous state | traces back |
| Pruning | one row per node | adds up over the hidden bases of each child | averages at the root |
| Forward algorithm | one cell per state and position | adds up over previous states | sums the last column |
All four are dynamic programming: Solve a big problem by building up answers to small ones, and reuse them
For the 300 rolls in the exercise:
| Path | Log probability |
|---|---|
| Viterbi | \(-543.1\) |
| Always fair | \(-553.6\) |
| The true path | \(-583.5\) |
We can only recover hidden states if they emit different things
| CpGs in 2 Mb of human chromosome 19 | Observed/expected |
|---|---|
| Whole stretch | 0.45 |
| Background | 0.38 |
| CpG islands | 0.78 |
CpG islands: stretches of a few hundred to a few thousand bases where CpGs are not depleted, often where genes start (Gardiner-Garden and Frommer 1987)
When we have data with known hidden states, we can train an HMM by just counting:
| One base | Pairs | |
|---|---|---|
| Accuracy | 0.921 | 0.959 |
| (Everything background) | 0.928 | 0.928 |
| UCSC islands found (of 109) | 65 | 70 |
| HMM islands that are real | 60 of 75 | 72 of 85 |
We use the same viterbi function as the casino! And 27 of 29 genes start near an HMM island.
| Problem | Hidden states | Observations |
|---|---|---|
| Gene finding (GENSCAN) (Burge and Karlin 1997) | exon, intron, intergenic, … | bases |
| Protein families (Pfam, HMMER) (Krogh et al. 1994; Eddy 2011) | match, insert, delete | amino acids |
| Conserved elements (phastCons) (Siepel et al. 2005) | conserved or not | alignment columns |
| Chromatin states (ChromHMM) (Ernst and Kellis 2012) | promoter, enhancer, … | histone marks |
| Population history (PSMC) (Li and Durbin 2011) | time to common ancestor | heterozygous sites |
The first HMM for DNA split sequences into stretches with different base compositions (Churchill 1989), a lot like our CpG islands
| Problem | Hidden states | Observations |
|---|---|---|
| Animal movement (Langrock et al. 2012) | resting, foraging, traveling | step lengths, turning angles |
| Speech recognition (Rabiner 1989) | sounds, words | audio |
| Digital communication (Viterbi 1967) | the bits that were sent | a noisy signal |
Viterbi invented his algorithm to decode noisy digital signals, like those sent to cell phones and from spacecraft; we’re using it to read genomes!
simulate_hmm: Simulate a night at the casinolog_joint_probability: How probable are the rolls and a path?viterbi_step and viterbi: Catch the casino cheatingplot_paths: Plot the true and Viterbi pathstrain_hmm: Learn an HMM by counting, and find CpG islands in a human chromosomeWatch for
How many sixes in a row does it take to convince an HMM the die is loaded?
I drafted and iterated on these slides in collaboration with Anthropic’s Claude (Opus 5.5 model).