Hidden Markov Models

Many movement models proposed in recent years belong to the class of hidden Markov models (HMMs). An HMM is a time series model that comprises two components, an observable series and an underlying, non-observable state sequence (just as we saw with the mixture model). The observed data are taken to be conditionally independent given the states and are generated by so-called state-dependent distributions.

The state sequence is modeled as a Markov process usually assumed to be of first order, which means that the probability of state occurrences at time \(t+1\) depends only on which state the chain is in at time \(t\). That is, for an HMM with \(K\) states there is a \(K\) by \(K\) transition probability matrix \(\Gamma\) with \(\gamma_{i,j} = p(z_t=j | z_t=i)\). For the matrix to properly define a probability distribution, each row must be a simplex (i.e. its components must add up to \(1\)).

Using our latent indicator variable approach for the case of two states, this can be written as:

\[ \begin{equation} z_t \sim \begin{cases} \text{Bern}(p_1) &\mbox{if } z_{t-1} = 1 \\ \text{Bern}(p_2) &\mbox{if } z_{t-1} = 0 \end{cases}\; . \end{equation} \]

A consequence of this formulation is that the amount of time \(D_n\) spent in a given state \(n\) (before switching to an other state) is a random variable that follows a geometric distribution with parameter \(1 − \gamma_{n,n}\).

We also have to define the initial state distribution \(\boldsymbol\delta_n = p(z_1 = n)\).

Let’s simulate a trajectory

Let’s look at the time series of states. We can see that there is some “structure” and that switches between states are not too frequent.

Now we fit an HMM (see hmm.stan). For more on HMMs in Stan see Leos-Barajas and Michelot (2018).

As usual, we need to check that the chains have converged (Rhat \(< 1.1\)), and that we have a decent sample size of the posteriors (n_eff). Then we can look at the posteriors for the parameters governing steps and turns

## Inference for Stan model: hmm.
## 3 chains, each with iter=1000; warmup=500; thin=1; 
## post-warmup draws per chain=500, total post-warmup draws=1500.
## 
##          mean se_mean   sd  2.5%   25%  50%  75%  98% n_eff Rhat
## mu[1]    0.41    0.02 0.61 -0.80  0.03 0.39 0.76 1.79  1062    1
## mu[2]    0.01    0.00 0.04 -0.07 -0.01 0.02 0.04 0.10  1724    1
## rho[1]   0.11    0.00 0.04  0.03  0.08 0.11 0.14 0.19   897    1
## rho[2]   0.76    0.00 0.03  0.70  0.75 0.77 0.79 0.82  1407    1
## shape[1] 0.95    0.00 0.05  0.86  0.92 0.95 0.99 1.06   852    1
## shape[2] 1.89    0.01 0.18  1.56  1.77 1.89 2.01 2.26  1183    1
## scale[1] 0.87    0.00 0.07  0.73  0.82 0.86 0.91 1.01  1214    1
## scale[2] 5.76    0.01 0.37  5.05  5.52 5.76 6.01 6.47  1360    1
## g[1,1]   0.93    0.00 0.02  0.89  0.92 0.93 0.95 0.96  1865    1
## g[1,2]   0.07    0.00 0.02  0.04  0.05 0.07 0.08 0.11  1865    1
## g[2,1]   0.14    0.00 0.04  0.07  0.11 0.14 0.17 0.22  1542    1
## g[2,2]   0.86    0.00 0.04  0.78  0.83 0.86 0.89 0.93  1542    1
## 
## Samples were drawn using NUTS(diag_e) at Thu Oct  8 08:10:03 2020.
## For each parameter, n_eff is a crude measure of effective sample size,
## and Rhat is the potential scale reduction factor on split chains (at 
## convergence, Rhat=1).

Let’s plot the posteriors

We can compare posteriors to true parameter values

The classified trajectory

Compare true states to estimated ones

Diagnostics

Let’s start by looking at the density of step and turn distributions. As we know the true parameters for these, we can add them to the plots.

Now a posterior predictive check on the autocorrelation in step lengths


HMM with covariates

plot time series for states

Fit the model

Before anything, we check that the chains have converged (r-hat < 1.1) and look at the posteriors for the steps and turns

The classsified trajectory

Compare true states to estimated ones


Exercise

  1. Fit a 3-state HMM to the simulated 2-state CRW.
  2. Plot the distributions fitted to the different states
  3. Do a posterior predictive check on the step length autocorrelation

References

Leos-Barajas, V., and T. Michelot. 2018. An introduction to animal movement modeling with hidden markov models using stan for bayesian inference.