There are two different sentences that sound like the same sentence:

What is the most likely hidden path?
What is the most likely hidden state at each time?

A hidden Markov model makes them different.

That difference is not a numerical nuisance. It is a loss function.

Suppose the hidden states are \(q_1,\ldots,q_T\) and the observations are \(o_1,\ldots,o_T\). The model has an initial distribution \(\pi\), transition probabilities \(a_{ij}\), and emission probabilities \(b_j(o)\). The joint probability of one hidden story is

\[p(q_{1:T},o_{1:T}) = \pi_{q_1}b_{q_1}(o_1) \prod_{t=2}^T a_{q_{t-1},q_t}b_{q_t}(o_t).\]

Viterbi decoding asks for the single path with the largest joint probability:

\[\hat q^{\mathrm{path}} = \arg\max_{q_{1:T}} p(q_{1:T},o_{1:T}).\]

Because \(p(o_{1:T})\) is the same denominator for every path, this is also the maximum posterior path.

Posterior decoding asks a different question. It computes

\[\gamma_t(i)=p(q_t=i\mid o_{1:T})\]

and then chooses the best label at each time:

\[\hat q^{\mathrm{marginal}}_t = \arg\max_i \gamma_t(i).\]

The first answer is a story. The second answer is a set of scenes.

The Same Trellis, Two Algebras

The forward recursion sums over all paths that end in a state:

\[\alpha_t(j) = \left(\sum_i \alpha_{t-1}(i)a_{ij}\right)b_j(o_t).\]

The Viterbi recursion changes one word:

\[\delta_t(j) = \max_i \delta_{t-1}(i)a_{ij}b_j(o_t).\]

That replacement, \(\sum \rightarrow \max\), is the whole personality change. The forward-backward algorithm is doing accounting over all explanations. The Viterbi algorithm is pruning all but the best prefix into each state.

Both are dynamic programs on the same trellis. They do not answer the same question.

This is why a marginal label sequence can be strange. It may pick the locally most likely state at every time and still be a weak whole path. In models with zero-probability transitions, it can even stitch together an impossible path. Mark Stamp’s HMM tutorial uses a small tree-ring example to make exactly this point: the dynamic-programming best sequence and the per-position HMM posterior sequence need not match, even when all transitions are legal.1

A Small Alarm System

The lab below uses a two-state HMM:

Q = quiet
R = risk
C = calm observation
A = alarm observation

The sensor is only moderately reliable. The hidden state is sticky: once the world is quiet, it tends to stay quiet; once risky, it tends to stay risky.

With the default settings:

observations                 CACAACACCA
Viterbi path                 QQQQQQQQQQ
posterior labels             QRRRRRRQQR
disagreements                7 / 10
Viterbi path posterior mass  9.695%
posterior-label path mass    0.390%
expected correct labels      5.578 vs 4.922
log p(observations)          -7.294

The posterior labels win the pointwise game: they would score more expected correct states if every time step were graded independently. But as a story, QRRRRRRQQR pays for too many switches. The Viterbi decoder says: all those borderline alarms are cheaper to explain as sensor noise than as a rapid hidden state itinerary.

Deterministic browser experiment. The code computes forward-backward posteriors and the Viterbi path in the log domain, then scores both decoded sequences under the same HMM.

Try three moves.

  1. Lower Persistence toward 55%. The transition penalty weakens. In the default bursty sequence, the disagreement can disappear because switching is no longer expensive enough to make a smooth story dominate.
  2. Raise Sensor accuracy. Alarms become harder to explain away. The posterior probabilities sharpen, and eventually Viterbi has to buy switches too.
  3. Change Pattern to alternating. This is the adversarial case for a sticky hidden state: every local clue wants to flip, while the path objective asks how many flips a plausible story can afford.

The lab is intentionally small, but the warning scales.

When The Objective Changes The Product

In speech recognition, a Viterbi decoder may return one word sequence. In a biosequence model, it may return one gene annotation. In a tracking problem, it may return one trajectory. In all of these cases, the answer has a contract: one coherent sequence.

But sometimes the product is not a coherent sequence. It is a per-time heatmap, an uncertainty band, a calibrated alert probability, or an expected number of correct labels. Then marginal posteriors are not a consolation prize. They are the object.

This is the same split that appears in coding theory. Viterbi decoding is a maximum-likelihood sequence method. Bahl, Cocke, Jelinek, and Raviv developed a forward-backward style algorithm for a different target: posterior state and transition probabilities, useful for minimizing symbol error rather than whole word/path error.2

That distinction is easy to miss because both algorithms draw the same lattice. The data structure is not the objective.

A Useful Review Checklist

When I see HMM-style decoding in a system, I want four questions answered.

First: what loss is being optimized? Whole-sequence accuracy, per-label accuracy, calibrated state probability, change-point timing, or downstream utility?

Second: are impossible transitions actually impossible, or merely rare? A marginal decoder can return adjacent labels that are individually likely but not jointly admissible if the transition matrix has zeros.

Third: are the probabilities being normalized in log space? The classic HMM recursions multiply many probabilities. Without scaling or logs, long sequences turn inference into an underflow experiment.

Fourth: is the path being treated as truth? A Viterbi path can have small posterior mass even when it is the largest single path, because the remaining mass is spread across many near-ties. In the default lab, the best path has only about 9.7% posterior mass. That is not a confession. It is the posterior saying: there are many plausible stories.

The One Sentence I Trust

Viterbi gives the best complete story under the model.

Forward-backward tells you how much the model believes each scene after reading the whole story.

Do not ask one of them to do the other’s job.

Paper Trail

  1. Mark Stamp, “A Revealing Introduction to Hidden Markov Models”, 2021 version. The tutorial explicitly contrasts the dynamic-programming best sequence with per-position posterior decoding in its tree-ring example. 

  2. L. R. Bahl, J. Cocke, F. Jelinek, and J. Raviv, “Optimal Decoding of Linear Codes for Minimizing Symbol Error Rate”, IEEE Transactions on Information Theory, 1974. DOI: 10.1109/TIT.1974.1055186