Viterbi algorithm


The Viterbi algorithm is a dynamic programming algorithm for finding the most likely sequence of hidden states—called the Viterbi path—that results in a sequence of observed events, especially in the context of Markov information sources and hidden Markov models.
The algorithm has found universal application in decoding the convolutional codes used in both CDMA and GSM digital cellular, dial-up modems, satellite, deep-space communications, and 802.11 wireless LANs. It is now also commonly used in speech recognition, speech synthesis, diarization, keyword spotting, computational linguistics, and bioinformatics. For example, in speech-to-text, the acoustic signal is treated as the observed sequence of events, and a string of text is considered to be the "hidden cause" of the acoustic signal. The Viterbi algorithm finds the most likely string of text given the acoustic signal.

History

The Viterbi algorithm is named after Andrew Viterbi, who proposed it in 1967 as a decoding algorithm for convolutional codes over noisy digital communication links. It has, however, a history of multiple invention, with at least seven independent discoveries, including those by Viterbi, Needleman and Wunsch, and Wagner and Fischer.
"Viterbi path" and "Viterbi algorithm" have become standard terms for the application of dynamic programming algorithms to maximization problems involving probabilities.
For example, in statistical parsing a dynamic programming algorithm can be used to discover the single most likely context-free derivation of a string, which is commonly called the "Viterbi parse". Another application is in target tracking, where the track is computed that assigns a maximum likelihood to a sequence of observations.

Extensions

A generalization of the Viterbi algorithm, termed the max-sum algorithm can be used to find the most likely assignment of all or some subset of latent variables in a large number of graphical models, e.g. Bayesian networks, Markov random fields and conditional random fields. The latent variables need in general to be connected in a way somewhat similar to an HMM, with a limited number of connections between variables and some type of linear structure among the variables. The general algorithm involves message passing and is substantially similar to the belief propagation algorithm.
With the algorithm called iterative Viterbi decoding one can find the subsequence of an observation that matches best to a given hidden Markov model. This algorithm is proposed by Qi Wang et al. to deal with turbo code. Iterative Viterbi decoding works by iteratively invoking a modified Viterbi algorithm, reestimating the score for a filler until convergence.
An alternative algorithm, the Lazy Viterbi algorithm, has been proposed. For many applications of practical interest, under reasonable noise conditions, the lazy decoder is much faster than the original Viterbi decoder. While the original Viterbi algorithm calculates every node in the trellis of possible outcomes, the Lazy Viterbi algorithm maintains a prioritized list of nodes to evaluate in order, and the number of calculations required is typically fewer than the ordinary Viterbi algorithm for the same result. However, it is not so easy to parallelize in hardware.

Pseudocode

This algorithm generates a path, which is a sequence of states that generate the observations with, where is the number of possible observations in the observation space.
Two 2-dimensional tables of size are constructed:
The table entries are filled by increasing order of :
with and as defined below. Note that does not need to appear in the latter expression, as it's non-negative and independent of and thus does not affect the argmax.
;Input:
;Output
function VITERBI
for each state do


end for
for each observation do
for each state do


end for
end for


for do


end for
return
end function
;Explanation:
Suppose we are given a hidden Markov model with state space, initial probabilities of being in state and transition probabilities of transitioning from state to state. Say, we observe outputs. The most likely state sequence that produces the observations is given by the recurrence relations
Here is the probability of the most probable state sequence responsible for the first observations that have as its final state. The Viterbi path can be retrieved by saving back pointers that remember which state was used in the second equation. Let be the function that returns the value of used to compute if, or if. Then
Here we're using the standard definition of arg max.
The complexity of this implementation is. A better estimation exists if the maximum in the internal loop is instead found by iterating only over states that directly link to the current state. Then using amortized analysis one can show that the complexity is, where is the number of edges in the graph.

Example

Consider a village where all villagers are either healthy or have a fever and only the village doctor can determine whether each has a fever. The doctor diagnoses fever by asking patients how they feel. The villagers may only answer that they feel normal, dizzy, or cold.
The doctor believes that the health condition of his patients operates as a discrete Markov chain. There are two states, "Healthy" and "Fever", but the doctor cannot observe them directly; they are hidden from him. On each day, there is a certain chance that the patient will tell the doctor he is "normal", "cold", or "dizzy", depending on their health condition.
The observations along with a hidden state form a hidden Markov model, and can be represented as follows in the Python programming language:

obs =
states =
start_p =
trans_p =
emit_p =

In this piece of code, start_p represents the doctor's belief about which state the HMM is in when the patient first visits. The particular probability distribution used here is not the equilibrium one, which is approximately . The transition_p represents the change of the health condition in the underlying Markov chain. In this example, there is only a 30% chance that tomorrow the patient will have a fever if he is healthy today. The emission_p represents how likely each possible observation, normal, cold, or dizzy is given their underlying condition, healthy or fever. If the patient is healthy, there is a 50% chance that he feels normal; if he has a fever, there is a 60% chance that he feels dizzy.
The patient visits three days in a row and the doctor discovers that on the first day he feels normal, on the second day he feels cold, on the third day he feels dizzy. The doctor has a question: what is the most likely sequence of health conditions of the patient that would explain these observations? This is answered by the Viterbi algorithm.

def viterbi:
V =
for st in states:
V =
# Run Viterbi when t > 0
for t in range:
V.append
for st in states:
max_tr_prob = V * trans_p
prev_st_selected = states
for prev_st in states:
tr_prob = V * trans_p
if tr_prob > max_tr_prob:
max_tr_prob = tr_prob
prev_st_selected = prev_st
max_prob = max_tr_prob * emit_p =
for line in dptable:
print
opt =
max_prob = 0.0
previous = None
# Get most probable state and its backtrack
for st, data in V.items:
if data > max_prob:
max_prob = data
best_st = st
opt.append
previous = best_st
# Follow the backtrack till the first observation
for t in range:
opt.insert
previous = V
print
def dptable:
# Print a table of steps from dictionary
yield " ".join
for state in V:
yield "%.7s: " % state + " ".join

The function viterbi takes the following arguments: obs is the sequence of observations, e.g. ; states is the set of hidden states; start_p is the start probability; trans_p are the transition probabilities; and emit_p are the emission probabilities. For simplicity of code, we assume that the observation sequence obs is non-empty and that trans_p and emit_p is defined for all states i,j.
In the running example, the forward/Viterbi algorithm is used as follows:

viterbi

The output of the script is

$ python viterbi_example.py
0 1 2
Healthy: 0.30000 0.08400 0.00588
Fever: 0.04000 0.02700 0.01512
The steps of states are Healthy Healthy Fever with highest probability of 0.01512

This reveals that the observations were most likely generated by states . In other words, given the observed activities, the patient was most likely to have been healthy both on the first day when he felt normal as well as on the second day when he felt cold, and then he contracted a fever the third day.
The operation of Viterbi's algorithm can be visualized by means of a
trellis diagram. The Viterbi path is essentially the shortest
path through this trellis.

General references