68  Hidden Markov Models

Every clustering technique in the previous topic treated observations as independent of each other: farm 7’s cluster assignment had nothing to do with farm 6’s. Hidden Markov Models (Lawrence R. Rabiner, 1989) handle a different situation, one where order matters: a sequence of observations collected over time from the same subject, where the thing generating those observations is itself changing state as time passes, and that underlying state is never observed directly.

A field’s disease condition is a natural example. Nobody directly observes “this field entered a stressed state in week 4.” What gets observed instead is a proxy, a satellite NDVI reading, a visible symptom score, and the true underlying condition, the hidden state, has to be inferred from that sequence of indirect, imperfect observations. That is exactly the problem a Hidden Markov Model is built to solve.

68.1 The Structure of a Hidden Markov Model

An HMM has three pieces, all of which have to be specified (or estimated) before the model can be used:

  • Hidden states: the unobserved condition of the system at each time step, such as “Healthy” or “Stressed.” The system is assumed to move between these states following a Markov property: the probability of the next state depends only on the current state, not on the full history of states before it.
  • The transition matrix: the probability of moving from each hidden state to each other hidden state (including staying put) at the next time step.
  • The emission matrix: the probability of observing each possible visible signal, given the current hidden state. A “Stressed” field is more likely to show a low NDVI reading than a “Healthy” one, but not certain to, which is exactly why the state has to be inferred rather than read off directly.

Given these three pieces and an observed sequence, two questions come up repeatedly in practice. First, how likely is this particular sequence of observations under the model at all, useful for flagging an unusual sequence. Second, and usually more useful, what is the most likely sequence of hidden states that produced these observations. The second question is answered by the Viterbi algorithm.

68.2 Worked Example

One field, tracked weekly over a ten-week stretch of the growing season. The hidden state each week is the field’s true condition, Healthy or Stressed, never observed directly. What is observed is a weekly NDVI reading, bucketed into Low, Medium, or High. The model’s known parameters, estimated from historical data on similar fields, are given below.

Initial state distribution (\(\pi\)): Healthy 0.70, Stressed 0.30 (most fields start the season healthy).

Transition matrix (\(A\)):

From \ To Healthy Stressed
Healthy 0.85 0.15
Stressed 0.25 0.75

Emission matrix (\(B\)):

State Low NDVI Medium NDVI High NDVI
Healthy 0.10 0.30 0.60
Stressed 0.60 0.30 0.10

Observed sequence, weeks 1 through 10: High, High, Medium, Low, Low, Low, Medium, High, High, High.

68.3 Hidden Markov Models in R

Dedicated HMM packages such as HMM or depmixS4 are not part of R’s base or recommended set. The Viterbi decoding algorithm and the forward algorithm are both short enough to implement directly from their own definitions using nothing beyond base R matrices and loops.

68.4 Reading the Result

The decoded state sequence should track the observed NDVI pattern closely but not identically: the model is weighing both the emission probabilities (how likely each NDVI reading is under each state) and the transition probabilities (how likely the field is to have actually switched states between two consecutive weeks), so an isolated one-week dip that would be cheap to explain as noise under a state that persisted may get smoothed over rather than triggering an immediate state switch. That smoothing is the entire value the Markov structure adds over simply thresholding each week’s NDVI reading in isolation: it uses the whole sequence, not just the current reading, to decide what state a field was most likely in during any given week. The probability of the sequence itself is a much smaller number than it might look, an inescapable consequence of multiplying ten separate probabilities together, and it is far more useful for comparing two candidate sequences or two candidate models against each other than for interpreting on its own.


Summary

Concept Description
Foundations
Hidden Markov Model Infers an unobserved underlying state from a sequence of indirect, imperfect observations over time
Hidden States and the Markov Property The next hidden state depends only on the current state, not the full history of states before it
Transition Matrix Gives the probability of moving from each hidden state to each other state at the next time step
Emission Matrix Gives the probability of each possible observed signal, given the current hidden state
Worked Example
Worked Example: Ten Weeks of NDVI Readings A field's true weekly condition, Healthy or Stressed, inferred from ten weeks of bucketed NDVI readings
The Viterbi Algorithm Decodes the single most likely hidden state sequence given an observed sequence and the model parameters
The Forward Algorithm Computes the total probability of an observed sequence under the model, useful for comparing sequences or models
Why the Full Sequence Matters Weighing the whole sequence, not just one reading at a time, smooths over noise a single-week threshold would miss