Backward algorithm 2026-10-20
The backward algorithm computes suffix likelihoods in a Hidden Markov model. With , its recurrence is . Together with the forward algorithm, it gives posterior state probabilities proportional to . The terminal condition must include any explicit End-state transition.
Forward algorithm 2026-10-20
The forward algorithm computes sequence likelihoods by summing over all hidden paths through a Hidden Markov model. For emitting states, obeys . It reuses partial sums rather than enumerating exponentially many state sequences. Silent states require additional transitions without consuming an observation. Scaling or log-sum-exp prevents floating-point underflow.
The sequence has five characters, while the four match states account for four compulsory emissions. Consequently exactly one insertion state emission is needed. An insertion at would have to emit T, which has probability zero there. The only nonzero hidden path is therefore
Multiply its transition probabilities and emission probabilities, including the probability of exiting immediately and bypassing :
Normally the forward algorithm sums over all compatible paths. Here all other paths have probability zero, so this single path gives the full sequence probability.
Use a uniform independent DNA background, so each nucleotide has probability . Conditional on length five, its sequence probability is . The requested score, often called log-odds in sequence analysis, is a log-likelihood ratio against this background:
Using natural logarithms instead gives . Identically distributed independent positions do not, by themselves, require equal base frequencies. If the intended background instead has specified frequencies , the general answer is
The numerical value above uses the conventional equal-frequency random DNA model.
Taking a logarithm only after multiplying probabilities does not prevent floating-point underflow: the product may already have rounded to zero, and its logarithm is then . Accumulate logarithms before forming tiny products; where alternative paths must be added, use log-sum-exp in the forward algorithm or rescale its intermediate probabilities.
Let be the hidden state. With the usual convention and , the Markov property and the conditional independence of emissions factor the joint probability as
The requested state-path probability after observing the complete data is the corresponding conditional probability:
Here the forward algorithm and backward algorithm give the same normalizer at any interior time,
There is no extra factor : it is already included in . If an unconditioned joint probability with the observations is intended, it is the numerator alone. Summing the boxed expression over all gives one.