Hidden Markov Models

An elementary first-order Hidden Markov model is a probabilistic model over N observations, yn, and N hidden states, xn, which can be fully defined by the conditional distributions p(ynxn,ϕ) and p(xnxn1,ϕ). Here we make the dependency on additional model parameters, ϕ, explicit. When x is continuous, the user can explicitly encode these distributions in Stan and use Markov chain Monte Carlo to integrate x out.

When each state x takes a value over a discrete and finite set, say {1,2,...,K}, we can take advantage of the dependency structure to marginalize x and compute p(yϕ). We start by defining the conditional observational distribution, stored in a K×N matrix ω with ωkn=p(ynxn=k,ϕ). Next, we introduce the K×K transition matrix, Γ, with Γij=p(xn=jxn1=i,ϕ). Each row defines a probability distribution and must therefore be a simplex (i.e. its components must add to 1). Currently, Stan only supports stationary transitions where a single transition matrix is used for all transitions. Finally we define the initial state K-vector ρ, with ρk=p(x0=kϕ).

The Stan functions that support this type of model are special in that the user does not explicitly pass y and ϕ as arguments. Instead, the user passes logω, Γ, and ρ, which in turn depend on y and ϕ.

Stan functions

real hmm_marginal(matrix log_omega, matrix Gamma, vector rho)
Returns the log probability density of y, with xn integrated out at each iteration.

Available since 2.24

The arguments represent (1) the log density of each output, (2) the transition matrix, and (3) the initial state vector.

  • log_omega: logωkn=logp(ynxn=k,ϕ), log density of each output,

  • Gamma: Γij=p(xn=j|xn1=i,ϕ), the transition matrix,

  • rho: ρk=p(x0=kϕ), the initial state probability.

array[] int hmm_latent_rng(matrix log_omega, matrix Gamma, vector rho)
Returns a length N array of integers over {1,...,K}, sampled from the joint posterior distribution of the hidden states, p(xϕ,y). May be only used in transformed data and generated quantities.

Available since 2.24

matrix hmm_hidden_state_prob(matrix log_omega, matrix Gamma, vector rho)
Returns the matrix of marginal posterior probabilities of each hidden state value. This will be a K×N matrix. The nth column is a simplex of probabilities for the nth variable. Moreover, let A be the output. Then Aij=p(xj=iϕ,y). This function may only be used in transformed data and generated quantities.

Available since 2.24
Back to top