🎲 HMM Trellis (2D)
Canvas view of states & observations
Setup
Transition A (rows = from)
Emission B (rows = state)
Controls
Stats
Viterbi log-prob
β€”
Correct states
β€”
Sequence prob
β€”
Status
Ready
Info & Theory

A hidden Markov model (HMM) hops between unseen hidden states (Sunny / Rainy / Foggy) while emitting visible observations (Walk / Shop / Clean). You only see the observations; the states are hidden.

Two matrices

  • Transition A: A[i][j] β€” probability of moving from state i to state j.
  • Emission B: B[i][k] β€” probability that state i emits observation k.

Viterbi decoding

Fills a trellis with Ξ΄_t(j) = max_i Ξ΄_{tβˆ’1}(i)Β·A[i][j]Β·B[j][o_t], stores back-pointers, then traces back from the best final state β€” this is the highlighted path.

Forward algorithm

Sums over all paths to give per-step state probabilities P(state | observations) and total sequence likelihood P(O), using log-arithmetic to avoid underflow.

About Hidden Markov Model β€” Canvas Trellis (2D)

This 2D companion drives the exact same hidden Markov model math as the 3D version β€” sampling from editable transition and emission matrices, Viterbi decoding, and the forward algorithm β€” but draws the trellis on a flat Canvas2D grid instead of a WebGL point cloud, so every edge, node and back-pointer reads as plain 2D geometry.

A Hidden Markov Model (HMM) describes a system where an underlying process transitions between hidden states according to Markov dynamics (each state depends only on the previous one), and each state emits observable outputs with known probabilities. The "hidden" aspect is that we can only see the emissions, not the states themselves β€” the goal is to infer the hidden sequence from observations.

Three canonical problems are solved with HMMs: evaluation (the probability of an observation sequence, via the Forward algorithm), decoding (the most likely hidden state sequence, via Viterbi), and learning (estimating the model parameters, via Baum–Welch). HMMs underlie speech recognition, gene structure prediction, part-of-speech tagging and financial regime detection.

Frequently Asked Questions

How is this different from the 3D version?

Same math β€” sampling, Viterbi decoding and the forward algorithm run identically. Only the rendering differs: this page draws the trellis with plain Canvas2D calls instead of a WebGL point cloud, which keeps the geometry legible at a glance.

What is the Viterbi algorithm?

The Viterbi algorithm efficiently finds the most probable sequence of hidden states given an observation sequence using dynamic programming. It runs in O(TΒ·KΒ²) time, where T is the sequence length and K is the number of states.

What do the node sizes on the trellis mean?

After decoding, each node's radius scales with its forward-algorithm probability β€” the chance of being in that state at that time step given all observations so far. Larger nodes are more likely states.

Why do the rings (true states) sometimes disagree with the Viterbi path?

The Viterbi path is the model's best guess from the observations alone. The rings mark the actual hidden states used to generate the sequence. They diverge when the emission probabilities are ambiguous β€” the same observation can plausibly come from more than one state.