hmm: Define a Hidden Markov Model

Description Usage Arguments Examples

Description

hmm() create a variable greta array following a Hidden Markov Model (HMM) 'distribution'. That is a probability distribution whose density is given by the probability of observing a sequence of observed states (the variable greta array), given a transition and an emission matrix as its parameters. This can be viewed as a compound distribution consisting of a categorical distribution over observed states, conditional on hidden states which (sequentially) follow another categorical distribution of states. The log density is calculated by analytically integrating out the hidden states using the forward algorithm.

This is a discrete, multivariate distribution, and is most likely to be used with distribution() to define a complete HMM, as in the example.

The transition and emission matrices should represent simplices; having rows summing to 1. These can be create with e.g. imultilogit().

Usage

1
hmm(initial, transition, emission, n_timesteps)

Arguments

initial

a length K row vector (ie. 1 x K matrix) of probabilities of initial hidden states

transition

a K x K matrix of transition probabilities between hidden states

emission

a K x N matrix of emission probabilities between hidden and observed states

n_timesteps

the number of timesteps (length of the observed state matrix) - must be a positive scalar integer

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
## Not run: 

# simulate data
n_hidden <- 2
n_observable <- 2
timesteps <- 20
initial <- random_simplex_matrix(1, n_hidden)
transition <- random_simplex_matrix(n_hidden,
                                    n_hidden)
emission <- random_simplex_matrix(n_hidden,
                                  n_observable)
hmm_data <- simulate_hmm(initial,
                         transition,
                         emission,
                         timesteps)
obs <- hmm_data$observed

# create simplex variables for the parameters
initial <- dirichlet(ones(1, n_hidden))
transition <- dirichlet(ones(n_hidden, n_hidden))
emission <- dirichlet(ones(n_hidden, n_observable))

# define the HMM over the observed states
distribution(obs) <- hmm(initial, transition, emission, timesteps)

# build the model
m <- model(transition, emission, initial)


## End(Not run)

greta-dev/greta.hmm documentation built on Oct. 5, 2019, 4:57 p.m.