Viterbi trellis
Decode the most likely hidden state sequence and inspect each probability column.
About this tool
Infer a most likely hidden state sequence from observed symbols under a discrete first-order HMM. The umbrella example uses Rain/Sun states and U (umbrella) or N (no umbrella); these are educational assumptions, not measured weather data. The dice example uses Fair/Loaded states and observations 1–6; loaded dice give six probability 0.5. The impossible example starts in A, cannot switch, and A emits only x. Observing y is impossible.
Observations are 1–32 complete case-sensitive tokens, separated by whitespace or commas; no token is shortened. Model settings contains editable JSON with states, obsLabels, start, trans and emit. The array order defines indices. start[i] is the initial state probability; trans[i][j] is the probability of moving from state i to j; emit[i][k] is the probability of symbol k in state i. Each distribution sums to 1 within 1e-9, with values in [0,1]. Use 1–4 distinct states and 1–8 distinct symbols, IDs of 1–12 ASCII letters, digits, underscores or hyphens. Model JSON is limited to 6,000 characters; observations to 512.
Calculate returns the result immediately; Step computes one full time column. Optional animation uses 0–2000 ms, initially 350. Pause/Resume retains the calculation, and Calculate finishes a paused run. Reset or edited input discards results. Model, observations, preset and delay are saved locally, including invalid drafts; results and animation are recomputed. Drafts exceeding the storage limit keep the previous saved copy and show a warning.
Viterbi maximizes P(states, observations), not total observation probability or posterior confidence. Initial log score is ln(start[i]) + ln(emit[i][o]). Later columns use max over predecessor states of previous score + ln(transition) + ln(emission); backpointers recover the path. Zero probability stays −∞. Natural logarithms prevent product underflow. Recurrence O(T·N²), working storage O(T·N); the column display additionally reconstructs prefix paths and creates snapshots. Exact ties use the first predecessor and final state in model order; multiple optimal paths may exist. A prefix winner can change as later observations arrive.
Rows are states, columns are times; labels below show observation and time index. Blue marks the current best prefix, then the final path. Log scores above nodes and in the optional table describe the best joint prefix ending in each state. Impossible transitions are omitted, future columns remain uncomputed. Long trellises scroll locally. Values shown to three decimals; decoding uses full precision. Bounded educational decoder, not model training, sequence marginalization, a speech recognizer or a real weather predictor.