🎲 Hidden Markov Model
States & observations
T = 12 steps
Viterbi accuracy:
Setup
Transition A (rows = from)
Emission B (rows = state)
Controls
Stats
Viterbi log-prob
Correct states
Sequence prob
Status
Ready
Info & Theory

A hidden Markov model (HMM) describes a system that hops between unseen hidden states (here Sunny / Rainy / Foggy) while emitting visible observations (Walk / Shop / Clean). You only see the observations; the states are hidden.

Two matrices

  • Transition A: A[i][j] is the probability of moving from state i to state j.
  • Emission B: B[i][k] is the probability that state i emits observation k.

Sampling

Start from the initial distribution, draw a state, emit an observation from B, then draw the next state from A. Repeat T times to build the observation strip and the (hidden) true path.

Viterbi decoding

The Viterbi algorithm finds the single most likely state path. It fills a trellis with δ_t(j) = max_i δ_{t−1}(i)·A[i][j]·B[j][o_t], stores back-pointers, then traces back from the best final state. The highlighted path on the lattice is this recovered sequence.

Forward algorithm

The forward pass sums over all paths to give per-step state probabilities P(state | observations) and the total sequence likelihood P(O). We use log-arithmetic to avoid underflow.

About Hidden Markov Model

A Hidden Markov Model (HMM) describes a system where an underlying process transitions between hidden states according to Markov dynamics (each state depends only on the previous one), and each state emits observable outputs with known probabilities. The "hidden" aspect is that we can only see the emissions, not the states themselves — our goal is to infer the hidden sequence from observations.

Three canonical problems are solved with HMMs: evaluation (computing the probability of an observation sequence given a model, via the Forward algorithm), decoding (finding the most likely hidden state sequence, via the Viterbi algorithm), and learning (estimating the model parameters that best explain observed data, via the Baum–Welch expectation-maximisation algorithm).

HMMs have wide applications across science and engineering. In speech recognition, phonemes are hidden states and acoustic features are emissions. In computational biology, HMMs model gene structures, protein domains, and evolutionary conservation. They also underlie part-of-speech tagging in NLP, financial regime detection, and anomaly detection in sensor time series.

Frequently Asked Questions

What distinguishes a Hidden Markov Model from a regular Markov chain?

In a regular Markov chain, states are directly observable. In an HMM, the states are hidden and only indirectly revealed through noisy or incomplete emissions. The model must infer the underlying state sequence from the observed output symbols.

What is the Viterbi algorithm?

The Viterbi algorithm efficiently finds the most probable sequence of hidden states given an observation sequence using dynamic programming. It runs in O(T·K²) time, where T is the sequence length and K is the number of states, making it tractable for practical problems.

How are HMMs trained?

HMMs are typically trained using the Baum–Welch algorithm, an instance of Expectation-Maximisation. It iterates between computing state occupation probabilities (E-step) and updating transition and emission parameters to maximise the likelihood of observed data (M-step).

Are HMMs still used in modern speech recognition?

HMMs formed the backbone of speech recognition from the 1980s through the 2000s. Modern systems use deep neural networks, but many hybrid architectures still combine neural acoustic models with HMM-based sequence decoding. Pure HMM systems remain in use for low-resource or embedded applications.

What is the Markov assumption and when does it fail?

The Markov assumption states that the next state depends only on the current state, not the full history. It fails when long-range dependencies matter — for example, grammatical agreement over many words in language. Higher-order Markov models and neural sequence models relax this assumption.