hmm
Builtin index · Statistics & ML
result = hmm(transition, emission, initial)
Input: transition, emission, initial · Output: result
What it is
Builds a discrete hidden Markov model from an (S, S) row-stochastic hidden-state transition matrix transition, an (S, O) row-stochastic emission matrix emission (O the number of observable symbols), and a length-S initial-state distribution initial.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = hmm(transition, emission, initial) | transition matrix or numbernot documented yetemission matrix or numbernot documented yetinitial matrix or numbernot documented yet | result Record | Returns a kind="hmm" Record with methods .forward/.viterbi. Parameter fitting (Baum-Welch) is not implemented — the transition/emission matrices must already be known. |
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])}")
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
hmm(transition, emission, initial) — a standard discrete Hidden Markov Model: transition is an (n,n) row-stochastic state-transition matrix (same contract as markov_chain's own P), emission is an (n,m) row-stochastic matrix (state i's distribution over the m observable symbols), and initial is the length-n starting state distribution. See hmm_forward/hmm_viterbi's own doc comments for the two algorithms this feeds (.forward(obs) / .viterbi(obs), both callable as methods via the existing recv.f(args) sugar). Baum-Welch (parameter re-estimation from unlabeled observations) is NOT implemented — scoped out to keep forward/ Viterbi correct and hand-verified rather than rushing a third, harder-to-verify algorithm into the same pass.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.