We can calculate the optimal path in a hidden Markov model using a
dynamic programming algorithm. This algorithm is widely known as
Viterbi Algorithm. Viterbi [10] devised this
algorithm for the decoding problem, even though its more general
description was originally given by Bellman [3].
Let X be a sequence of length L. for
and
,
we consider a path
ending at k, and the probability of
generating
the prefix
of X. Denote by vk(i) the maximal (over
all paths, ,
ending at k) such probability.
(11) |
vbegin(0) | = | 1 | (12) |
= | 0 | (13) |
(15) |
We can reconstruct the path
itself by keeping back pointers during the
recursive stage and tracing them.
Complexity: We calculate the values of
cells of the matrix V, spending O(|Q|) operations per cell.
The overall time complexity is therefore
and
the space complexity is
.
Since we are dealing with probabilities, the extensive
multiplication operations we perform may result in an underflow.
This can be avoided if we choose to work with logarithmic scores.
We can therefore define vk(i) to be the logarithmic score of
the most probable path for the prefix
that
ends in the state k.
We shall initialize:
vbegin(0) | = | 0 | (16) |
= | (17) |
The recursion will look like:
(18) |
Finally, the score for the best path
is:
(19) |