AI Glossary / Mathematics and optimization / Advanced
Forward algorithm
Definition
The forward algorithm is a dynamic programming method for computing the likelihood of an observation sequence under a hidden Markov model. It sums the contributions of all possible hidden-state paths efficiently, rather than enumerating each path separately.
What it means
Observed events can be compatible with many unseen explanations. A machine's sensor readings, for example, may be generated by different sequences of operating conditions. The forward algorithm combines those possibilities according to the model's initial-state, transition and observation probabilities.
Its answer concerns the observations under the specified model. It does not identify a single best hidden-state sequence, prove which physical events occurred or automatically estimate the model's parameters. Those are different tasks, even when they use related calculations.
How it works
For each state j and time t, store alpha_t(j): the joint probability of the observations through time t and of being in state j then. Initialize with alpha_1(j) = pi_j b_j(o_1), multiplying the initial-state probability by the first observation probability.
For each later observation, calculate alpha_t(j) = b_j(o_t) sum_i alpha_(t−1)(i) a_ij. The sum collects paths reaching j from every predecessor. Finally, sum the last values over states to obtain the sequence likelihood for a fixed-length model without additional termination probabilities.
For N states and T observations, dense transitions give O(TN squared) work. Keeping just the preceding and current vectors requires O(N) working storage. Practical implementations use scaling or log-space arithmetic to prevent numerical underflow on long sequences.
Three practical examples
1. Equipment monitoring: calculate how well a specified normal-operation model explains a sequence of discrete sensor signals. A low likelihood may motivate investigation, but interpreting it requires an appropriate comparison and attention to sequence length.
2. Speech modeling: an HMM can assign an acoustic-observation sequence a likelihood while accounting for alternative hidden-state paths. Continuous observations use emission densities rather than probabilities of exact measurement values.
3. Model comparison: score the same observation sequence under two candidate HMMs. Likelihood contributes evidence for comparison, while complexity, prior assumptions and evaluation design still matter.
Think of it this way
Imagine water moving through a network of channels. Several routes can arrive at the same junction. Instead of labeling every droplet by its complete route, record the combined flow at each junction and pass that amount onward using the channel proportions. The algorithm similarly combines probability contributions that share the same current state.
Worked example
Consider an original two-state machine model with states Normal and Strained. Initial probabilities are 0.6 and 0.4. From Normal, transition probabilities to Normal and Strained are 0.8 and 0.2; from Strained they are 0.3 and 0.7.
The observations are Quiet or Alert. Normal emits Alert with probability 0.1 and Quiet with 0.9. Strained emits Alert with 0.8 and Quiet with 0.2. All probabilities are stipulated for teaching, not measured from a real machine.
For the sequence Alert, Quiet, Alert, the first forward values are Normal: 0.6 × 0.1 = 0.06; Strained: 0.4 × 0.8 = 0.32.
After Quiet, the Normal value is (0.06 × 0.8 + 0.32 × 0.3) × 0.9 = 0.1296. The Strained value is (0.06 × 0.2 + 0.32 × 0.7) × 0.2 = 0.0472.
After the final Alert, Normal becomes (0.1296 × 0.8 + 0.0472 × 0.3) × 0.1 = 0.011784. Strained becomes (0.1296 × 0.2 + 0.0472 × 0.7) × 0.8 = 0.047168.
Adding them gives sequence probability 0.058952. Independently enumerating all eight hidden-state paths gives the same total. The largest individual contribution, from Strained throughout, is 0.025088: selecting only that path would omit substantial probability mass.
The final Strained value is not itself a conditional state probability. Dividing by the sequence likelihood gives 0.047168 / 0.058952, approximately 0.8001. This is the probability of the final state being Strained given all three observations and the stipulated model.
Illustrative case study
In a fictional manufacturing pilot, a team uses an HMM to score sensor sequences. An early implementation keeps only the strongest predecessor at each step, producing scores lower than the intended total sequence likelihood.
A small exhaustive example exposes the problem: maximization selects a best path, whereas the forward calculation sums alternative paths. After correcting the recurrence, the team checks several short sequences against enumeration and introduces numerically stable calculations for longer records.
The pilot then evaluates whether the scores distinguish relevant operating situations. Matching the mathematical recurrence is necessary, but it does not establish that the chosen states and emissions adequately describe the equipment.
Common misconceptions and mistakes
Forward values are joint probabilities, not normalized state probabilities. Their sum is generally not one.
Replacing the sum over predecessors with a maximum changes the calculation toward Viterbi decoding. The best path and the combined likelihood answer different questions.
The algorithm does not learn transition probabilities merely by processing a sequence. Parameter estimation requires a separate procedure, such as an appropriate forward-backward training method.
Very small likelihoods are normal for long discrete sequences. They should not automatically be labeled anomalies, and floating-point underflow must not be confused with a mathematically impossible sequence.
Business use
The method helps evaluate sequential models without discarding uncertainty over their hidden explanations. Teams using such models should specify what their scores represent, how sequence lengths are handled and how thresholds or comparisons are validated.
A useful acceptance test is agreement with exhaustive enumeration on tiny examples. Operational assessment then needs representative data, clear decision criteria and checks on whether the model's assumptions remain suitable.
People also ask
Does forward return the most likely path?
No; use a decoding method such as Viterbi for that task.
Can it support filtering?
Yes. Normalizing the current forward vector gives the state distribution conditional on observations so far, when the likelihood is positive.
Does it use future observations to revise earlier states?
The basic forward pass does not. Smoothing combines additional backward information.
Check your understanding
What is the worked sequence's likelihood?
Answer: 0.058952.
Why is 0.047168 not an 80 percent probability by itself?
Answer: It is a joint probability; normalization by 0.058952 produces the conditional probability.
What prevents exponential enumeration?
Answer: Combining all paths with the same current state into one stored value.
Related terms
- Hidden Markov model
- Dynamic programming
- Viterbi algorithm
- Forward-backward algorithm
- Filtering
- Smoothing
- log-sum-exp
Learn next
Reproduce the eight-path sum, then normalize each forward vector. Compare the resulting filtered state probabilities with a Viterbi path and with smoothed probabilities that also use later observations.
More in Mathematics and optimization
- Absolute error loss - Absolute error loss measures a numerical prediction's error as the absolute difference between its predicted and reference values: L(y,y-hat)=|y-y-hat|.
- Active-set method - An active-set method is an optimization approach that works with a changing subset of constraints believed to be tight at the solution.
- Adjoint state method - The adjoint state method computes how a chosen objective changes when the parameters of a mathematical model change.
- Σ-algebra
- Algebraic connectivity - Algebraic connectivity is the second-smallest eigenvalue of a graph's Laplacian matrix.
- Algorithm IMED - Algorithm IMED, short for Indexed Minimum Empirical Divergence, is a method for choosing actions in a stochastic multi-armed bandit problem.
Sources
Daniel Jurafsky and James H. Martin, Speech and Language Processing, Appendix A, Section A.3, likelihood computation and the forward recurrence: https://web.stanford.edu/~jurafsky/slp3/A.pdf
The two-state machine calculations and manufacturing case are original illustrations.
Previous: Forney factor graph Next: Forward-backward algorithm