Tutorials › Stochastic Processes › Viterbi Paths Versus Pointwise Posterior Decoding

Hidden Markov Models · Tutorial 949 of 994

Viterbi Paths Versus Pointwise Posterior Decoding

Learn what each decoding rule optimizes, compute both from an HMM posterior, and recognize when pointwise decoding can disagree with or even violate path constraints.

Advanced 12 min read

What You'll Learn

  • Define Viterbi decoding and pointwise posterior decoding for a fixed observation sequence.
  • Distinguish maximizing a joint path probability from maximizing state probabilities one time at a time.
  • Compute smoothed state probabilities from normalized path weights.
  • Show why pointwise decoding minimizes posterior expected Hamming loss.
  • Identify how ties and zero-probability transitions affect decoded sequences.
  • Recognize a setting in which the two decoding rules agree.

Two Different Questions About a Hidden Path

After observing a sequence, there are at least two natural ways to report the hidden states. One is to select the single entire path with the largest posterior probability. The other is to select, at each time separately, the state with the largest posterior probability at that time. These procedures can give different sequences because the first compares probabilities of complete paths, while the second compares probabilities of individual states.

Let \((X_n)_{n\ge1}\) be a hidden Markov model on a finite state space \(S\), with initial distribution \(\pi\), transition matrix \(P=(p_{ij})\), and observations \(Y_1,\ldots,Y_T\). The transition matrix is row-stochastic: \(p_{ij}\ge0\) and \(\sum_{j\in S}p_{ij}=1\) for each \(i\). Conditional on the hidden states, the observations are independent, and the likelihood of observation \(y_n\) in state \(i\) is denoted \(g_i(y_n)\). The observation likelihoods may be probability masses or densities; assume the observed sequence has positive marginal likelihood, so its posterior distribution is defined.

Definition: For a hidden path \(x_{1:T}=(x_1,\ldots,x_T)\), its joint likelihood with the observations is \(w(x_{1:T})=\pi_{x_1}g_{x_1}(y_1)\prod_{n=2}^{T}p_{x_{n-1}x_n}g_{x_n}(y_n)\). The Viterbi path is any path maximizing \(w(x_{1:T})\). The smoothed state probability is \(\gamma_n(i)=\mathbb{P}(X_n=i\mid Y_1=y_1,\ldots,Y_T=y_T)\). Pointwise posterior decoding chooses, separately at each \(n\), a state \(\hat{x}_n\in\arg\max_{i\in S}\gamma_n(i)\).

The posterior probability of a complete path is its joint likelihood divided by the marginal likelihood of the observations. That denominator is the same for every path. Thus maximizing the joint likelihood is exactly the same as maximizing the posterior probability of the whole path. In contrast, \(\gamma_n(i)\) sums the posterior probabilities of all paths that pass through state \(i\) at time \(n\). It is not the probability of one particular path.

Proposition (The two decoding objectives): A Viterbi path maximizes the posterior probability of an exactly correct entire sequence. Pointwise posterior decoding minimizes the posterior expected number of incorrectly classified time points, also called Hamming loss. These objectives need not have the same optimizer.

Proof. Write \(X_{1:T}=(X_1,\ldots,X_T)\) and let \(z_{1:T}\) be any proposed decoded sequence. Under whole-sequence zero-one loss, the loss is \(0\) if \(X_{1:T}=z_{1:T}\) and \(1\) otherwise. Its posterior expectation is \(1-\mathbb{P}(X_{1:T}=z_{1:T}\mid Y_{1:T}=y_{1:T})\). Minimizing this quantity is therefore equivalent to maximizing the posterior probability of the entire path, which is the Viterbi objective.

For Hamming loss, the number of errors is \(\sum_{n=1}^{T}\mathbf{1}_{\{X_n\ne z_n\}}\). By linearity of conditional expectation, its posterior expectation is \(\sum_{n=1}^{T}\mathbb{P}(X_n\ne z_n\mid Y_{1:T}=y_{1:T})=\sum_{n=1}^{T}(1-\gamma_n(z_n))\). Each summand depends only on \(z_n\); hence the sum is minimized by choosing \(z_n\) to maximize \(\gamma_n(z_n)\) at every time. If there are ties, any maximizing state at each time is optimal for this loss. The two criteria optimize different quantities, so their optimizers need not agree. \(\square\)

A Case Where the Decoded Sequences Differ

The next example computes all path weights, then sums them to obtain the smoothed probabilities. The transition and emission probabilities are valid, but an intermediate observation strongly favors one state while persistence in the chain favors paths that remain in the same state.

Worked Example: A Pointwise Path That Is Not the Viterbi Path

Let \(S=\{0,1\}\), with initial distribution \(\pi=(\tfrac12,\tfrac12)\) and transition matrix \(P=\begin{bmatrix}\tfrac45&\tfrac15\\[2pt]\tfrac15&\tfrac45\end{bmatrix}\). Both row sums are \(\tfrac45+\tfrac15=1\). The observation alphabet is \(\{a,b,c\}\), with emission probabilities \(g_0=(\tfrac1{10},\tfrac1{10},\tfrac45)\) and \(g_1=(\tfrac3{10},\tfrac1{200},\tfrac{139}{200})\), in the order \(a,b,c\). The sums are \(\tfrac1{10}+\tfrac1{10}+\tfrac45=1\) and \(\tfrac{60}{200}+\tfrac1{200}+\tfrac{139}{200}=1\). Given observations \(a,b,a\), compare the two decoding rules.

Use the all-zero path as a baseline. For an observation \(a\), the emission likelihood ratio \(g_1(a)/g_0(a)\) is \((\tfrac3{10})/(\tfrac1{10})=3\); for \(b\), the ratio is \((\tfrac1{200})/(\tfrac1{10})=\tfrac1{20}\). Relative to a stay transition, a switch has ratio \((\tfrac15)/(\tfrac45)=\tfrac14\). Consequently, the path weight divided by the weight of \(000\) is the product of its emission ratios at times in state \(1\), multiplied by \(\tfrac14\) for each switch.

The eight relative weights, with every path listed, are \(000:1\), \(001:3/4\), \(010:1/320\), \(011:3/80\), \(100:3/4\), \(101:9/16\), \(110:3/80\), and \(111:9/20\). For instance, the weight for \(010\) is \((\tfrac1{20})(\tfrac14)^2=\tfrac1{320}\), while for \(101\) it is \(3\cdot3\cdot(\tfrac14)^2=\tfrac9{16}\).

Using denominator \(320\), the total relative weight is \(320+240+1+12+240+180+12+144=1149\), or \(\tfrac{1149}{320}\). The largest individual weight is \(1\), for path \(000\), so the Viterbi path is \(000\). To find the smoothed probability of state \(1\) at time 1, add the weights of paths \(100,101,110,111\): their numerators over \(320\) sum to \(240+180+12+144=576\). Thus \(\gamma_1(1)=\tfrac{576}{1149}\). By symmetry, \(\gamma_3(1)=\tfrac{576}{1149}\). At time 2, the weights of paths \(010,011,110,111\) sum to \(1+12+12+144=169\) over \(320\), so \(\gamma_2(1)=\tfrac{169}{1149}\).

Since \(576>\tfrac{1149}{2}=574.5\), the most probable state at times 1 and 3 is \(1\). Since \(169<574.5\), the most probable state at time 2 is \(0\). Pointwise posterior decoding therefore gives \(101\), different from the Viterbi path \(000\). The calculations also check the posterior normalization: the state-1 and state-0 numerators at each time add to \(1149\); at time 2 they are \(169\) and \(1149-169=980\).

This example shows why the path with the largest joint posterior probability need not use the most probable state at every time. A state’s smoothed probability collects weight from many paths. The Viterbi criterion instead compares each path’s weight individually.

When Pointwise Decoding Does Agree with Viterbi

Agreement is guaranteed in a useful special case: if the hidden states are conditionally independent given the observations, the posterior probability of a path factors into its one-time posterior probabilities. Maximizing the product can then be done separately at each time. The example below has a transition matrix with identical rows, so each new hidden state is drawn independently of the previous one.

Proposition: If, conditional on the observations, \(X_1,\ldots,X_T\) are independent, then every sequence formed by choosing a posterior-maximizing state at each time is a Viterbi path. This statement includes ties, provided the chosen states have positive posterior probability.

Proof. Conditional independence gives \(\mathbb{P}(X_{1:T}=x_{1:T}\mid Y_{1:T}=y_{1:T})=\prod_{n=1}^{T}\gamma_n(x_n)\). For any path \(x_{1:T}\), each factor satisfies \(\gamma_n(x_n)\le\max_{i\in S}\gamma_n(i)\). Multiplying these nonnegative inequalities gives \(\prod_n\gamma_n(x_n)\le\prod_n\max_i\gamma_n(i)\). A path that chooses a maximizing state at every time attains the right-hand side, so it maximizes the path posterior. If a factor maximum is zero, every state has zero posterior at that time, which would contradict the assumption that the observed sequence has a defined posterior distribution; thus the maximizing choices have positive posterior probability. \(\square\)

Worked Example: Independent Hidden States

Let \(S=\{0,1\}\), \(\pi=(\tfrac12,\tfrac12)\), and \(P=\begin{bmatrix}\tfrac12&\tfrac12\\[2pt]\tfrac12&\tfrac12\end{bmatrix}\). Each row sums to \(\tfrac12+\tfrac12=1\). For both states, the emission probabilities on alphabet \(\{a,b\}\) sum to one: \(g_0(a)=\tfrac34\), \(g_0(b)=\tfrac14\), \(g_1(a)=\tfrac14\), \(g_1(b)=\tfrac34\). Observe \(a,b\).

The transition rows do not depend on the current state, so the joint posterior factors across the two times. At time 1 the prior probabilities are equal, and observation \(a\) has likelihoods \(\tfrac34\) in state 0 and \(\tfrac14\) in state 1. Normalizing their sum \(\tfrac34+\tfrac14=1\) gives posterior probabilities \((\tfrac34,\tfrac14)\). At time 2, observation \(b\) gives posterior probabilities \((\tfrac14,\tfrac34)\).

The pointwise decoder is \(01\). The four path posterior probabilities, using the product factorization, are \(00:\tfrac34\cdot\tfrac14=\tfrac3{16}\), \(01:\tfrac34\cdot\tfrac34=\tfrac9{16}\), \(10:\tfrac14\cdot\tfrac14=\tfrac1{16}\), and \(11:\tfrac14\cdot\tfrac34=\tfrac3{16}\). Their sum is \(\tfrac{3+9+1+3}{16}=1\), and the largest is \(\tfrac9{16}\), for path \(01\). Thus both methods give the same answer, as the proposition predicts.

Ties, Constraints, and a Common Mistake

Pointwise decoding is a rule for selecting states at each time; it does not, by itself, enforce that adjacent selected states can follow one another under the transition matrix. When posterior maxima tie, the chosen combination can even have posterior probability zero. In such cases, it is important to distinguish the set of pointwise-optimal sequences from the set of valid paths with positive posterior probability.

Worked Example: Independent Tie-Breaking Can Violate a Transition Constraint

Let \(S=\{0,1\}\), \(\pi=(\tfrac12,\tfrac12)\), and \(P=\begin{bmatrix}0&1\\[2pt]1&0\end{bmatrix}\). Both row sums equal \(1\). Let both states emit the observed symbol \(a\) with probability \(1\), and observe \(a,a\). The only paths with positive probability are \(01\) and \(10\). Each has joint probability \(\tfrac12\), so each has posterior probability \(\tfrac12\) and is a Viterbi path.

At either time the smoothed probabilities are \(\gamma_n(0)=\gamma_n(1)=\tfrac12\): each of the two possible paths contributes probability \(\tfrac12\) to one of the states at that time. Thus both states maximize at both times. A tie-breaking rule that selects state 0 at each time reports \(00\), but \(p_{00}=0\), so this sequence has posterior probability zero and is not a Viterbi path. Choosing \(01\) or \(10\) is pointwise optimal as well and is a valid Viterbi path. The tie does not make \(00\) a valid path.

Common mistake. It is incorrect to say that the Viterbi path must be obtained by taking the most probable state at each time. In the first worked example, that calculation gives \(101\), whereas the largest individual path weight belongs to \(000\). The correction is to compute or recursively maximize complete-path weights for Viterbi decoding; use the smoothed probabilities when the target is minimum posterior expected Hamming loss.

Both summaries can be useful, but they answer different questions. A Viterbi path is appropriate when the decision is about the entire hidden sequence being exactly correct. Pointwise posterior decoding is appropriate when errors are counted one time point at a time. If the application requires a valid path and uses a timewise loss, independent pointwise choices may need an additional constrained optimization step; that changes the decision problem and should be stated explicitly.

Check Your Understanding

Use the objectives and calculations above to answer these questions.

  1. In one sentence, distinguish the posterior quantity maximized by Viterbi decoding from the quantity maximized by pointwise posterior decoding.
  2. Why does maximizing joint likelihood over paths give the same result as maximizing posterior probability of a path after the observations are fixed?
  3. For a decoded sequence \(z_{1:T}\), write its posterior expected Hamming loss in terms of \(\gamma_n(z_n)\).
  4. In the first worked example, why does the smoothed probability of state 1 at a time involve adding several path weights?
  5. In the deterministic-alternation example, which two paths have positive posterior probability, and why can the pointwise tie-breaking choice \(00\) not be a Viterbi path?