S1442 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 p0.
  • 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?

Name it

Markov chains

A chain with transition matrix Aij=P(zt+1=j|zt=i) forgets everything but its present state. Its stationary distribution π satisfies 𝝅TA=𝝅T; for two states with leaving probabilities a and b, π1=b/(a+b), and any start approaches it by the factor 1−a−b per step. Time spent in a state per visit is geometric with mean 1/(1−Aii).

Hidden Markov models

The state zt follows the chain, and each observation xt is drawn from an emission distribution p(xt|zt). The joint probability of a state path and the observations is

p(𝐱,𝐳)=πz1p(x1|z1)∏t=2NAzt−1ztp(xt|zt)

The forward algorithm

Summing over all KN paths is hopeless, but the sum factors. With αt(j)=p(x1,…,xt,zt=j):

α1(j)=πjp(x1|j),αt(j)=p(xt|j)∑iαt−1(i)Aij

Then p(𝐱)=∑jαN(j), and αt(j)/∑iαt(i) is the filtered probability of the current state. The cost is about NK2 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
  1. Hour 1. α1=(23×0.1,13×0.7)=(0.0667,0.2333) so P(faulty) = 0.778 already.
  2. Hour 2, healthy. α2(H)=(0.0667×0.9+0.2333×0.2)×0.1=0.0107
  3. Hour 2, faulty. α2(F)=(0.0667×0.1+0.2333×0.8)×0.7=0.1353
  4. Filter. The sequence has probability 0.0107 + 0.1353 = 0.146, so P(faulty now)=0.1353/0.146=0.927

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
  1. 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.
  2. Listing paths. For 20 hours there are 220=1,048,576 paths.
  3. Forward recursion. It takes about 20×22=80 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
  1. Count transitions. With the states known, maximum likelihood just counts: failure=9/900=0.01per hourrepair=30/100=0.3per hour
  2. Long-run faulty fraction. πF=0.010.01+0.3=3.2%
  3. Mean fault length. 1/0.3=3.3hours
  4. 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°.

Explain why

Why can the forward algorithm sum over every hidden path without listing them?

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.