An HMM has hidden states Rainy (R) and Sunny (S), initial probabilities π=(0.6,0.4), transitions P(R→R)=0.7, P(R→S)=0.3, P(S→R)=0.4, P(S→S)=0.6, and emissions P(walk∣R)=0.1, P(shop∣R)=0.4, P(walk∣S)=0.6, P(shop∣S)=0.3. Using the Viterbi algorithm, what is the most likely hidden sequence for the observations (walk, shop)?

Probabilistic models, Hidden Markov models
Level L2 Elementary. Length Medium. Nature Mat Mathematical.

What's being asked

Whether you can run max-product dynamic programming with back-pointers, and know how it differs from the forward algorithm.

Key idea

Write zt for the hidden state and ot for the observation at time t, aij=P(zt+1=j∣zt=i) for transitions and bj(o)=P(o∣zt=j) for emissions. δt(j)=max⁡z1:t−1P(z1:t−1,zt=j,o1:t), the probability of the best path that ends in state j at time t, satisfies δt+1(j)=bj(ot+1)max⁡iδt(i) aij, with back-pointer ψt+1(j)=arg⁡max⁡iδt(i) aij. It is the forward algorithm with the sum replaced by a max.

Steps

  1. Initialise: δ1(R)=0.6×0.1=0.06, δ1(S)=0.4×0.6=0.24.
  2. δ2(R)=0.4×max⁡(0.06×0.7, 0.24×0.4)=0.4×max⁡(0.042, 0.096)=0.0384, back-pointer S.
  3. δ2(S)=0.3×max⁡(0.06×0.3, 0.24×0.6)=0.3×max⁡(0.018, 0.144)=0.0432, back-pointer S.
  4. Terminate: max⁡(0.0384, 0.0432)=0.0432 at z2=S; follow the back-pointer to z1=S. The most likely sequence is (Sunny, Sunny), with joint probability P(z,o)=0.0432.
  5. Contrast: the forward algorithm sums over paths to get P(o1:T); Viterbi maximises to get the single best path. The best path need not equal the sequence of individually most likely states from forward–backward.

Common mistakes

How long your answer should be

A two-column table of δ values with back-pointers, then one line for the path; 3 minutes.

Last checked on 4 December 2026.

Read this

Previous in Probabilistic models: How is naive Bayes drawn as a graphical model, and what is its independence assumption? A spam filter has P(spam)=0.4. The word "offer" appears in 60% of spam and 10% of ham (non-spam) emails; the word "meeting" appears in 30% of spam and 50% of ham. Using only these two words, what is P(spam∣offer,meeting) for an email that contains both?
Next in Probabilistic models: What is the conditional distribution of a partitioned multivariate Gaussian? If (x1,x2) is jointly Gaussian with means μ1=1, μ2=2, variances Σ11=4, Σ22=2 and covariance Σ12=2, what is p(x1∣x2=3)?