viterbi
Builtin index · Statistics & ML
result = hmm.viterbi(obs)
Input: obs · Output: result
What it is
Runs the Viterbi algorithm on an hmm model hmm for a length-T integer observation sequence obs (same encoding as forward).
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = hmm.viterbi(obs) | obs vectornot documented yet | result vector | Returns a length-T vector of integer hidden-state indices: the single most likely path, not a distribution over paths. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
model = hmm(transition, emission, initial)
path = model.viterbi(obs) # one call, entire 8000-step decode
from tour
P = [0.9, 0.5, 0.1, 0.5] as matrix(2, 2) # column-major: rows [0.9 0.1], [0.5 0.5]
chain = markov_chain(P, 0)
print("stationary: {round(chain.stationary(), 4)}")
print("3-step: {round(chain.transition ^ 3, 4)}")
trans = [0.7, 0.4, 0.3, 0.6] as matrix(2, 2)
emit = [0.5, 0.1, 0.4, 0.3, 0.1, 0.6] as matrix(2, 3)
h = hmm(trans, emit, [0.6, 0.4])
print("P(normal, cold, dizzy) = {round(h.forward([0, 1, 2]), 5)}")
print("most likely path = {h.viterbi([0, 1, 2])}")
output
stationary: [0.8333, 0.1667] 3-step: [0.844, 0.156; 0.78, 0.22] P(normal, cold, dizzy) = 0.03628 most likely path = [0, 0, 1]
from statistics-ml
How it works
viterbi(model, obs) — callable as model.viterbi(obs). The HMM's most likely hidden-state path given the 0-based observation-symbol sequence obs (Viterbi decoding). See hmm_viterbi's own doc comment for the dynamic program.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.