A hidden Markov model (HMM) is a probabilistic model of sequences in which a system moves through hidden states that cannot be observed directly, and each state emits an observable symbol or measurement according to its own probability distribution. In machine learning, the HMM is one of the classic generative models for sequential data, and in artificial intelligence it long served as the backbone of speech recognition, part-of-speech tagging and biological sequence analysis.
The model rests on two simplifying assumptions. The Markov property states that the next hidden state depends only on the current one, and output independence states that each observation depends only on the state that produced it. These assumptions make inference tractable and are also the main source of the model’s limitations.
How a hidden Markov model is structured
An HMM is fully specified by three groups of parameters, often written together as lambda = (A, B, pi). The hidden states form a first-order Markov chain, while the observations are produced by state-dependent emission distributions. The following table lists the components and their typical notation.
| Component | Notation | Meaning |
|---|---|---|
| Hidden states | S = {s1, …, sN} | Finite set of unobserved conditions of the system |
| Initial distribution | pi_i | Probability that the sequence starts in state i |
| Transition matrix | A, entries a_ij | Probability of moving from state i to state j |
| Emission distribution | B, entries b_j(o) | Probability of observing o while in state j |
| Observation sequence | O = o1, …, oT | The data actually recorded over T time steps |
Emissions can be discrete, using a categorical table per state, or continuous, typically using a Gaussian mixture model per state. The continuous variant, known as the GMM-HMM, dominated acoustic modeling for decades because each state could represent a complex distribution of spectral features.
The three classic HMM problems and their algorithms
Practical work with an HMM reduces to three canonical problems: evaluating how likely a sequence is, finding the most probable hidden path and learning the parameters from data. Each has a dynamic programming solution that avoids enumerating the N to the power of T possible state paths.
| Problem | Question answered | Standard algorithm | Complexity |
|---|---|---|---|
| Evaluation | How likely is this observation sequence? | Forward algorithm | O(N squared times T) |
| Decoding | Which hidden state path is most likely? | Viterbi algorithm | O(N squared times T) |
| Filtering and smoothing | What is the state probability at each step? | Forward-backward algorithm | O(N squared times T) |
| Learning | Which parameters best explain the data? | Baum-Welch algorithm | O(N squared times T) per iteration |
The Baum-Welch algorithm is a special case of the expectation-maximization algorithm. The expectation step uses forward-backward passes to compute expected state occupancies and transitions, and the maximization step re-estimates A, B and pi from those expectations. It converges to a local maximum of the likelihood, so results depend on initialization.
In practice, implementations work with log probabilities or scaling factors, because multiplying many small probabilities over long sequences causes numerical underflow. Skipping this step is one of the most frequent bugs in hand-written HMM code.
Common HMM variants
The basic HMM has been extended in several directions to handle richer data and constraints. The variants below differ in topology, emission type or the way time is modeled.
| Variant | Key change | Typical application |
|---|---|---|
| Left-to-right HMM | Transitions only forward or self-loop | Phonemes and words in speech |
| Gaussian HMM | Continuous Gaussian emissions | Sensor signals and finance regimes |
| Profile HMM | Match, insert and delete states | Protein and DNA family alignment |
| Hidden semi-Markov model | Explicit state duration distribution | Activity and sleep stage modeling |
| Input-output HMM | Transitions conditioned on inputs | Control and dialogue systems |
| Factorial HMM | Several parallel hidden chains | Source separation and energy disaggregation |
The hidden semi-Markov model addresses a notable weakness: in a standard HMM, the time spent in a state follows a geometric distribution, which rarely matches real durations such as phoneme lengths or machine operating cycles.
How HMMs compare with related sequence models
An HMM is best understood as a dynamic Bayesian network with one discrete hidden variable per time step. It sits between simple Markov chains and modern neural sequence models, and the comparison below clarifies where it fits.
| Model | Model type | Hidden state | Main strength |
|---|---|---|---|
| Markov chain | Generative | None, states are observed | Simplest sequence model |
| Hidden Markov model | Generative | Discrete, single variable | Interpretable, data-efficient inference |
| Kalman filter | Generative | Continuous, linear Gaussian | Optimal tracking of continuous signals |
| Conditional random field | Discriminative | Labels conditioned on full input | Rich, overlapping features |
| Recurrent neural network | Usually discriminative | Continuous learned vector | Long-range, nonlinear patterns |
Compared with a Gaussian mixture model, which treats observations as independent draws, an HMM adds temporal dependence between the mixture components. Compared with neural networks, it needs far less training data, but it cannot capture long-range context unless the state space grows very large.
Practical use and design choices
Building a useful HMM depends mostly on choosing the number of states, the topology and the emission family. Practitioners usually compare candidate models with BIC or held-out log-likelihood, because likelihood on training data always rises as states are added.
- Initialize carefully: k-means clustering of observations is a common starting point for emission parameters.
- Run several restarts of Baum-Welch and keep the model with the best held-out likelihood.
- Constrain the topology when domain knowledge exists, for example left-to-right models for ordered processes.
- Apply feature scaling to continuous inputs so that Gaussian emissions remain numerically stable.
- Use Viterbi decoding when a single consistent label sequence is required, and posterior probabilities when uncertainty matters.
Limitations and common mistakes
The strengths of the HMM come from its assumptions, and so do its main failure modes. The table below links typical symptoms to their causes and remedies.
| Symptom | Likely cause | Remedy |
|---|---|---|
| Likelihood becomes zero or NaN | Probability underflow | Use log-space or scaled recursions |
| Results change between runs | Local optima in Baum-Welch | Multiple restarts, better initialization |
| Unrealistic state durations | Geometric duration assumption | Hidden semi-Markov model |
| States lack clear meaning | Too many states, label switching | Fewer states, model selection, constraints |
| Poor accuracy on long context | First-order Markov assumption | Higher-order states or neural models |
A common misconception is that the hidden states automatically correspond to meaningful real-world categories. Unsupervised training finds states that maximize likelihood, and interpreting them requires validation against labeled examples or expert judgment.
Put simply
Imagine trying to guess the weather in another city only by watching what a friend there wears each day. You never see the weather itself, only the coats, shorts and umbrellas. A hidden Markov model works the same way: something hidden changes over time, and you only see clues it leaves behind.
The model knows two things: how the hidden situation tends to change from one day to the next, and which clues each situation usually produces. With that knowledge, it can work backward from the clues to the most likely sequence of hidden situations.
In practice
A maintenance team at a manufacturing plant monitors a pump with a vibration sensor that reports one reading per minute. The team suspects the pump moves through three conditions, namely healthy, worn and near failure, but no sensor measures wear directly.
A data scientist fits a three-state Gaussian HMM with a left-to-right topology to six months of vibration data, using ten Baum-Welch restarts and selecting the model by held-out likelihood. Viterbi decoding of new readings then labels each hour with its most likely condition.
When the posterior probability of the worn state stays above 0.8 for a full shift, the system raises a maintenance ticket. The outcome is earlier, planned repairs instead of emergency shutdowns, and the transition matrix also gives engineers an estimate of how long pumps typically remain in each condition.