← Wiki Index / Computation & Algorithms / Hidden Markov Model
💻 Computation & Algorithms

Hidden Markov Model

A Hidden Markov model (HMM) is a doubly stochastic generative model in which an unobservable first-order Markov chain transitions through latent states that emit observed symbols or continuous vectors according to state-specific probability distributions.

A Hidden Markov model (HMM) is a statistical generative model within computation and algorithms that represents sequential or time-series data as the observable output of an underlying, unobservable Markov chain. Formulated mathematically by Leonard E. Baum and colleagues at the Institute for Defense Analyses between 1966 and 1972 and popularized in speech recognition by Lawrence Rabiner (1989) and in computational molecular biology by Anders Krogh, David Haussler, Richard Durbin, and Sean Eddy (1994–1998), a Hidden Markov model couples a discrete latent state trajectory π = (π1, π2, ..., πT) with state-dependent emission distributions that generate the observed sequence X = (x1, x2, ..., xT).

In the natural sciences and bioinformatics, Hidden Markov models provide the probabilistic engine for remote protein domain homology detection (Pfam and HMMER profile HMMs), ab initio eukaryotic exon–intron gene finding (GENSCAN, Augustus), CpG island and chromatin-state genome segmentation (ChromHMM), single-molecule ion-channel patch-clamp current analysis, and coalescent ancestral demographic inference (Pairwise Sequentially Markovian Coalescent, PSMC).

Mathematical Formulation of Doubly Stochastic Processes

State Space, Transition Matrix, and Emission Distributions

A discrete first-order Hidden Markov model θ = (S, Σ, π0, A, E) is specified by five components:

  1. Latent State Alphabet (S): A finite set of K = |S| hidden states S = {s1, s2, ..., sK} (for example, {α-helix, β-sheet, random coil} in protein secondary structure, or {Match, Insert, Delete} in a profile alignment model).
  2. Observation Alphabet (Σ) or Continuous Space: Either a discrete symbol alphabet Σ = {v1, ..., vM} (such as the 4 nucleotides {A, C, G, T} or 20 canonical amino acids) or a continuous vector space ℝd.
  3. Initial State Distribution (π0): A probability vector a0k = P(π1 = sk) satisfying ∑k=1K a0k = 1.
  4. State Transition Probability Matrix (A): A K × K row-stochastic matrix with entries akl = P(πt = sl | πt−1 = sk), obeying the first-order Markov memoryless property P(πt | π1, ..., πt−1) = P(πt | πt−1) and row normalization ∑l=1K akl = 1.
  5. Emission Probability Distributions (E): For each latent state k ∈ S, a probability distribution ek(xt) = P(xt | πt = sk) governing the observed symbol (categorical multinomial probabilities ∑b ∈ Σ ek(b) = 1, or continuous Gaussian mixture densities). Conditional on the current latent state πt, the observation xt is statistically independent of all previous states and observations.

Under these two conditional independence assumptions, the joint probability of observing sequence X = (x1, ..., xT) together with a specific latent state path π = (π1, ..., πT) factorizes into a product of T transitions and T emissions:

P(X, π | θ) = a0,π1 · eπ1(x1) · ∏t=2T [ aπt−1,πt · eπt(xt) ]

The Three Canonical Inference Problems and Dynamic Programming Algorithms

Evaluating or training a Hidden Markov model requires solving three computational problems, each of which would require summing or maximizing over KT = exp(T ln K) exponential state paths if evaluated by brute force, but reduces to O(T K2) polynomial time via dynamic programming.

Canonical HMM ProblemMathematical ObjectiveAlgorithmic SolutionTime ComplexityPrimary Scientific Use Case
1. Evaluation (Scoring)Marginal likelihood P(X | θ) = ∑π P(X, π | θ)Forward algorithm (or Backward algorithm)O(T K2)Scoring whether a query protein belongs to a Pfam domain family
2. Decoding (Alignment)Optimal path π* = arg maxπ P(X, π | θ) or posterior P(πt = k | X, θ)Viterbi algorithm or Posterior decoding (Forward–Backward)O(T K2)Segmenting exons/introns, CpG islands, or transmembrane helices
3. Learning (Training)Maximum-likelihood parameters θ* = arg maxθ P(X | θ)Baum–Welch algorithm (Expectation–Maximization)O(T K2) per EM iterationEstimating transition and emission matrices from unannotated sequences

1. Likelihood Evaluation: The Forward and Backward Algorithms

To evaluate the total marginal probability P(X | θ) = ∑π P(X, π | θ) that model θ generated sequence X across all KT possible hidden paths, the Forward algorithm defines the forward variable αt(k) = P(x1, x2, ..., xt, πt = sk | θ) as the joint probability of observing prefix x1..t and arriving in state sk at step t:

  • Initialization (t = 1): α1(k) = a0k · ek(x1) for 1 ≤ k ≤ K.
  • Induction (t = 2, ..., T): αt(l) = el(xt) · ∑k=1K αt−1(k) akl.
  • Termination: P(X | θ) = ∑k=1K αT(k).

Symmetrically, the Backward algorithm defines the backward variable βt(k) = P(xt+1, ..., xT | πt = sk, θ) as the conditional probability of observing the remaining suffix from t + 1 to T given state sk at time t, initialized at βT(k) = 1 and propagated backward from t = T − 1 down to 1 via:

βt(k) = ∑l=1K akl · el(xt+1) · βt+1(l)

Because multiplying hundreds or thousands of probabilities smaller than 1 rapidly underflows IEEE 754 floating-point precision (P(X | θ) ∼ exp(−cT)), production implementations either rescale αt(k) by 1 / ∑j αt(j) at every step t or accumulate log-probabilities using the numerically stable identity:

ln(exp(u) + exp(v)) = max(u, v) + ln(1 + exp(−|u − v|))

2. State Sequence Decoding: Viterbi versus Posterior Decoding

When the goal is to uncover the hidden biological structure along a sequence—such as locating the boundaries of transmembrane α-helices or coding exons—two distinct decoding criteria are used:

  • Viterbi Path Decoding (Global Path Optimum): Formulated by Andrew J. Viterbi in 1967 for convolutional channel codes, the Viterbi algorithm replaces the sum in the Forward step with a maximum operator to find the single globally most probable state path π* = arg maxπ P(X, π | θ). Operating in log-space to avoid underflow, let Vt(l) denote the log-probability of the best prefix path ending in state l at step t:

Vt(l) = ln el(xt) + max1 ≤ k ≤ K [ Vt−1(k) + ln akl ] Saving the maximizing predecessor pointer ptrt(l) = arg maxk [Vt−1(k) + ln akl] allows a linear O(T) traceback from arg maxk VT(k) to recover π*.

  • Posterior (Forward–Backward) Decoding (Pointwise Marginal Optimum): When many near-optimal paths share similar probabilities, the single Viterbi path may be less informative than the marginal posterior probability γt(k) = P(πt = sk | X, θ) that position t belongs to state k, summed over all paths passing through (t, k). Combining the Forward and Backward variables yields:

γt(k) = [ αt(k) · βt(k) ] / P(X | θ) Choosing π̂t = arg maxk γt(k) at each position maximizes the expected number of correctly labeled positions, and the posterior probability γt(k) ∈ [0, 1] provides a calibrated pointwise confidence score for each residue alignment column.

3. Parameter Estimation: The Baum–Welch (EM) Algorithm

When the latent state labels are unobserved in training data, the transition matrix A and emission matrix E cannot be estimated by direct frequency counting. Leonard Baum, Ted Petrie, George Soules, and Norman Weiss (1970) developed the Baum–Welch algorithm, a special case of the Expectation–Maximization (EM) framework later generalized by Arthur Dempster, Nan Laird, and Donald Rubin (1977):

  • E-Step (Expectation): Using the current parameter estimate θ(r), run the Forward and Backward passes to compute the posterior state occupancy γt(k) = P(πt = sk | X, θ(r)) and the posterior transition responsibility ξt(k, l) = P(πt = sk, πt+1 = sl | X, θ(r)):

ξt(k, l) = [ αt(k) · akl · el(xt+1) · βt+1(l) ] / P(X | θ(r)) Summing ξt(k, l) over t = 1..T − 1 yields the expected number of transitions Âkl from state k to state l, and summing γt(k) over steps where xt = b yields the expected emission count Êk(b).

  • M-Step (Maximization): Normalize the expected counts to obtain updated parameters akl(r+1) = Âkl / ∑l′ Âkl′ and ek(r+1)(b) = Êk(b) / ∑b′ Êk(b′). By Jensen's inequality and the non-negativity of Kullback–Leibler divergence, each EM iteration monotonically increases (or leaves unchanged) the marginal log-likelihood ln P(X | θ(r+1)) ≥ ln P(X | θ(r)) until converging to a local maximum.

Profile Hidden Markov Models and Genomic Applications

Krogh–Haussler–Eddy Profile HMMs (Plan 7 Architecture)

A landmark breakthrough in bioinformatics occurred in 1994 when Anders Krogh, Michael Brown, I. Saira Mian, Kimmen Sjölander, and David Haussler introduced profile Hidden Markov models to convert a multiple sequence alignment of a homologous protein or RNA family into a position-specific probabilistic model. Whereas standard pairwise alignment uses position-invariant substitution matrices (BLOSUM62) and uniform affine gap penalties, a protein domain contains conserved active-site columns (with low Shannon entropy and strong intolerance to insertions) alternating with variable surface loops that readily tolerate insertions and deletions.

A profile HMM of consensus length L models each alignment column j ∈ {1, ..., L} with a repeating trio of states:

  1. Match State (Mj): Emits a single residue according to column j's position-specific amino-acid or nucleotide probability vector eMj(x) (regularized with Dirichlet mixture priors to handle small sample sizes).
  2. Insert State (Ij): Emits one or more extra residues between consensus columns j and j + 1 via a self-loop transition aIj,Ij.
  3. Silent Delete State (Dj): Emits no symbol, allowing a query sequence to skip consensus column j at a position-specific gap-opening (Mj−1 → Dj) and gap-extension (Dj−1 → Dj) probability.

Implemented in Sean Eddy's HMMER software suite (using the Plan 7 architecture, which forbids direct Ij ↔ Dj transitions and sums over all alignments via SIMD-vectorized Forward scoring) and the Pfam / InterPro databases, profile HMMs detect distant evolutionary homologs across the "twilight zone" (< 20–25% sequence identity) where pairwise BLAST searches fail.

CpG Islands, Generalized Gene-Finding HMMs, and Coalescent HMMs

Beyond homology search, HMMs partition whole genomes into functional annotations:

  • CpG Island and Chromatin Segmentation: In vertebrate genomes, 5-methylcytosine spontaneously deaminates to thymine, depleting CG dinucleotides genome-wide except within unmethylated promoter CpG islands. A two-regime HMM (or multi-state ChromHMM over ChIP-seq histone modifications) switches between background and island transition matrices to segment chromosomes via Viterbi or posterior decoding.
  • Generalized Hidden Markov Models (GHMMs) for Gene Prediction: Because standard geometric state dwell-time distributions P(length = d) = (1 − p)pd−1 poorly match empirical eukaryotic exon lengths and 3-base codon periodicity, eukaryotic gene finders such as GENSCAN (Chris Burge and Samuel Karlin, 1997) and Augustus use semi-Markov (generalized) HMMs in which each state emits an entire variable-length feature (5′ UTR, initial exon, donor splice site, intron, acceptor splice site, internal exon, terminal exon) conditioned on explicit length distributions.

Frequently Asked Questions

Why do sequence analysts use both the Viterbi algorithm and the Forward algorithm on the same profile HMM?

The Viterbi algorithm finds the single best alignment path π* between a query sequence and a profile HMM, which is required to display residue-to-column correspondences. However, when two distantly related proteins can be aligned in dozens of slightly shifted register variations around gapped loops, summing the probabilities of all valid alignments via the Forward algorithm yields a more sensitive log-odds bit score for homology detection than evaluating any single path alone.

How does the Markov assumption limit standard HMMs on RNA secondary structure?

A first-order Hidden Markov model conditions only on the immediately preceding state πt−1 (equivalent to a regular grammar in the Chomsky hierarchy). Because RNA base pairing couples distant nucleotides i and j separated by arbitrary hairpin spans (i ≪ j), modeling nested base pairs requires stepping up from regular HMMs to Stochastic Context-Free Grammars (SCFGs) (such as the Infernal covariance models used by the Rfam database), whose Inside and CYK dynamic programming algorithms run in O(L3) rather than O(L) time.

References

  1. Rabiner, L. R. (1989). A tutorial on hidden Markov models and selected applications in speech recognition. Proceedings of the IEEE, 77(2), 257–286. https://doi.org/10.1109/5.18626
  2. Baum, L. E., Petrie, T., Soules, G., & Weiss, N. (1970). A maximization technique occurring in the statistical analysis of probabilistic functions of Markov chains. The Annals of Mathematical Statistics, 41(1), 164–171. https://doi.org/10.1214/aoms/1177697196
  3. Krogh, A., Brown, M., Mian, I. S., Sjölander, K., & Haussler, D. (1994). Hidden Markov models in computational biology: Applications to protein modeling. Journal of Molecular Biology, 235(5), 1501–1531. https://doi.org/10.1006/jmbi.1994.1104
  4. Eddy, S. R. (1998). Profile hidden Markov models. Bioinformatics, 14(9), 755–763. https://doi.org/10.1093/bioinformatics/14.9.755
  5. Burge, C., & Karlin, S. (1997). Prediction of complete gene structures in human genomic DNA. Journal of Molecular Biology, 268(1), 78–94. https://doi.org/10.1006/jmbi.1997.0951
🧬 Support Open Science
Explore interactive science games on LowKey Games or browse scientific apparel & accessories in the CRV Science Shop.
💡 Spot an inaccuracy or have primary literature to expand this article?
Submit a citation or revision note using your shared CRV Science / LowKey Games profile ID.
Suggest Revision