Statistics & probability

Hidden Markov model forward backward and Viterbi calculator

A hidden Markov model separates observed symbols from the underlying states that produce them.

Your numbers

Row stochastic matrix, up to six states.
Same state rows; columns are observed symbols.
1–200 observations.

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

A hidden Markov model separates observed symbols from the underlying states that produce them. Given transition probabilities, emission probabilities and an initial state distribution, this calculator computes the observation log likelihood, smoothed state probabilities and the most probable complete state path. Parameters are supplied, not learned.

THE FORMULAalpha_t(j)∝b_j(o_t)Σ_i alpha_(t−1)(i)a_ij; gamma_t∝alpha_t beta_t.

A worked example

For initial probabilities (0.6,0.4), transition rows (0.7,0.3),(0.4,0.6), emissions (0.9,0.1),(0.2,0.8) and observations 0,1, the sequence probability is 0.209. The best hidden path is 0,1 with joint probability 0.1296; its probability differs from the total over all paths.

Good to know

Discrete symbols and known normalized probabilities, up to six states and 200 observations. Scaled forward/backward arithmetic and log Viterbi arithmetic are used. Zero-probability sequences are rejected. No training, continuous emissions or claim that Viterbi equals every marginal maximum.

The observation sequence is different from the state path

A hidden state selects an emission distribution, while the emitted symbol is what the input sequence records. State indices and symbol indices need not have the same count or meaning. Transition rows describe moves between states; emission rows describe symbols conditional on a state, and both families must be normalized explicitly.

The initial vector applies before the first observed symbol. That symbol’s emission probability is included immediately, rather than first making a transition to a second hidden state. This timing convention matters for short sequences and is fixed by the default two-observation example and exhaustive hidden-path comparisons.

The best hidden path is only one contribution to the observation likelihoodS0S1S0S1Observed: 0, 1P(sequence)=0.209Best path P=0.1296
For initial probabilities (0.6,0.4), transition rows (0.7,0.3),(0.4,0.6), emissions (0.9,0.1),(0.2,0.8) and observations 0,1, the sequence probability is 0.209. The best hidden path is 0,1 with joint probability 0.1296; its probability differs from the total over all paths.

The Markov chain calculator from a transition matrix covers the nearby task. Observed symbols are emissions from hidden states; current chain tool has no observation likelihood or state decoding.

Forward and backward scaling preserve likelihood

A long sequence can have an extremely small total probability even when each step is ordinary. Forward messages are normalized at each time and their log scale factors are added to obtain the sequence log likelihood. This avoids turning a valid small probability into an unexplained numerical zero through repeated multiplication.

Backward messages use the corresponding forward scale factors so their product with the forward messages yields normalized smoothed state probabilities. Smoothing uses the entire sequence, including later observations. It differs from filtering, which conditions only on observations available up to the current time and can produce different intermediate state beliefs.

Joint and marginal decisions answer different questions

Viterbi maximizes one joint path probability, while the smoothed table maximizes nothing independently: it reports each time’s marginal probabilities. Taking the largest marginal state at every time can produce a sequence different from Viterbi. Neither choice is the same as summing all valid hidden paths to obtain the observation likelihood.

The model parameters are supplied and remain fixed. This calculator does not estimate a transition table from observations or perform Baum–Welch training. Small-case validation enumerates every hidden path to verify both the likelihood sum and the maximum path, while zero-probability observation sequences receive an explicit error rather than a fabricated state history.

Separate sequence probability from state decoding

The forward algorithm sums over every possible hidden-state path to obtain the probability of the observed symbols. Viterbi instead maximizes the joint probability of one complete hidden path and the observations. Forward–backward combines prefix and suffix information to obtain the posterior probability of each state at each time, conditioned on the whole observation sequence.

These are three distinct questions. Taking the most probable posterior state at each time need not produce the Viterbi path, because independently chosen marginal modes can have poor transition compatibility. The displayed path is a sequence-level maximum, whereas each posterior row describes uncertainty at one time. Neither output trains transition or emission probabilities.

Enumerate the default two-step example

For observations 0,1, the four hidden paths have joint probabilities: (0,0)=0.6×0.9×0.7×0.1=0.0378; (0,1)=0.6×0.9×0.3×0.8=0.1296; (1,0)=0.4×0.2×0.4×0.1=0.0032; and (1,1)=0.4×0.2×0.6×0.8=0.0384. Their sum is 0.209, the observation probability.

The Viterbi path is (0,1), since 0.1296 is largest. Its posterior probability is 0.1296/0.209≈0.6201, not 0.1296. For the first time point, state-zero posterior probability is (0.0378+0.1296)/0.209≈0.80096. This explicit enumeration explains why the likelihood, path joint probability and marginal posterior entries have different meanings.

Enter transition and emission rows consistently

Transition row i contains probabilities for the next state given current state i. Emission row i contains probabilities of the observed symbol given hidden state i. The initial vector describes the hidden state before the first emission in this convention. Each row and the initial vector must be nonnegative and sum to one; symbols index emission columns starting at zero.

A structural zero forbids an event. If every hidden path assigns zero probability to an entered sequence, the calculator rejects it rather than inventing a posterior distribution. Small positive probabilities are not interchangeable with zero: changing a zero to a positive value changes which paths the model allows. Supply probabilities based on the intended model, not just values adjusted to make a particular sequence pass.

Use scaling without losing the probability interpretation

Products of many small probabilities can underflow ordinary floating-point arithmetic. Forward–backward rescales its intermediate messages, keeping track of normalization factors, while Viterbi works with log probabilities. The reported log likelihood remains meaningful even when the corresponding unlogged probability is extremely small.

A low sequence probability alone is not a model-failure test: longer sequences naturally have smaller probabilities. Compare models on the same sequence and account for how their parameters were obtained. Missing symbols, continuous observations, parameter estimation and confidence in the fitted parameters need additional procedures. This calculator assumes a known discrete HMM and answers its probability and decoding questions only.

A few questions, answered

Is Viterbi the sequence of marginal maxima?

Not necessarily. Viterbi maximizes one joint path; smoothed marginals answer per-time questions.

Are model parameters trained?

No. Transition, emission and initial probabilities are supplied and held fixed.

Why does the Viterbi joint probability differ from its posterior probability?

The joint value includes the observation probability. Divide the winning path’s joint probability by the total observation probability to obtain its posterior probability.

Can I use the most likely state in each posterior row as the Viterbi path?

Not in general. Marginal state modes and the maximum-probability complete path optimize different quantities.

Find your calculator

Start typing to explore the library.