S14 · Lesson 42 · Hidden Markov models
Markov chains with their transition matrices, stationary distributions and dwell times, hidden states seen through noisy observations, the forward algorithm for filtering and likelihoods, Viterbi decoding of the best state path, and training by EM
A machine is either healthy or failing, and you can't see which; you only see whether its vibration alarm fires, and the alarm is unreliable. Its state persists from hour to hour, so a run of alarms means more than any single one. A hidden Markov model captures exactly this: a state that evolves by a Markov chain, and observations that depend only on the current state. Its algorithms, built on dynamic programming, track the hidden state, score whole sequences and find the most likely path. They were the backbone of speech recognition for decades and are the Viterbi decoder of every convolutional code in S8.
- A two-state Markov chain leaves state 1 with probability a per step and returns from state 2 with probability b. The probability of being in state 1 after n steps, starting from .
- It settles to the stationary value b/(a + b), approaching it geometrically by the factor 1 − a − b each step.
- When a + b > 1 the factor is negative and the approach oscillates; when a and b are small the chain is "sticky" and forgets its start slowly.
Predict
A machine stays healthy from one hour to the next with probability 0.9 and stays faulty with probability 0.8. In the long run, what fraction of hours is it healthy?
In balance, the flow out of healthy equals the flow back: , so = 2/3. Healthy spells last 1/0.1 = 10 hours on average and faulty ones 1/0.2 = 5, which is the same 2:1 ratio.
Name it
Markov chains
A chain with transition matrix forgets everything but its present state. Its stationary distribution π satisfies ; for two states with leaving probabilities a and b, , and any start approaches it by the factor per step. Time spent in a state per visit is geometric with mean .
Hidden Markov models
The state follows the chain, and each observation is drawn from an emission distribution . The joint probability of a state path and the observations is
The forward algorithm
Summing over all paths is hopeless, but the sum factors. With :
Then , and is the filtered probability of the current state. The cost is about operations.
Viterbi and training
Replacing the sum by a max (and remembering which predecessor won) finds the single most probable path: the Viterbi algorithm. Training by EM, the Baum–Welch algorithm, adds a backward pass to get each state's posterior at each time and then re-estimates A, π and the emissions from expected counts, exactly as lesson 36 did for mixtures.
Worked example
The machine's alarm fires with probability 0.1 when healthy and 0.7 when faulty. Starting from the stationary distribution, it fires in hours 1 and 2. Run the forward algorithm.
Show the solution
- Hour 1. so P(faulty) = 0.778 already.
- Hour 2, healthy.
- Hour 2, faulty.
- Filter. The sequence has probability 0.0107 + 0.1353 = 0.146, so
Answer the two alarms have probability 0.146, and the machine is faulty with probability 0.778 after hour 1 and 0.927 after hour 2.
Worked example
What's the most likely state path for those two alarms, and how much work does the forward algorithm save over listing paths for 20 hours?
Show the solution
- Score the paths. The four paths score HH 0.0060, HF 0.0047, FH 0.0047 and FF 0.1307, so Viterbi picks faulty–faulty.
- Listing paths. For 20 hours there are paths.
- Forward recursion. It takes about multiply-adds.
Answer the most likely path is faulty–faulty, and for 20 hours the forward algorithm's 80 multiply-adds replace a list of 1,048,576 paths.
Worked example
A labeled maintenance log shows a machine healthy for 900 hours, during which it failed 9 times, and faulty for 100 hours, during which it was repaired 30 times. Estimate the hourly transition probabilities, the long-run fraction of time it's faulty, and the mean length of a fault.
Show the solution
- Count transitions. With the states known, maximum likelihood just counts:
- Long-run faulty fraction.
- Mean fault length.
- Hidden states. When the states are hidden, Baum–Welch does the same counting with expected counts from the forward–backward algorithm, the EM of lesson 36 applied to a chain.
Answer failure 0.01 and repair 0.3 per hour; the machine is faulty 3.2% of the time, and a fault lasts 3.3 hours on average.
Check
A two-state chain leaves state 1 with probability 0.2 and leaves state 2 with probability 0.1. What's the stationary probability of state 1?
Check
A state persists from one step to the next with probability 0.9. How many steps does a visit last on average?
Check
The same kind of chain (leaving probabilities 0.1 and 0.2) starts in state 1 with probability 1. What's the probability of state 1 after 3 steps?
Check
A machine is healthy with prior probability 0.5. An alarm fires with probability 0.05 when healthy and 0.9 when faulty. It fires. What's the probability the machine is faulty?
Check
A chain starts healthy with probability 0.5 and stays healthy with probability 0.8. Alarms fire with probability 0.3 when healthy and 0.9 when faulty. What's the joint probability of "healthy, healthy" with alarms in both hours?
Check
Roughly how many multiply-adds does the forward algorithm need for a 4-state model and 10 observations?
Check
A two-state chain leaves state 1 with probability 0.1 and state 2 with probability 0.2 per step. It starts in state 1.
(a)
What is the probability it's in state 1 after 4 steps?
(b)
What is the time constant of its approach to equilibrium?
Check
A two-state chain leaves state 1 with probability a and state 2 with probability b, and starts in state 1.
Step 1
What is its stationary probability of state 1?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is the probability of state 1 after n steps?
Explain why
Why can the forward algorithm sum over every hidden path without listing them?
Because of the Markov property, the future depends on the past only through the current state. So all paths that end in the same state at time t can be merged: their total probability, α, is all that later steps need to know. At each step the algorithm updates K numbers, one per state, by summing the K ways to arrive there and multiplying by the new observation's probability. The exponential number of paths is never formed; the work grows linearly with the length of the sequence. Viterbi uses the same merging with a maximum in place of the sum.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.