Hidden Markov model

Also known as: HMM

On this page 8

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.

ComponentNotationMeaning
Hidden statesS = {s1, …, sN}Finite set of unobserved conditions of the system
Initial distributionpi_iProbability that the sequence starts in state i
Transition matrixA, entries a_ijProbability of moving from state i to state j
Emission distributionB, entries b_j(o)Probability of observing o while in state j
Observation sequenceO = o1, …, oTThe data actually recorded over T time steps
Components of a hidden Markov model

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.

ProblemQuestion answeredStandard algorithmComplexity
EvaluationHow likely is this observation sequence?Forward algorithmO(N squared times T)
DecodingWhich hidden state path is most likely?Viterbi algorithmO(N squared times T)
Filtering and smoothingWhat is the state probability at each step?Forward-backward algorithmO(N squared times T)
LearningWhich parameters best explain the data?Baum-Welch algorithmO(N squared times T) per iteration
Core inference and learning tasks

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.

VariantKey changeTypical application
Left-to-right HMMTransitions only forward or self-loopPhonemes and words in speech
Gaussian HMMContinuous Gaussian emissionsSensor signals and finance regimes
Profile HMMMatch, insert and delete statesProtein and DNA family alignment
Hidden semi-Markov modelExplicit state duration distributionActivity and sleep stage modeling
Input-output HMMTransitions conditioned on inputsControl and dialogue systems
Factorial HMMSeveral parallel hidden chainsSource separation and energy disaggregation
Frequently used variants

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.

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.

ModelModel typeHidden stateMain strength
Markov chainGenerativeNone, states are observedSimplest sequence model
Hidden Markov modelGenerativeDiscrete, single variableInterpretable, data-efficient inference
Kalman filterGenerativeContinuous, linear GaussianOptimal tracking of continuous signals
Conditional random fieldDiscriminativeLabels conditioned on full inputRich, overlapping features
Recurrent neural networkUsually discriminativeContinuous learned vectorLong-range, nonlinear patterns
HMM versus neighboring approaches

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.

SymptomLikely causeRemedy
Likelihood becomes zero or NaNProbability underflowUse log-space or scaled recursions
Results change between runsLocal optima in Baum-WelchMultiple restarts, better initialization
Unrealistic state durationsGeometric duration assumptionHidden semi-Markov model
States lack clear meaningToo many states, label switchingFewer states, model selection, constraints
Poor accuracy on long contextFirst-order Markov assumptionHigher-order states or neural models
Symptoms, causes and fixes

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.