Skip to content

Commit 50a1431

Browse files
committed
Switched hidden for latent and other edits
1 parent 742aa09 commit 50a1431

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

knitr/hmm-example/hmm-example.Rmd

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ of the system.
2727

2828
Any realization of an HMM is a sequence of $N$ integers in the range $[1, K]$,
2929
however, because of the structure of the HMM, it is not necessary to sample
30-
the latent states to do inference on the transition probabilities, the
30+
the hidden states to do inference on the transition probabilities, the
3131
parameters of the measurement model, or the estimates of the initial state.
3232
Estimates of the distribution of states at each measurement time can be
3333
computed separately.
@@ -38,15 +38,16 @@ section of the Function Reference Guide.
3838

3939
There are three functions
4040

41-
- `hmm_marginal` - The likelihood of an HMM with the latent discrete states
41+
- `hmm_marginal` - The likelihood of an HMM with the hidden discrete states
4242
integrated out
43-
- `hmm_latent_rng` - A function to generate random realizations from the posterior of an HMM
44-
- `hmm_hidden_state_prob` - A function to compute the distribution of latent
45-
states at each measurement.
43+
- `hmm_latent_rng` - A function to generate draws of the hidden state from the posterior
44+
- `hmm_hidden_state_prob` - A function to compute the distribution of hidden states
45+
states at each measurement time
4646

4747
This guide will demonstrate how to simulate HMM realizations in R, fit the data
48-
with `hmm_marginal` and produce estimates of the latent states using
49-
`hmm_hidden_state_prob`.
48+
with `hmm_marginal`, produce estimates of the hidden states using
49+
`hmm_hidden_state_prob`, and generate draws of the hidden state from the
50+
posterior with `hmm_latent_rng`.
5051

5152
### Generating HMM realizations
5253

@@ -82,7 +83,8 @@ The trajectory can easily be visualized:
8283
qplot(1:N, states)
8384
```
8485

85-
An HMM is useful when the latent state is only measured indirectly.
86+
An HMM is useful when the hidden state is not measure directly (if the
87+
state was measured directly, it wouldn't be hidden).
8688

8789
In this example the observations are assumed to be
8890
normally distributed with a state specific mean and some measurement error.
@@ -143,7 +145,7 @@ directly. In this case the transition matrix needs constructed from `t1`,
143145

144146
The measurement model, in contrast, is not passed directly to the HMM function.
145147

146-
Instead, a $KxN$ matrix `log_omega` of log likelihoods is passed in. The
148+
Instead, a $K \times N$ matrix `log_omega` of log likelihoods is passed in. The
147149
$(k, n)$ entry of this matrix is the log likelihood of the $nth$ measurement
148150
given the system at time $n$ is actually in state $k$. For the generative
149151
model above, these are log normals evaluated at the three different means.
@@ -189,7 +191,7 @@ The estimated group means match the known ones:
189191
```{r}
190192
fit$summary("mu")
191193
```
192-
The estimated of the initial condition is not much more informative than
194+
The estimated initial conditions are not much more informative than
193195
the prior, but it is there:
194196
```{r}
195197
fit$summary("rho")
@@ -212,33 +214,33 @@ fit$summary("t3")
212214

213215
### State Probabilities
214216

215-
Even though the latent states are not sampled directly, the distribution
216-
of latent states at each time point can be computed with the function
217+
Even though the hidden states are not sampled directly, the distribution
218+
of hidden states at each time point can be computed with the function
217219
`hmm_hidden_state_prob`:
218220

219221
```{stan, output.var = "", eval = FALSE}
220222
generated quantities {
221-
matrix[3, N] latent_probs = hmm_hidden_state_prob(log_omega, Gamma, rho);
223+
matrix[3, N] hidden_probs = hmm_hidden_state_prob(log_omega, Gamma, rho);
222224
}
223225
```
224226

225227
These can be plotted:
226228

227229
```{r}
228-
latent_probs_df = fit$draws() %>%
230+
hidden_probs_df = fit$draws() %>%
229231
as_draws_df %>%
230-
select(starts_with("latent_probs")) %>%
232+
select(starts_with("hidden_probs")) %>%
231233
pivot_longer(everything(),
232234
names_to = c("state", "n"),
233235
names_transform = list(k = as.integer, n = as.integer),
234-
names_pattern = "latent_probs\\[([0-9]*),([0-9]*)\\]",
235-
values_to = "latent_probs")
236+
names_pattern = "hidden_probs\\[([0-9]*),([0-9]*)\\]",
237+
values_to = "hidden_probs")
236238
237-
latent_probs_df %>%
239+
hidden_probs_df %>%
238240
group_by(state, n) %>%
239-
summarize(qh = quantile(latent_probs, 0.8),
240-
m = median(latent_probs),
241-
ql = quantile(latent_probs, 0.2)) %>%
241+
summarize(qh = quantile(hidden_probs, 0.8),
242+
m = median(hidden_probs),
243+
ql = quantile(hidden_probs, 0.2)) %>%
242244
ungroup() %>%
243245
ggplot() +
244246
geom_errorbar(aes(n, ymin = ql, ymax = qh, width = 0.0), alpha = 0.5) +

knitr/hmm-example/hmm-example.stan

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ model {
5050
}
5151

5252
generated quantities {
53-
matrix[3, N] latent_probs = hmm_hidden_state_prob(log_omega, Gamma, rho);
53+
matrix[3, N] hidden_probs = hmm_hidden_state_prob(log_omega, Gamma, rho);
5454
int y_sim[N] = hmm_latent_rng(log_omega, Gamma, rho);
5555
}

0 commit comments

Comments
 (0)