|
1 | 1 | --- |
2 | 2 | title: "HMM Example" |
3 | 3 | author: "Ben Bales" |
4 | | -date: 9-26-2020 |
| 4 | +date: 10-2-2020 |
5 | 5 | output: |
6 | 6 | html_document: default |
7 | 7 | pdf_document: default |
@@ -45,13 +45,16 @@ There are three functions |
45 | 45 |
|
46 | 46 | - `hmm_marginal` - The likelihood of an HMM with the hidden discrete states |
47 | 47 | integrated out |
48 | | -- `hmm_latent_rng` - A function to generate draws of the hidden state from the posterior |
49 | | -- `hmm_hidden_state_prob` - A function to compute the distribution of hidden states |
50 | | -states at each measurement time |
| 48 | +- `hmm_latent_rng` - A function to generate draws of the hidden state that are |
| 49 | +implicitly integrated out of the model when using `hmm_marginal` (this is |
| 50 | +different than sampling more states with a posterior draw of a transition matrix |
| 51 | +and initial state) |
| 52 | +- `hmm_hidden_state_prob` - A function to compute the distributions of the |
| 53 | +integrated out hidden states |
51 | 54 |
|
52 | 55 | This guide will demonstrate how to simulate HMM realizations in R, fit the data |
53 | | -with `hmm_marginal`, produce estimates of the hidden states using |
54 | | -`hmm_hidden_state_prob`, and generate draws of the hidden state from the |
| 56 | +with `hmm_marginal`, produce estimates of the distributions of the hidden states |
| 57 | +using `hmm_hidden_state_prob`, and generate draws of the hidden state from the |
55 | 58 | posterior with `hmm_latent_rng`. |
56 | 59 |
|
57 | 60 | ### Generating HMM realizations |
@@ -221,7 +224,7 @@ fit$summary("t3") |
221 | 224 |
|
222 | 225 | ### State Probabilities |
223 | 226 |
|
224 | | -Even though the hidden states are not sampled directly, the distribution |
| 227 | +Even though the hidden states are integrated out, the distribution |
225 | 228 | of hidden states at each time point can be computed with the function |
226 | 229 | `hmm_hidden_state_prob`: |
227 | 230 |
|
@@ -258,14 +261,20 @@ hidden_probs_df %>% |
258 | 261 | xlab("Time (n)") |
259 | 262 | ``` |
260 | 263 |
|
261 | | -New simulations from the fitted HMM can be generated with `hmm_latent_rng`: |
| 264 | +If it is more convenient to work with draws of the hidden states at each time |
| 265 | +point (instead of the probabilities provided by `hmm_hidden_state_prob`), these |
| 266 | +can be generated with `hmm_latent_rng`: |
262 | 267 |
|
263 | 268 | ```{stan, output.var = "", eval = FALSE} |
264 | 269 | generated quantities { |
265 | 270 | int[N] y_sim = hmm_latent_rng(log_omega, Gamma, rho) |
266 | 271 | } |
267 | 272 | ``` |
268 | 273 |
|
| 274 | +Note, these are the probabilities and draws of the hidden state that was |
| 275 | +integrated out. This is different than sampling new HMM realizations using |
| 276 | +posterior draws of the initial condition and the transition matrix. |
| 277 | + |
269 | 278 | These can be plotted as well: |
270 | 279 |
|
271 | 280 | ```{r} |
|
0 commit comments