File Exchange

image thumbnail

Forward Viterbi Algorithm

version 1.1 (1.83 KB) by

Forward Viterbi algorithm based on:

4 Ratings



View License

Forward Viterbi algorithm based on the Python code found at:

Also included is an example based on the one from the Wikipedia page for a simple 2-state model with 4 observations. To paraphrase:

Bob tells Alice his daily activities (observations) and Alice wants to determine the most likely weather each day (states). Since Alice lives far away, the weather is unknown to her (hidden). Assuming that the states behave as a Markov process, and the observations have a statistical dependency on the states, the Viterbi algorithm can find the most likely weather pattern (path).

More information can be found at:

Comments and Ratings (5)

weike yin


GC (view profile)

Dear sir
I run forward-viterbi with an observation sequence of 2 elements.
The output is sequence of 3 states and compute the probability of this sequence.
In fact the sequence of the best states is right but there is one element more and the probabilty correspond with the sequence with this extra state.
Sincerely yours

Sam Ham

The code works fine now, the previous bug was fixed, I just tested it.

Sam Ham


Justin (view profile)

foward_viterbi.m contains a bug not in the original python version. If the output is longer than 3 states then this line throws an error. The 2nd index to emit_p should be the position of the output token in the vector of possible output states, not the index into the observation vector.
p = emit_p(source_state,output) * trans_p(source_state,next_state);



Fixed the problem found by Justin. To make things simpler, observations are now numbered, rather than allowing strings.

MATLAB Release
MATLAB 7.5 (R2007b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video