Hidden Markov Models › Computing Sequence Probabilities
Language AI HandbookAll chapters Hidden Markov Models › Computing Sequence ProbabilitiesOne section · Sections in this chapter4 / 13 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Given an HMM, how do we compute the probability of a particular observation sequence and state sequence? This question has two related but distinct versions. You might want to know the probability of a specific combination of words and tags: for example, how likely is the sentence "the dog runs" tagged as "DT NN VB"? Or you might want to know the probability of the observation sequence alone, summed over all possible hidden state sequences: how likely is the sentence "the dog runs" to appear at all, regardless of how it is tagged? The first question gives you the joint probability ; the second gives you the marginal . Both are important, and the joint probability decomposition is the foundation for computing either one.
The joint probability factors according to the HMM structure, exploiting the independence assumptions to decompose a complex probability into simple terms.
For an observation sequence and state sequence , the joint probability is:
where:
- : the joint probability of observing sequence while the hidden states follow sequence
- : the initial probability of starting in state , denoted
- : the emission probability of the first observation given the first state, denoted
- : the product over all positions from 2 to
- : the transition probability from state to state , denoted
- : the emission probability of observation from state , denoted
The formula captures the generative story of an HMM: start in some state (with probability ), emit the first word (with probability ), then repeatedly transition to a new state (with probability ) and emit the next word (with probability ). This story is "generative" because you can use it to generate sentences: sample a starting tag from , sample a word from , sample a next tag from , sample the next word from , and so on. The HMM describes a process that could plausibly have produced the training data, and parameter estimation is the task of finding the process whose description fits that data best.
Let's compute this for a concrete example: the sentence "the dog runs" with tags "DT NN VB." We'll trace through each step of the computation, showing how the probabilities multiply together to give the joint probability of this particular word-tag pairing.
def compute_joint_probability(observations, states, pi, A, B):
"""Compute P(observations, states) for an HMM."""
n = len(observations)
# Start with initial probability and first emission
prob = pi[states[0]] * B[states[0]][observations[0]]
# Multiply by transition and emission for each subsequent position
for t in range(1, n):
transition_prob = A[states[t - 1]][states[t]]
emission_prob = B[states[t]][observations[t]]
prob *= transition_prob * emission_prob
return prob
# Our example sentence
observations = ["the", "dog", "runs"]
tag_sequence = ["DT", "NN", "VB"]
joint_prob = compute_joint_probability(observations, tag_sequence, pi, A, B)Computing P('the dog runs', 'DT NN VB')
==================================================
1. Initial: P(DT) = 0.6
2. Emission: P('the'|DT) = 0.9
3. Transition: P(NN|DT) = 0.8
4. Emission: P('dog'|NN) = 0.45
5. Transition: P(VB|NN) = 0.6
6. Emission: P('runs'|VB) = 0.5
Joint probability: 0.6 × 0.9 × 0.8 × 0.45 × 0.6 × 0.5
= 0.058320The joint probability is quite small because we're multiplying many probabilities together. For longer sequences, these products become vanishingly small, leading to numerical underflow. A 20-word sentence with probabilities around 0.1 at each step would produce a product on the order of , which many floating-point implementations round to zero. In practice, we work in log space, converting multiplications to additions:
Since is a monotonically increasing function, maximizing the log probability is equivalent to maximizing the probability itself. Working in log space prevents numerical underflow and is standard practice for probabilistic sequence models.