Skip to content
Part IA Lent Term

Viterbi Algorithm: Full Worked Example

Setup: COVID HMM from 2022 Q9

Viterbi trellis showing the optimal state path M to H to H to H for the COVID infection level example with probabilities at each timestep

Training data (timesteps 1–7): hidden states L, M, H, H, H, M, M; observations +, ++, ++, +++, ++, ++, +.

Estimated parameters — Transition matrix AA (MLE, no smoothing):

qt1q_{t-1} / qtq_tLMH
L010
M02/31/3
H01/32/3

Emission matrix BB (MLE, no smoothing):

qtq_t / oto_t++++++
L100
M010
H02/31/3

Task

Timesteps 8–10, observations: +++, ++, ++. Unknown state sequence. Start from t=7 in state M. Find the most likely hidden-state path.

Initialisation at t=7t = 7

We are in state M at t=7. Set:

δL(7)=0δM(7)=1δH(7)=0\delta_L(7) = 0 \qquad \delta_M(7) = 1 \qquad \delta_H(7) = 0

t=8t = 8: Observation = +++

StateComputationδ\deltaψ\psi
LbL(+++)=0δL(8)=0b_L(\text{+++}) = 0 \Rightarrow \delta_L(8) = 00
MbM(+++)=0δM(8)=0b_M(\text{+++}) = 0 \Rightarrow \delta_M(8) = 00
Hmax[δM(7)aMHbH(+++),  δH(7)aHHbH(+++)]\max[\delta_M(7) \cdot a_{M \to H} \cdot b_H(\text{+++}),\; \delta_H(7) \cdot a_{H \to H} \cdot b_H(\text{+++})]19\frac{1}{9}M

δH(8)=11313=19\delta_H(8) = 1 \cdot \frac{1}{3} \cdot \frac{1}{3} = \frac{1}{9}

Best at t=8t = 8: H with δ=19\delta = \frac{1}{9}, came from M.

t=9t = 9: Observation = ++

StateComputationδ\deltaψ\psi
LNo viable path leads to L0
HδH(8)aHHbH(++)\delta_H(8) \cdot a_{H \to H} \cdot b_H(\text{++})481\frac{4}{81}H
MδH(8)aHMbM(++)\delta_H(8) \cdot a_{H \to M} \cdot b_M(\text{++})381\frac{3}{81}H

δH(9)=192323=481\delta_H(9) = \frac{1}{9} \cdot \frac{2}{3} \cdot \frac{2}{3} = \frac{4}{81} δM(9)=19131=127=381\delta_M(9) = \frac{1}{9} \cdot \frac{1}{3} \cdot 1 = \frac{1}{27} = \frac{3}{81}

Best at t=9t = 9: H with δ=481\delta = \frac{4}{81}, came from H.

t=10t = 10: Observation = ++

StateComputationδ\deltaψ\psi
HδH(9)aHHbH(++)\delta_H(9) \cdot a_{H \to H} \cdot b_H(\text{++})16729\frac{16}{729}H
MδH(9)aHMbM(++)\delta_H(9) \cdot a_{H \to M} \cdot b_M(\text{++})12729\frac{12}{729}H
LNo viable path leads to L0

δH(10)=4812323=16729\delta_H(10) = \frac{4}{81} \cdot \frac{2}{3} \cdot \frac{2}{3} = \frac{16}{729} δM(10)=481131=4243=12729\delta_M(10) = \frac{4}{81} \cdot \frac{1}{3} \cdot 1 = \frac{4}{243} = \frac{12}{729}

Best at t=10t = 10: H with δ=16729\delta = \frac{16}{729}, came from H.

Backtracking

Start from q10=Hq_{10}^* = H and follow backpointers:

q10=HψH(10)=Hq9=Hq_{10}^* = H \quad \Rightarrow \quad \psi_H(10) = H \quad \Rightarrow \quad q_9^* = H ψH(9)=Hq8=H\psi_H(9) = H \quad \Rightarrow \quad q_8^* = H ψH(8)=Mq7=M\psi_H(8) = M \quad \Rightarrow \quad q_7^* = M

Optimal state sequence (t=7–10): [M,H,H,H][M, H, H, H]. For t=8,9,10: H, H, H.

Trellis Table (Exam Layout)

Statet=7t=7 (M)t=8t=8 (+++)t=9t=9 (++)t=10t=10 (++)
L0000
M103/81 ←H12/729 ←H
H01/9 ←M4/81 ←H16/729 ←H

Bold = max per column. Arrows show backpointers. Trace backwards from t=10: H → H → H → M.

Key Observations

  • Zero emissions (bL(+++)=0b_L(\text{+++}) = 0, bM(+++)=0b_M(\text{+++}) = 0) permanently kill those paths at t=8. Without smoothing, dead paths never recover.
  • The transition aMH=1/3a_{M \to H} = 1/3 at t=8 gets the path to H, which then becomes the only viable route forward.
  • bH(++)=2/3b_H(\text{++}) = 2/3 gives H an edge over M at t=9 and t=10 despite bM(++)=1b_M(\text{++}) = 1, because H’s accumulated delta is higher at t=8.
  • The optimal path probability is 16729\frac{16}{729}, which is very small — typical for Viterbi with many multiplications. In practice, use log-space to avoid underflow.

Summary

TimestepObservationBest Stateδj(t)\delta_j(t)Source
8+++H1/91/9M
9++H4/814/81H
10++H16/72916/729H

Past paper questions: y2022p3q9