Untangled

Topic 2.2

Viterbi

Given a hidden Markov model and an observed sequence, find the single hidden path that is most probable.

Where people get stuck

  • A cell is not the best path so far.Vs(t) is the best score among paths that end in state s at position t. You keep one winner per state, so every column holds two numbers here, one for E and one for I.
  • Choose first, emit once.For each node, take the best of previous score × transition over the incoming states. Multiply by the emission after choosing. The emission does not depend on which route you arrived by.
  • The table does not show the answer yet.The bigger score in a middle column may not lie on the final best path. Only after the last column do you pick the better end state and follow the saved pointers back.
  • Long products shrink to nothing.Work with log scores instead. Products become sums and the winning path stays the same.

The recurrence

Same hidden states as the gene model in lecture: E for a gene base, I for intergenic, and START and END that emit nothing. The slide's rule is candidate score = previous score × transition × emission.

Vs(1) = P(s|START) · P(x1|s)
Vs(t) = maxq [ Vq(t−1) · P(s|q) ] · P(xt|s)
pointers(t) = the q that wins the max
best path probability = maxs Vs(n) (END is forced, so no extra factor)

The default sequence ATGCC is the five-base example from lecture. The probabilities in the demos are illustrative numbers with the same structure as the lecture model, not the slide's own values.

Watch it run

Press Next column to fill the trellis one position at a time, then Backtrack. Click any node to see its arithmetic. Switch to log₂ scores to see products turn into sums. Left and right arrow keys also step.

Check yourself

Why keep both an E score and an I score at every position?

Which state is best at position t depends on what comes next. A worse prefix ending in I can still lead to the best full path if the following bases favor I. Keeping the best prefix for each ending state loses nothing, because under the Markov assumption the worse prefix can never beat the better one with the same continuation.

The best score in column 3 is in state E, but the best path ends in I. Is something wrong?

No. The winner of a middle column is only the best prefix to that point. The full best path is decided at the last column and traced back through the pointers.

Why does taking logs not change the answer?

Log is increasing, so if one product is larger than another, its log is larger too. A sum of logs of the factors equals the log of the product, which is why the same path wins.