The forward probabilityαi(t)=P(O1O2…Ot,Xt=si∣λ)
is the probability of the observations up to time tand being in state
i at time t.
⚠GotchaWhy not brute force
Enumerating all state sequences is exponential in the length of the
sequence. The forward algorithm uses dynamic programming - it solves time
t using the result already computed for t−1 - which makes it linear in
sequence length.
Every node holds the total probability of all paths that reach it, so each column is computed once and reused. That reuse is what turns an exponential enumeration into a linear scan.
The backward probabilityβi(t) is the probability of seeing the
observations from time t+1 to the end, given that we are in state i at
time t.
βT(i)=1for all iβt(i)=j∑aij⋅bj(Ot+1)⋅βt+1(j)⚠GotchaBeta fills right to left, and its emission index is t+1
The last column is all 1s, not all 0s - there are no remaining observations to
explain, and an empty product is 1. Note also that β uses
bj(Ot+1), the next observation, whereas α uses the current
one. Getting that index wrong is the usual source of a wrong beta table.
→StepsThe beta table, computed right to left
Column 3, initialisation.β3(S1)=1 and β3(S2)=1.
Column 2, with V2 as O3.β2(S1)=0.4×0.4+0.6×0.5=0.16+0.30=0.46.
β2(S2)=0.3×0.4+0.7×0.5=0.12+0.35=0.47.
Column 1, with V3 as O2.β1(S1)=0.4×0.5×0.46+0.6×0.2×0.47=0.092+0.0564=0.1484.
P(O∣λ) can be recovered from any column as the dot product of
that column's α and β values, and every column gives the same
answer. That is the standard way to check your arithmetic.
→StepsThree columns, one answer
Column 1:0.02×0.1484+0.24×0.1348=0.03532.
Column 2:0.04×0.46+0.036×0.47=0.03532.
Column 3:0.01072×1+0.0246×1=0.03532.
The occupation or gamma probability is the probability of being in
state i at time tgiven all the observations, past and future:
γt(i)=P(O∣λ)αt(i)⋅βt(i)→StepsThe gamma table, dividing through by 0.03532
γ1(S1)=0.02×0.1484/0.03532=0.08403 and γ1(S2)=0.24×0.1348/0.03532=0.91596.
γ2(S1)=0.04×0.46/0.03532=0.52095 and γ2(S2)=0.036×0.47/0.03532=0.47904.
γ3(S1)=0.01072×1/0.03532=0.30351 and γ3(S2)=0.0246×1/0.03532=0.69648.
Gamma
t=1
t=2
t=3
S1
0.08403
0.52095
0.30351
S2
0.91596
0.47904
0.69648
◎Exam cueEach gamma column sums to 1
It has to: at any time t the chain is in exactly one state, so the
probabilities over states must total 1. Picking the highest-gamma state at each
time is called posterior decoding, and it is a different answer from
Viterbi.
Forwardαₜ(j) = [ Σᵢ αₜ₋₁(i)·aᵢⱼ ] · bⱼ(Oₜ). Sums over all paths into the node, so each node holds a TOTAL probability. Answers "how likely is this observation sequence".
Viterbivₜ(j) = [ maxᵢ vₜ₋₁(i)·aᵢⱼ ] · bⱼ(Oₜ). Keeps only the most likely path into the node, and records a BACKPOINTER to the max-giving predecessor. Answers "which states produced it".
Viterbi also records the back-pointer - the predecessor that gave the max -
at every node, and backtracks from the best final node at the end to recover
the optimal state sequence.
→StepsViterbi on the canonical HMM
t = 1, V1.v1(S1)=0.2×0.1=0.02, v1(S2)=0.8×0.3=0.24. No backpointers yet.
t = 2 into S1, V3. Candidates 0.02×0.4=0.008 and 0.24×0.3=0.072. Max is 0.072 from S2, so v2(S1)=0.072×0.5=0.036.
t = 2 into S2. Candidates 0.02×0.6=0.012 and 0.24×0.7=0.168. Max is 0.168 from S2, so v2(S2)=0.168×0.2=0.0336.
t = 3 into S1, V2. Candidates 0.036×0.4=0.0144 and 0.0336×0.3=0.01008. Max is 0.0144 from S1, so v3(S1)=0.0144×0.4=0.00576.
t = 3 into S2. Candidates 0.036×0.6=0.0216 and 0.0336×0.7=0.02352. Max is 0.02352 from S2, so v3(S2)=0.02352×0.5=0.01176.
Terminate and backtrack. The larger final value is 0.01176 at S2; its backpointer is S2 at t=2, whose backpointer is S2 at t=1. The best sequence is S2 → S2 → S2.
Same trellis, one operator changed. Only the winning edge into each node survives, and following the bold edges back from the best final node recovers the optimal state sequence S2, S2, S2.⇄CompareLikelihood versus Viterbi, the dishonest-casino intuition
Likelihood (γ)At each time, sum over sequences to get the state distribution at time t. It fluctuates a lot from one step to the next - a narrow, local view.
ViterbiThe single most likely path through the whole trellis. Smoother, and it reflects the transition probabilities properly - a global view.
Baum-Welch learns the HMM parameters λ=(A,B,π) from
observations alone. It is EM applied to HMMs.
≡FactsThe chicken and egg, and the extra quantity it needs
If we knew the state-occupation probabilityγ, we could derive the emission and transition probabilities. If we knew those, we could derive γ. Neither is available first.
ξ (xi) is the probability of transitioning from state i to state j after time t, given all the observations. Like γ, it is computed from α and β.
→StepsThe EM loop for an HMM
E-step: with the current model, run forward-backward to compute γ and ξ - that is, establish p(γ,ξ∣x,a,b).
M-step: re-estimate the best transition a and emission b as point estimates that maximise the objective.
Repeat: fix one set of parameters to improve the other, until convergence.
Baum-Welch is EM wearing HMM notation. Forward-backward supplies the soft counts, and the soft counts supply the next model.≡FactsApplications of HMMs
Speech recognition.
DNA and base-pair analysis.
Part-of-speech tagging.
The dishonest casino - inferring which die, fair or biased, is being used from the observed rolls.