Hidden Markov Models
An elementary first-order Hidden Markov model is a probabilistic model over observations, , and hidden states, , which can be fully defined by the conditional distributions and . Here we make the dependency on additional model parameters, , explicit. When is continuous, the user can explicitly encode these distributions in Stan and use Markov chain Monte Carlo to integrate out.
When each state takes a value over a discrete and finite set, say , we can take advantage of the dependency structure to marginalize and compute . We start by defining the conditional observational distribution, stored in a matrix with Next, we introduce the transition matrix, , with 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 -vector , with
The Stan functions that support this type of model are special in that the user does not explicitly pass and as arguments. Instead, the user passes , , and , which in turn depend on and .
Stan functions
real
hmm_marginal
(matrix log_omega, matrix Gamma, vector rho)
Returns the log probability density of , with 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 density of each output,
Gamma
: , the transition matrix,
rho
: , the initial state probability.
array[] int
hmm_latent_rng
(matrix log_omega, matrix Gamma, vector rho)
Returns a length array of integers over , sampled from the joint posterior distribution of the hidden states, . 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 matrix. The column is a simplex of probabilities for the variable. Moreover, let be the output. Then . This function may only be used in transformed data and generated quantities.
Available since 2.24
Back to top