HMM Procedure

Hidden Markov Model

This section is a brief review of the hidden Markov model. For more information, see Rabiner (1989), Krolzig (1997), Frühwirth-Schnatter (2006), and Cappé, Moulines, and Rydén (2010).

The hidden Markov model (HMM) is a bivariate discrete time process StartSet bold upper S Subscript t Baseline comma bold upper Y Subscript t Baseline EndSet, where StartSet bold upper S Subscript t Baseline EndSet is a (hidden) Markov chain; conditional on StartSet bold upper S Subscript t Baseline EndSet, the observable process StartSet bold upper Y Subscript t Baseline EndSet is a sequence of independent random variables such that the conditional distribution of bold upper Y Subscript t depends only on bold upper S Subscript t. The bold upper S Subscript t is often called the state. In general, the HMM can be expressed in three equations:

  1. Initialization equation: bold upper S 1 tilde h Subscript theta Baseline left-parenthesis dot right-parenthesis

  2. Transition equation: bold upper S Subscript t Baseline tilde f Subscript theta Baseline left-parenthesis dot vertical-bar bold upper S Subscript t minus 1 Baseline right-parenthesis

  3. Observation equation: bold upper Y Subscript t Baseline tilde g Subscript theta Baseline left-parenthesis dot vertical-bar bold upper S Subscript t Baseline right-parenthesis

Here the initial probability distribution function h Subscript theta Baseline left-parenthesis dot right-parenthesis is the probability distribution function for initial state bold upper S 1; the transition probability distribution function f Subscript theta Baseline left-parenthesis dot vertical-bar bold upper S Subscript t minus 1 Baseline right-parenthesis is the probability distribution function of current state bold upper S Subscript t conditional on the past state bold upper S Subscript t minus 1; the observation probability distribution function g Subscript theta Baseline left-parenthesis dot vertical-bar bold upper S Subscript t Baseline right-parenthesis is the probability distribution function of current observation bold upper Y Subscript t conditional on the current state bold upper S Subscript t; and all probability distribution functions might depend on the parameter theta.

There is no restriction on the dimensionality of bold upper S Subscript t and bold upper Y Subscript t; that is, they might be scalars, vectors, or matrices.

If the transition probability distribution function f Subscript theta Baseline left-parenthesis dot vertical-bar dot right-parenthesis is time-independent (that is, the Markov chain is time-homogeneous), then the HMM is called the (time-)homogeneous HMM; otherwise, the HMM is called the (time-)inhomogeneous HMM.

The state bold upper S Subscript t could take discrete or continuous values or even a mix of discrete and continuous values.[7] In the simplest and most popular case, bold upper S Subscript t is a scalar and takes finite discrete values; that is, bold upper S Subscript t Baseline element-of StartSet 1 comma ellipsis comma upper K EndSet. In this case, the HMM could be called the finite-state-space HMM.

For the finite-state-space HMM, the initial state distribution function h Subscript theta Baseline left-parenthesis dot right-parenthesis can be expressed as a upper K times 1 vector pi, which is called the initial state probability vector (ISPV); that is,

StartLayout 1st Row  pi equals StartSet pi Subscript i Baseline equals p left-parenthesis bold upper S 1 equals i right-parenthesis comma i equals 1 comma ellipsis comma upper K EndSet EndLayout

For the finite-state-space and homogeneous HMM, the transition probability distribution function f Subscript theta Baseline left-parenthesis dot vertical-bar dot right-parenthesis can be expressed as a upper K times upper K matrix bold upper A, which is called the transition probability matrix (TPM), where an element a Subscript i j of bold upper A denotes the probability transitioning from past state i to current state j; that is,

StartLayout 1st Row  a Subscript i j Baseline equals p left-parenthesis bold upper S Subscript t Baseline equals j vertical-bar bold upper S Subscript t minus 1 Baseline equals i right-parenthesis EndLayout

There are six common problems to solve for an HMM:

  • Filtering problem: What is p left-parenthesis bold upper S Subscript t Baseline vertical-bar bold upper Y 1 comma ellipsis comma bold upper Y Subscript t Baseline right-parenthesis, the probability distribution of state bold upper S Subscript t given observations bold upper Y 1 comma ellipsis comma bold upper Y Subscript t Baseline?

  • Smoothing problem: What is p left-parenthesis bold upper S Subscript t Baseline vertical-bar bold upper Y 1 comma ellipsis comma bold upper Y Subscript upper T Baseline right-parenthesis, the probability distribution of state bold upper S Subscript t given observations bold upper Y 1 comma ellipsis comma bold upper Y Subscript upper T Baseline, where T is the sample size?

  • Forecasting problem: What is p left-parenthesis bold upper S Subscript t plus h Baseline vertical-bar bold upper Y 1 comma ellipsis comma bold upper Y Subscript t Baseline right-parenthesis comma h greater-than 0, the probability distribution of state bold upper S Subscript t plus h given observations bold upper Y 1 comma ellipsis comma bold upper Y Subscript t Baseline?

  • Evaluating problem: What is p left-parenthesis bold upper Y 1 comma ellipsis comma bold upper Y Subscript t Baseline right-parenthesis, the probability (or the likelihood) of observations bold upper Y 1 comma ellipsis comma bold upper Y Subscript t Baseline?

  • Decoding problem: What is arg max Underscript bold upper S 1 comma ellipsis comma bold upper S Subscript upper T Baseline Endscripts p left-parenthesis bold upper S 1 comma ellipsis comma bold upper S Subscript upper T Baseline comma bold upper Y 1 comma ellipsis comma bold upper Y Subscript upper T Baseline right-parenthesis, the most likely sequence of hidden states bold upper S 1 comma ellipsis comma bold upper S Subscript upper T Baseline given observations bold upper Y 1 comma ellipsis comma bold upper Y Subscript upper T Baseline, where T is the sample size?

  • Learning problem: How do you estimate the unknown parameter theta given observations bold upper Y 1 comma ellipsis comma bold upper Y Subscript upper T Baseline, where T is the sample size?

In most cases, the learning problem is the most difficult one. You might apply the maximum likelihood (ML) method or the maximum a posteriori (MAP) method to get the point estimate of parameter theta. For more information, see the section Parameter Estimation Methods.

Several algorithms are helpful in solving the problems for the HMMs. Three of them are the forward algorithm, the backward algorithm, and the Viterbi algorithm. They might be generalized to support many types of HMMs. The following sections focus on applying them to the finite-state-space and homogeneous HMM.

Forward Algorithm

The forward algorithm recursively calculates the joint probability distribution of a state at time t, bold upper S Subscript t, and all observations up to time t, bold upper Y 1 comma ellipsis comma bold upper Y Subscript t Baseline. First, define alpha Subscript t Baseline left-parenthesis i right-parenthesis comma i equals 1 comma ellipsis comma upper K comma as the joint probability of left-parenthesis bold upper S Subscript t Baseline equals i right-parenthesis and bold upper Y 1 comma ellipsis comma bold upper Y Subscript t Baseline:

StartLayout 1st Row  alpha Subscript t Baseline left-parenthesis i right-parenthesis identical-to p left-parenthesis bold upper S Subscript t Baseline equals i comma bold upper Y 1 comma ellipsis comma bold upper Y Subscript t Baseline right-parenthesis EndLayout

Then, alpha Subscript t Baseline left-parenthesis i right-parenthesis can be recursively calculated by the following steps:

  1. Calculate alpha 1 left-parenthesis i right-parenthesis comma i equals 1 comma ellipsis comma upper K comma by

    StartLayout 1st Row  alpha 1 left-parenthesis i right-parenthesis equals pi Subscript i Baseline g Subscript theta Baseline left-parenthesis bold upper Y 1 vertical-bar bold upper S 1 equals i right-parenthesis EndLayout
  2. Calculate alpha Subscript t Baseline left-parenthesis i right-parenthesis comma i equals 1 comma ellipsis comma upper K comma t equals 2 comma ellipsis comma upper T comma by

    StartLayout 1st Row  alpha Subscript t Baseline left-parenthesis i right-parenthesis equals left-parenthesis sigma-summation Underscript j equals 1 Overscript upper K Endscripts alpha Subscript t minus 1 Baseline left-parenthesis j right-parenthesis a Subscript j i Baseline right-parenthesis g Subscript theta Baseline left-parenthesis bold upper Y Subscript t Baseline vertical-bar bold upper S Subscript t Baseline equals i right-parenthesis EndLayout

You can solve the evaluating problem by

StartLayout 1st Row  upper L Subscript t Baseline identical-to p left-parenthesis bold upper Y 1 comma ellipsis comma bold upper Y Subscript t Baseline right-parenthesis equals sigma-summation Underscript j equals 1 Overscript upper K Endscripts alpha Subscript t Baseline left-parenthesis j right-parenthesis EndLayout

Because the likelihood of all observations, upper L Subscript upper T, can be calculated, you can apply the maximum likelihood method to estimate the unknown parameter theta; if the prior distribution of the parameter theta is given, you can also apply the MAP method. That is, you might use the forward algorithm in the learning problem.

You can solve the filtering problem by

StartLayout 1st Row  p Subscript t Superscript left-parenthesis f right-parenthesis Baseline left-parenthesis i right-parenthesis identical-to p left-parenthesis bold upper S Subscript t Baseline equals i vertical-bar bold upper Y 1 comma ellipsis comma bold upper Y Subscript t Baseline right-parenthesis equals alpha Subscript t Baseline left-parenthesis i right-parenthesis slash upper L Subscript t EndLayout

When the filtering problem is solved, you can solve the forecast problem, because the h-step-ahead prediction of the state probability can be calculated by

StartLayout 1st Row  p Subscript t plus h vertical-bar t Superscript left-parenthesis p right-parenthesis Baseline equals left-parenthesis bold upper A prime right-parenthesis Superscript h Baseline p Subscript t Superscript left-parenthesis f right-parenthesis EndLayout

where p Subscript t Superscript left-parenthesis f right-parenthesis is the upper K times 1 vector with the ith element as p Subscript t Superscript left-parenthesis f right-parenthesis Baseline left-parenthesis i right-parenthesis, and p Subscript t plus h vertical-bar t Superscript left-parenthesis p right-parenthesis is the upper K times 1 vector with the ith element as p Subscript t plus h vertical-bar t Superscript left-parenthesis p right-parenthesis Baseline left-parenthesis i right-parenthesis:

StartLayout 1st Row  p Subscript t plus h vertical-bar t Superscript left-parenthesis p right-parenthesis Baseline left-parenthesis i right-parenthesis identical-to p left-parenthesis bold upper S Subscript t plus h Baseline equals i vertical-bar bold upper Y 1 comma ellipsis comma bold upper Y Subscript t Baseline right-parenthesis EndLayout

The distribution of bold upper Y Subscript t plus h conditional on observations bold upper Y 1 comma ellipsis comma bold upper Y Subscript t Baseline is the following mixture distribution:

StartLayout 1st Row  bold upper Y Subscript t plus h Baseline vertical-bar bold upper Y 1 comma ellipsis comma bold upper Y Subscript t Baseline tilde sigma-summation Underscript i equals 1 Overscript upper K Endscripts p Subscript t plus h vertical-bar t Superscript left-parenthesis p right-parenthesis Baseline left-parenthesis i right-parenthesis g Subscript theta Baseline left-parenthesis dot vertical-bar bold upper S Subscript t plus h Baseline equals i right-parenthesis EndLayout

Backward Algorithm

To solve the smoothing problem, in addition to the forward algorithm, you also need the backward algorithm. Define beta Subscript t Baseline left-parenthesis i right-parenthesis as the probability of observing the future observations bold upper Y Subscript t plus 1 Baseline comma ellipsis comma bold upper Y Subscript upper T Baseline conditional on the current state bold upper S Subscript t Baseline equals i:

StartLayout 1st Row  beta Subscript t Baseline left-parenthesis i right-parenthesis identical-to p left-parenthesis bold upper Y Subscript t plus 1 Baseline comma ellipsis comma bold upper Y Subscript upper T Baseline vertical-bar bold upper S Subscript t Baseline equals i right-parenthesis EndLayout

The backward algorithm recursively calculates beta Subscript t Baseline left-parenthesis i right-parenthesis as follows:

  1. Set beta Subscript upper T Baseline left-parenthesis i right-parenthesis equals 1 comma i equals 1 comma ellipsis comma upper K.

  2. Calculate beta Subscript t Baseline left-parenthesis i right-parenthesis comma i equals 1 comma ellipsis comma upper K, t equals upper T minus 1 comma ellipsis comma 1, by

    StartLayout 1st Row  beta Subscript t Baseline left-parenthesis i right-parenthesis equals sigma-summation Underscript j equals 1 Overscript upper K Endscripts a Subscript i j Baseline g Subscript theta Baseline left-parenthesis bold upper Y Subscript t plus 1 Baseline vertical-bar bold upper S Subscript t plus 1 Baseline equals j right-parenthesis beta Subscript t plus 1 Baseline left-parenthesis j right-parenthesis EndLayout

When you calculate both alpha Subscript t Baseline left-parenthesis i right-parenthesis and beta Subscript t Baseline left-parenthesis i right-parenthesis comma i equals 1 comma ellipsis comma upper K comma t equals 1 comma ellipsis comma upper T, by using the forward algorithm and backward algorithm, the smoothing problem is solved, because you can calculate the probability of state bold upper S Subscript t taking value i given all observations bold upper Y 1 comma ellipsis comma bold upper Y Subscript upper T Baseline as follows:

StartLayout 1st Row  gamma Subscript t Baseline left-parenthesis i right-parenthesis identical-to p left-parenthesis bold upper S Subscript t Baseline equals i vertical-bar bold upper Y 1 comma ellipsis comma bold upper Y Subscript upper T Baseline right-parenthesis equals StartFraction alpha Subscript t Baseline left-parenthesis i right-parenthesis beta Subscript t Baseline left-parenthesis i right-parenthesis Over sigma-summation Underscript j equals 1 Overscript upper K Endscripts alpha Subscript t Baseline left-parenthesis j right-parenthesis beta Subscript t Baseline left-parenthesis j right-parenthesis EndFraction EndLayout

Viterbi Algorithm

You use the Viterbi algorithm to solve the decoding problem. First, define the highest probability of a single state path ending in state bold upper S Subscript t Baseline equals i and observations bold upper Y 1 comma ellipsis comma bold upper Y Subscript t Baseline as follows:

StartLayout 1st Row  upper V Subscript t Baseline left-parenthesis i right-parenthesis equals max Underscript bold upper S 1 comma ellipsis comma bold upper S Subscript t minus 1 Baseline Endscripts p left-parenthesis bold upper S 1 comma ellipsis comma bold upper S Subscript t minus 1 Baseline comma bold upper S Subscript t Baseline equals i comma bold upper Y 1 comma ellipsis comma bold upper Y Subscript t Baseline right-parenthesis EndLayout

To keep track of the best path, for t equals 2 comma ellipsis comma upper T, define

StartLayout 1st Row  v Subscript t Baseline left-parenthesis i right-parenthesis equals arg max Underscript j Endscripts upper V Subscript t minus 1 Baseline left-parenthesis j right-parenthesis a Subscript j i EndLayout

Then, upper V Subscript t Baseline left-parenthesis i right-parenthesis and v Subscript t Baseline left-parenthesis i right-parenthesis can be recursively calculated as follows:

  1. Calculate upper V 1 left-parenthesis i right-parenthesis comma i equals 1 comma ellipsis comma upper K comma by

    StartLayout 1st Row  upper V 1 left-parenthesis i right-parenthesis equals pi Subscript i Baseline g Subscript theta Baseline left-parenthesis bold upper Y 1 vertical-bar bold upper S 1 equals i right-parenthesis EndLayout
  2. Calculate upper V Subscript t Baseline left-parenthesis i right-parenthesis and v Subscript t Baseline left-parenthesis i right-parenthesis comma i equals 1 comma ellipsis comma upper K comma t equals 2 comma ellipsis comma upper T comma by

    StartLayout 1st Row 1st Column upper V Subscript t Baseline left-parenthesis i right-parenthesis 2nd Column equals 3rd Column left-parenthesis max Underscript j Endscripts upper V Subscript t minus 1 Baseline left-parenthesis j right-parenthesis a Subscript j i Baseline right-parenthesis g Subscript theta Baseline left-parenthesis bold upper Y Subscript t Baseline vertical-bar bold upper S Subscript t Baseline equals i right-parenthesis 2nd Row 1st Column v Subscript t Baseline left-parenthesis i right-parenthesis 2nd Column equals 3rd Column arg max Underscript j Endscripts upper V Subscript t minus 1 Baseline left-parenthesis j right-parenthesis a Subscript j i EndLayout

Then, the probability of the best path is

StartLayout 1st Row  upper V Superscript asterisk Baseline equals max Underscript i Endscripts upper V Subscript upper T Baseline left-parenthesis i right-parenthesis EndLayout

Define v Subscript upper T Superscript asterisk Baseline equals arg max Underscript i Endscripts upper V Subscript upper T Baseline left-parenthesis i right-parenthesis, and the best state path can be backtracked by

StartLayout 1st Row  v Subscript t Superscript asterisk Baseline equals v Subscript t plus 1 Baseline left-parenthesis v Subscript t plus 1 Superscript asterisk Baseline right-parenthesis comma t equals upper T minus 1 comma ellipsis comma 1 EndLayout

That is, StartSet v Subscript t Superscript asterisk Baseline EndSet Subscript t equals 1 comma ellipsis comma upper T is the best state path. The decoding problem is solved.



[7] In some literature, the HMM is referred to only as the discrete-state-space HMM (that is, the state can take only discrete values). The name “state space model” is used for the continuous-state-space HMM (that is, the state can take only continuous values).

Last updated: July 09, 2026