Did you know? A machine may make a high-pitched sound when something is wrong, yet occasionally make the same sound while operating normally. The sound is visible to a sensor, but the machine’s real internal condition is not directly observable. How can software reason across several readings instead of overreacting to one noisy clue?
A hidden Markov model (HMM) is a probabilistic model for sequences with hidden states that generate observable signals. It represents how states tend to change from one time step to the next and how likely each state is to produce each observation.
Quick answer: define possible hidden states → specify starting probabilities → describe transitions → describe emissions → observe a sequence → compute likely states or sequence probabilities → test the assumptions and uncertainty. The mathematics helps infer plausible hidden patterns. It cannot directly reveal an unobserved reality with certainty.
We will use an invented two-state machine to make the idea concrete, with arithmetic you can check by hand.
Hidden Markov models in 30 seconds
- Hidden state: an unobserved condition, such as Stable or Vibrating, that the model assumes exists.
- Observation: a measurement or symbol the system actually records, such as Low or High sensor activity.
- Initial probabilities: model beliefs about the state at the first time step.
- Transition probabilities: the probability of moving from one state to another in one step.
- Emission probabilities: the probability of an observation given the current state.
- Markov assumption: the next hidden state depends on the current hidden state rather than the full state history, in the standard first-order HMM.
- Conditional observation assumption: an observation is generated from the current hidden state in the standard model.
- Decoding: inferring a likely hidden-state sequence from observed signals.
A fictional sensor experiment
Imagine a small machine with two possible hidden states: S = Stable and V = Vibrating. At each time step a simple sensor reports either Low or High vibration.
The model begins with P(S at time 1) = 0.8 and P(V at time 1) = 0.2. These are invented teaching probabilities, not specifications from any real machine.
If the machine is Stable now, suppose it remains Stable at the next step with probability 0.9 and shifts to Vibrating with probability 0.1.
If the machine is Vibrating now, suppose it becomes Stable next with probability 0.3 and remains Vibrating with probability 0.7.
The sensor is imperfect: in state S it says Low with probability 0.8 and High with probability 0.2. In state V it says Low with probability 0.2 and High with probability 0.8.
Step 1: Separate states from observations
Students often mix up what a model assumes internally with what a sensor records. Stable and Vibrating are our hidden state labels; Low and High are the observed output labels.
The sensor’s High reading is more likely in state V, but it is still possible in state S. One reading is therefore not a perfect map of the hidden state.
Step 2: Ensure starting probabilities add to one
The initial probabilities represent a complete set of alternatives at the first step. Our example has 0.8 + 0.2 = 1.
If several hidden states are possible, the complete list of starting probabilities should likewise sum to one. Zero may be a deliberate structural restriction, but it can also hide an unjustified belief that a state is impossible.
Step 3: Build a valid transition table
From Stable, the next-step probabilities are S→S = 0.9 and S→V = 0.1. From Vibrating, they are V→S = 0.3 and V→V = 0.7.
Each row must sum to one: 0.9 + 0.1 = 1 and 0.3 + 0.7 = 1. Rows describe possible next states conditioned on the current state.
Do not automatically reverse a conditional probability. P(V next | S now) and P(S next | V now) are different quantities here.
Step 4: Build an emission table
When the hidden state is S, our sensor has P(Low | S) = 0.8 and P(High | S) = 0.2. When it is V, P(Low | V) = 0.2 and P(High | V) = 0.8.
Again, each row sums to one, because it gives a distribution across possible observations for one hidden state.
A more advanced model might emit continuous measurements using a Gaussian distribution rather than one of two simple categories. The underlying distinction between state and observation remains.
Step 5: Observe two readings
Now suppose we record the sequence Low, High. We want to know which hidden-state sequences could plausibly have produced those two readings.
There are four two-step paths: SS, SV, VS and VV. The HMM model combines initial state probability, first emission, transition probability and second emission for each path.
Step 6: Calculate the Stable–Stable path
For SS, multiply P(S first) × P(Low | S) × P(S next | S) × P(High | S).
That is 0.8 × 0.8 × 0.9 × 0.2 = 0.1152. It may seem odd that the model can remain Stable after a High reading. The answer is that High is possible, though less likely, while the Stable-to-Stable transition is quite likely.
Step 7: Calculate the other paths
For SV the product is 0.8 × 0.8 × 0.1 × 0.8 = 0.0512.
For VS the product is 0.2 × 0.2 × 0.3 × 0.2 = 0.0024.
For VV the product is 0.2 × 0.2 × 0.7 × 0.8 = 0.0224.
These values are joint probabilities of a hidden path and the observed Low, High sequence under our fictional model. They are not yet the conditional probabilities of each hidden path given the readings.
Step 8: Add paths to find the observation likelihood
All four hidden-state paths can generate the two readings. To find the probability of the observed sequence under this model, add their joint probabilities:
0.1152 + 0.0512 + 0.0024 + 0.0224 = 0.1912.
This number is the model’s likelihood for the observed Low, High sequence given its defined parameters. It is not an accuracy score or evidence that the model assumptions are correct.
Step 9: Find the single most likely hidden path
The largest path probability is 0.1152 for SS, so the most likely complete state path is Stable, Stable in this deliberately small example.
That does not mean the High sensor reading is wrong. It means the combination of a strong starting belief in Stable, a high tendency to remain Stable and imperfect sensor emissions makes SS the most probable complete path.
For longer sequences, enumerating every path becomes impractical. The Viterbi algorithm uses dynamic programming to find the most probable hidden path efficiently.
Step 10: Do not confuse a path with a state’s probability
Suppose you ask a different question: What is the probability that the second hidden state is Vibrating, given both readings? Sum the two paths that end in V (SV and VV) and divide by the total observation likelihood.
The numerator is 0.0512 + 0.0224 = 0.0736. Divide by 0.1912 to get approximately 0.385, or 38.5%.
So the most likely complete hidden path is SS, while the probability of a Vibrating state at the second time step is about 38.5%. They answer different questions. Never swap a path maximum for a per-time posterior.
Step 11: What the forward algorithm calculates
The forward algorithm sums over hidden-state possibilities efficiently as it processes a sequence. In our tiny example we directly added four paths to obtain 0.1912.
With longer data, dynamic programming avoids recomputing an exponential number of full sequences. That makes likelihood calculation practical for many real sequential tasks.
Step 12: What the backward algorithm contributes
The backward algorithm works from later observations towards earlier positions. Combined with the forward calculation, it can help estimate marginal probabilities for hidden states at each time step.
This is useful when the question is not ‘Which one full state path wins?’ but ‘How uncertain are we about the hidden state at each moment?’
State posteriors express uncertainty under model assumptions; they are not direct observations of the hidden state.
Step 13: How an HMM learns its probabilities
In an unsupervised HMM, the true state labels may be unknown in training observations. An expectation-maximization method commonly called Baum–Welch can iteratively estimate start, transition and emission parameters.
The method uses current parameters to estimate hidden-state responsibilities and then updates parameters to better fit observations. It may converge to a local optimum, so initialization and model comparison matter.
Some applications use known state annotations or constraints; ‘hidden’ describes what is not directly observed in the inference problem, not a ban on ever using supervised information.
Step 14: Why the Markov assumption is strong
A first-order Markov model assumes the next hidden state depends on the present state, not the whole earlier hidden history after conditioning on that state.
Real processes may depend on longer histories, time spent in the current state, day of week or external forces. If these factors matter, a basic two-state stationary HMM can be misspecified.
More elaborate state-space models, higher-order variants and other sequence methods can address different dependencies, but they introduce additional complexity and assumptions.
Step 15: State duration can expose a weakness
In a basic HMM with fixed self-transition probability, state durations follow a geometric-style pattern. That may be unsuitable if a machine is required to stay in one mode for at least a set number of cycles.
An explicit-duration or hidden semi-Markov approach can be considered when state sojourn times are central. Use it only when the domain and validation justify that extra structure.
Step 16: Why state names may change between training runs
A model fitted without known state labels may discover components numbered 0 and 1, but that numbering is arbitrary. Another fit can swap the numbers and still represent the same probability model.
Even descriptive labels such as Stable and Vibrating require external validation. A high-vibration component is not necessarily a confirmed mechanical fault.
Training with several initializations and checking observed emission patterns helps avoid telling an unwarranted story about latent states.
Step 17: Compare HMMs with ordinary clustering
In Gaussian mixture clustering, observations can receive soft membership weights in different components, without necessarily modelling how those components change across a sequence.
An HMM adds an explicit transition structure. Two observations with the same sensor reading may receive different inferred state probabilities depending on the neighbouring readings and transition dynamics.
That is why an HMM can be useful for sequential measurements, not simply for a bag of unrelated records.
Step 18: How to test a hidden-state model
Set aside complete sequences for evaluation rather than randomly mixing neighbouring time steps between training and test sets. Otherwise adjacent readings may leak information and create an exaggerated performance estimate.
Compare the model’s likelihood on unseen sequences against sensible baselines, check whether predicted emissions resemble actual measurement patterns and examine sensitivity to the assumed number of states.
If true states are observable in a carefully annotated subset, use that subset to assess decoding accuracy. Report uncertainty and failure cases, not only the most attractive visualisation.
Step 19: Where hidden Markov models can help
Speech and language research: older and specialized sequence models use HMMs to represent changes between latent states as signals unfold.
Industrial monitoring: machine behaviour may have operating modes that are inferred from sensor traces, subject to independent equipment checks.
Biological sequences: HMMs can model different hidden regimes along sequences when assumptions are suitable.
Classroom learning: an invented machine example teaches conditional probability, sequence reasoning and the difference between data and assumptions.
Step 20: Limitations that should appear beside the diagram
An HMM cannot guarantee that its inferred states correspond to clean real categories. Wrong numbers of states, poor emissions, changing conditions and nonstationary transitions can all produce misleading classifications.
A long run of highly predictable readings does not prove the process is governed by a first-order Markov chain. It may be predictable for another reason.
For consequential predictions, use domain expertise and independent evaluation rather than treating decoded states as an automatic diagnosis.
Five common mistakes
- ‘The observed signal is the hidden state.’ No. The signal is generated conditionally on a hidden state in the model.
- ‘A high emission probability proves a state.’ No. Starting and transition probabilities also shape inference.
- ‘The Viterbi path gives the probability of each state.’ No. Viterbi finds one most likely full path, whereas posterior marginal inference sums over paths.
- ‘The transition matrix can have any row totals.’ No. Each complete conditional distribution should sum to one.
- ‘A trained state label is an established real-world condition.’ No. It needs external meaning and validation.
Try it: the two-reading challenge
- Write the initial state probabilities S = 0.8 and V = 0.2.
- Write the two transition rows: from S, (0.9, 0.1); from V, (0.3, 0.7).
- Write the emission rows: P(Low, High | S) = (0.8, 0.2) and P(Low, High | V) = (0.2, 0.8).
- Use the observed sequence Low, High and multiply the four path probabilities: SS, SV, VS and VV.
- Find their sum, 0.1912. Circle SS as the most probable complete path.
- Explain why the posterior probability of V at time 2, about 0.385, is different from the winning path probability.
- Finally ask: if the model starts with different state probabilities, might the winning path change? Yes—test the assumption.
Frequently asked questions
What does hidden mean in a hidden Markov model?
The state that generates each observation is not directly known in the inference task; the model reasons about it using observations and transition assumptions.
What is the difference between transition and emission?
A transition describes how the hidden state changes over time. An emission describes how likely an observable signal is in a given hidden state.
What is the Viterbi algorithm?
It is a dynamic-programming procedure for finding a most probable full hidden-state path under a specified HMM and observation sequence.
What does the forward algorithm find?
It efficiently sums probability mass across possible state paths, allowing the likelihood of an observed sequence to be calculated.
Is an HMM always better than a neural network?
No. Different models fit different tasks and assumptions. Compare reliability, interpretability, data needs and performance on genuinely unseen sequences.
Can students learn HMMs without coding?
Yes. A two-state, two-observation worksheet demonstrates the essentials using multiplication, addition and conditional probability.
The one-minute takeaway
A hidden Markov model describes how hidden states move through time and how those states generate observable evidence. Transition and emission probabilities must be distinguished. Viterbi asks for one most likely path; forward and backward reasoning helps sum across possibilities. Every inference remains conditional on the model’s assumptions.
Trusted sources and related articles
- hmmlearn Official Tutorial — start, transition, emission, likelihood and decoding
- How Gaussian Mixture Models Find Overlapping Groups — mixtures without an HMM transition structure
- How Naive Bayes Classifies Text — another application of conditional probability
- scikit-learn: Common Pitfalls — data leakage and evaluation discipline
- How AI Works: Complete Overview — the larger explainer series
- Secondary 1 Mathematics Tutor Clementi — build fluency in step-by-step reasoning