-
Notifications
You must be signed in to change notification settings - Fork 11
/
speed.Rmd
534 lines (412 loc) · 17.6 KB
/
speed.Rmd
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# Fasten convergence {#speed}
## Under the hood
Our `nimble` workflow so far
```{r}
knitr::include_graphics("images/nimble_workflow_sofar.png")
```
But `nimble` gives full access to the MCMC engine
```{r}
knitr::include_graphics("images/nimble_workflow.png")
```
Steps to use NIMBLE at full capacity
1. Build the model. It is an R object.
2. Build the MCMC.
3. Compile the model and MCMC.
4. Run the MCMC.
5. Extract the samples.
Know what? `nimbleMCMC` does all of this at once.
Back to CJS models with Dipper data.
Define model
```{r echo = FALSE}
dipper <- read_csv(here::here("dat", "dipper.csv"))
y <- dipper %>%
select(year_1981:year_1987) %>%
as.matrix()
first <- apply(y, 1, function(x) min(which(x !=0)))
my.constants <- list(N = nrow(y), T = ncol(y), first = first)
my.data <- list(y = y + 1)
zinits <- y + 1 # non-detection -> alive
zinits[zinits == 2] <- 1 # dead -> alive
initial.values <- function() list(phi = runif(1,0,1),
p = runif(1,0,1),
z = zinits)
parameters.to.save <- c("phi", "p")
n.iter <- 2500
n.burnin <- 1000
n.chains <- 2
```
```{r, eval = TRUE, echo = TRUE}
hmm.phip <- nimbleCode({
delta[1] <- 1 # Pr(alive t = 1) = 1
delta[2] <- 0 # Pr(dead t = 1) = 0
phi ~ dunif(0, 1) # prior survival
gamma[1,1] <- phi # Pr(alive t -> alive t+1)
gamma[1,2] <- 1 - phi # Pr(alive t -> dead t+1)
gamma[2,1] <- 0 # Pr(dead t -> alive t+1)
gamma[2,2] <- 1 # Pr(dead t -> dead t+1)
p ~ dunif(0, 1) # prior detection
omega[1,1] <- 1 - p # Pr(alive t -> non-detected t)
omega[1,2] <- p # Pr(alive t -> detected t)
omega[2,1] <- 1 # Pr(dead t -> non-detected t)
omega[2,2] <- 0 # Pr(dead t -> detected t)
# likelihood
for (i in 1:N){
z[i,first[i]] ~ dcat(delta[1:2])
for (j in (first[i]+1):T){
z[i,j] ~ dcat(gamma[z[i,j-1], 1:2])
y[i,j] ~ dcat(omega[z[i,j], 1:2])
}
}
})
```
Run and summarise
```{r, eval = TRUE, echo = TRUE, cache = TRUE, message = TRUE}
mcmc.phip <- nimbleMCMC(code = hmm.phip,
constants = my.constants,
data = my.data,
inits = initial.values,
monitors = parameters.to.save,
niter = n.iter,
nburnin = n.burnin,
nchains = n.chains)
```
```{r, eval = TRUE, echo=TRUE, message = TRUE}
MCMCsummary(object = mcmc.phip, round = 2)
```
Detailed Nimble workflow
1. Build the model (R object)
```{r, eval = TRUE, echo = TRUE, message = TRUE}
hmm.phip <- nimbleModel(code = hmm.phip,
constants = my.constants,
data = my.data,
inits = initial.values())
```
2. Build the MCMC
```{r, eval = TRUE, echo = TRUE, message = TRUE}
phip.mcmc.configuration <- configureMCMC(hmm.phip)
phip.mcmc <- buildMCMC(phip.mcmc.configuration)
```
3. Compile the model and MCMC
```{r, eval = TRUE, echo = TRUE, message = TRUE}
phip.model <- compileNimble(hmm.phip)
c.phip.mcmc <- compileNimble(phip.mcmc, project = phip.model)
```
4. Run the MCMC
```{r, eval = TRUE, echo = TRUE, message = TRUE}
samples <- runMCMC(c.phip.mcmc, niter = 1000)
# Alternative:
# c.phip.mcmc$run(1000)
# samples <- as.matrix(c.phip.mcmc$mvSamples)
```
5. Look at results
```{r, eval = TRUE, echo = TRUE, message = TRUE}
summary(samples[,"phi"])
summary(samples[,"p"])
```
## Why is it useful?
Use and debug model in `R`
Makes your life easier when it comes to debugging
Inspect variables
```{r}
hmm.phip$gamma
```
Calculate likelihood
```{r}
hmm.phip$calculate()
```
Example of debugging a model in `R`
Pretend an impossible state was given in inits, making a dead bird alive again.
```{r echo=FALSE}
# Pretend that inits for the 6th bird are invalid, going from dead to alive.
saved_z_6 <- phip.model$z[6,]
phip.model$z[6,] <- c(1, 1, 2, 1, 2, 2, 2)
```
```{r, eval = TRUE, echo = TRUE, message = TRUE}
phip.model$calculate("z") # We can see there is a problem in z (states).
c(phip.model$calculate("z[5,]"), # Bird 5 is valid.
phip.model$calculate("z[6,]")) # Bird 6 isn't.
phip.model$z[6,] # We have found the problem
```
```{r echo=FALSE}
# Restore to valid valies
phip.model$z[6,] <- saved_z_6
```
## Change/modify/write samplers
Slice samplers instead of Metropolis-Hastings.
Samplers on a log scale, especially for a variance, standard deviation, or precision parameter.
Blocking correlated parameters.
To know all samplers available in Nimble, type in `help(samplers)`.
Source code for samplers and distributions is **in R** and can be copied and modified.
Use [`compareMCMCs` package](https://github.com/nimble-dev/compareMCMCs) to compare options (including Stan and Jags!).
Consider a model with wing length and individual random effect on survival.
```{r echo = FALSE}
dipper <- read_csv(here::here("dat", "dipper.csv"))
y <- dipper %>%
select(year_1981:year_1987) %>%
as.matrix()
first <- apply(y, 1, function(x) min(which(x !=0)))
wing.length.st <- as.vector(scale(dipper$wing_length))
my.constants <- list(N = nrow(y),
T = ncol(y),
first = first,
winglength = wing.length.st)
zinits <- y + 1 # non-detection -> alive
zinits[zinits == 2] <- 1 # dead -> alive
initial.values <- function() list(beta = rnorm(2,0,1.5),
sdeps = runif(1,0,3),
p = runif(1,0,1),
z = zinits)
parameters.to.save <- c("beta", "sdeps", "p")
n.iter <- 10000
n.burnin <- 2500
n.chains <- 2
```
```{r}
hmm.phiwlrep <- nimbleCode({
p ~ dunif(0, 1) # prior detection
omega[1,1] <- 1 - p # Pr(alive t -> non-detected t)
omega[1,2] <- p # Pr(alive t -> detected t)
omega[2,1] <- 1 # Pr(dead t -> non-detected t)
omega[2,2] <- 0 # Pr(dead t -> detected t)
for (i in 1:N){
logit(phi[i]) <- beta[1] + beta[2] * winglength[i] + eps[i] #<<
eps[i] ~ dnorm(mean = 0, sd = sdeps) #<<
gamma[1,1,i] <- phi[i] # Pr(alive t -> alive t+1)
gamma[1,2,i] <- 1 - phi[i] # Pr(alive t -> dead t+1)
gamma[2,1,i] <- 0 # Pr(dead t -> alive t+1)
gamma[2,2,i] <- 1 # Pr(dead t -> dead t+1)
}
beta[1] ~ dnorm(mean = 0, sd = 1.5)
beta[2] ~ dnorm(mean = 0, sd = 1.5)
sdeps ~ dunif(0, 10)
delta[1] <- 1 # Pr(alive t = 1) = 1
delta[2] <- 0 # Pr(dead t = 1) = 0
# likelihood
for (i in 1:N){
z[i,first[i]] ~ dcat(delta[1:2])
for (j in (first[i]+1):T){
z[i,j] ~ dcat(gamma[z[i,j-1], 1:2, i]) #<<
y[i,j] ~ dcat(omega[z[i,j], 1:2])
}
}
})
```
```{r echo = FALSE, eval = FALSE}
# mcmc.phiwlrep <- nimbleMCMC(code = hmm.phiwlrep,
# constants = my.constants,
# data = my.data,
# inits = initial.values,
# monitors = parameters.to.save,
# niter = n.iter,
# nburnin = n.burnin,
# nchains = n.chains)
hmm.phiwlrep.m <- nimbleModel(code = hmm.phiwlrep,
constants = my.constants,
data = my.data,
inits = initial.values())
Rmcmc <- buildMCMC(hmm.phiwlrep.m)
Cmodel <- compileNimble(hmm.phiwlrep.m)
Cmcmc <- compileNimble(Rmcmc, project=hmm.phiwlrep.m)
times.phiwlrep <- system.time( mcmc.phiwlrep <- runMCMC(Cmcmc, niter = 10000, nchains = 2) )
# Alternative:
# Cmcmc$run(10000)
# samples1 <- as.matrix(Cmcmc$mvSamples)
# Cmcmc$run(10000)
# samples2 <- as.matrix(Cmcmc$mvSamples)
save(times.phiwlrep, mcmc.phiwlrep, file = here::here("dat", "wl.RData"))
```
Trace plot for standard deviation of the random effect (default sampler)
```{r echo = FALSE, message=FALSE, warning=FALSE}
load(here::here("dat", "wl.RData"))
```
```{r, echo = FALSE}
MCMCtrace(mcmc.phiwlrep, params = "sdeps", pdf = FALSE)
```
Change samplers
Good sampling strategies depend on the model and data. What are the samplers used by default?
```{r echo = FALSE}
hmm.phiwlrep.m <- nimbleModel(code = hmm.phiwlrep,
constants = my.constants,
data = my.data,
inits = initial.values())
```
```{r}
mcmcConf <- configureMCMC(hmm.phiwlrep.m)
```
Remove default sampler, and use slice sampler
```{r}
mcmcConf$removeSamplers('sdeps')
mcmcConf$addSampler(target = 'sdeps',
type = "slice") #<<
mcmcConf
```
```{r, echo = FALSE}
Rmcmc <- buildMCMC(mcmcConf)
Cmodel <- compileNimble(hmm.phiwlrep.m)
Cmcmc <- compileNimble(Rmcmc, project=hmm.phiwlrep.m)
times.phiwlrep.with.slice <- system.time( mcmc.phiwlrep.with.slice <- runMCMC(Cmcmc, niter = 10000, nchains = 2))
samples1 <- mcmc.phiwlrep.with.slice[[1]]
samples2 <- mcmc.phiwlrep.with.slice[[2]]
iUse <- 2501:10000
ESS.with.slice <- coda::effectiveSize(list(coda::as.mcmc(samples1[iUse, 'sdeps']),
coda::as.mcmc(samples2[iUse, 'sdeps'])))
# Cmcmc$run(10000)
# samples1 <- as.matrix(Cmcmc$mvSamples)
# Cmcmc$run(10000)
# samples2 <- as.matrix(Cmcmc$mvSamples)
df <- data.frame(iter = c(2501:10000, 2501:10000),
samples = c(samples1[2501:10000,"sdeps"], samples2[2501:10000,"sdeps"]),
chain = c(rep("chain 1", length(samples1[2501:10000,"sdeps"])),
rep("chain 2", length(samples2[2501:10000,"sdeps"]))))
df.beta <- data.frame(iter = c(2501:10000, 2501:10000),
beta1 = c(samples1[2501:10000,"beta[1]"], samples2[2501:10000,"beta[1]"]),
beta2 = c(samples1[2501:10000,"beta[2]"], samples2[2501:10000,"beta[2]"]),
chain = c(rep("chain 1", length(samples1[2501:10000,"sdeps"])),
rep("chain 2", length(samples2[2501:10000,"sdeps"]))))
```
Trace plot for standard deviation of the random effect (slice sampler)
```{r, echo = FALSE}
MCMCtrace(mcmc.phiwlrep.with.slice, params = "sdeps", pdf = FALSE)
```
```{r, echo = FALSE}
# df %>%
# ggplot() +
# aes(x = iter, y = samples, group = chain, color = chain) +
# geom_line() +
# labs(x = "iterations", y = "random effect standard deviation", color = "")
```
## Which is better? MCMC efficiency
+ MCMC efficiency depends on both mixing and computation time.
+ MCMC efficiency = Effective Sample Size (ESS) / computation time.
+ MCMC efficiency is the number of effectively independent posterior samples generated per second.
+ ESS is different for each parameter. (Computation time is the same for each parameter.)
+ ESS can be estimated from packages `coda` or `mcmcse`. These give statistical estimates, so different runs will give different estimates.
```{r echo = FALSE}
iUse <- 2501:10000
time_frac <- 0.75 # Include 75% of the time to count sampling of recorded sampled, which is not the only valid option.
ESS <- coda::effectiveSize(list(coda::as.mcmc(mcmc.phiwlrep[[1]][iUse, 'sdeps']),
coda::as.mcmc(mcmc.phiwlrep[[2]][iUse, 'sdeps'])))
t1 <- (time_frac * times.phiwlrep[3])
efficiency <- ESS / t1
t.with.slice <- (time_frac * times.phiwlrep.with.slice[3])
efficiency.with.slice <- ESS.with.slice / t.with.slice
rd2 <- function(x) round(x, digits = 2)
```
+ Efficiency with default sampler = `r rd2(ESS)` / `r rd2(t1)` = `r rd2(efficiency)`.
+ Efficiency with slice sampler = `r rd2(ESS.with.slice)` / `r rd2(t.with.slice)` = `r rd2(efficiency.with.slice)`.
Block sampling
+ High correlation in (regression) parameters may make independent samplers inefficient.
```{r, echo = FALSE}
df.beta %>%
ggplot() +
aes(x = beta1, y = beta2, group = chain, color = chain) +
geom_point(alpha = .05) +
labs(x = "beta1", y = "beta2", color = "")
```
Block sampling (propose candidate values from multivariate distribution) might help.
Remove and replace independent RW samples by block sampling. Then proceed as usual.
```{r message = FALSE, warning=FALSE}
mcmcConf$removeSamplers(c('beta[1]','beta[2]'))
mcmcConf$addSampler(target = c('beta[1]','beta[2]'),
type = "RW_block") #<<
```
```{r message = FALSE, warning=FALSE}
mcmcConf
```
## Strategies for improving MCMC
+ Choose better initial values.
+ Customize sampler choice (more in [Chapter 7 of the User's manual](https://r-nimble.org/html_manual/cha-mcmc.html)).
+ Reparameterize, e.g. standardize covariates, deal with parameter redundancy.
+ Rewrite the model.
+ Vectorize to improve computational efficiency (not covered).
+ Avoid long chains of deterministic dependencies.
+ Marginalize to remove parameters
+ Use new functions and new distributions written as nimbleFunctions.
+ Write new samplers that take advantage of particular model structures (not covered).
+ Using multiple cores with parallelization: see how-to at <https://r-nimble.org/nimbleExamples/parallelizing_NIMBLE.html>
## Marginalization
User-defined distributions is another neat feature of Nimble.
Integrate over latent states if those are not the focus of ecological inference (marginalization).
Marginalization often (but not always) improves MCMC. See [Ponisio et al. 2020](https://onlinelibrary.wiley.com/doi/full/10.1002/ece3.6053) for examples.
The [nimbleEcology](https://cran.r-project.org/web/packages/nimbleEcology/vignettes/Introduction_to_nimbleEcology.html) package implements capture-recapture models and HMMs with marginalization.
Our model $(\phi_A, \phi_B, \psi_{AB}, \psi_{BA}, p_A, p_B)$
```{r eval = FALSE}
multisite <- nimbleCode({
...
# Likelihood
for (i in 1:N){
# Define latent state at first capture
z[i,first[i]] <- y[i,first[i]] - 1
for (t in (first[i]+1):K){
# State process: draw S(t) given S(t-1)
z[i,t] ~ dcat(gamma[z[i,t-1],1:3])
# Observation process: draw O(t) given S(t)
y[i,t] ~ dcat(omega[z[i,t],1:3])
}
}
})
```
Same model with nimbleEcology
```{r eval = FALSE}
multisite <- nimbleCode({
...
# initial state probs
for(i in 1:N) {
init[i, 1:4] <- gamma[ y[i, first[i] ] - 1, 1:4 ] # first state propagation
}
# likelihood
for (i in 1:N){
y[i,(first[i]+1):K] ~ dHMM(init = init[i,1:4], # count data from first[i] + 1
probObs = omega[1:4,1:4], # observation matrix
probTrans = gamma[1:4,1:4], # transition matrix
len = K - first[i], # nb of occasions
checkRowSums = 0) # do not check whether elements in a row sum tp 1
}
...
```
This runs twice as fast as the standard formulation with explicit latent states.
Marginalizing typically gives better mixing.
Reducing redundant calculations
So far, a row of the dataset is an individual. However, several individuals may share the same encounter history.
The contribution of $M$ individuals with the same encounter history is the likelihood of this particular encounter history raised to the power $M$.
Using this so-called **weighted likelihood** greatly decreases the computational burden.
This idea is used in most computer programs that implement maximum likelihood. In the Bayesian framework, the same idea was proposed in [Turek et al. (2016)](https://doi.org/10.1007/s10651-016-0353-z).
Cannot be done in Jags. Can be done in nimble thanks to nimble functions! The run is *much* faster. Also allows fitting models to big datasets. More details in dedicated Worksheet.
## Summary
+ NIMBLE is under active development. Contributors are welcome, including those who want to get involved but don't know where.
+ Faster building of models and algorithms. Ability to save and re-load compiled work.
+ Automatic differentiation of model calculations, enabling Hamiltonian Monte Carlo, other sampling strategies, and Laplace approximation.
+ Tools for building packages that use NIMBLE "under the hood".
## Further reading
+ Turek, D., de Valpine, P. & Paciorek, C.J. [Efficient Markov chain Monte Carlo sampling for hierarchical hidden Markov models](https://doi.org/10.1007/s10651-016-0353-z) *Environ Ecol Stat* 23: 549–564 (2016).
+ Ponisio, L.C., de Valpine, P., Michaud, N., and Turek, D. [One size does not fit all: Customizing MCMC methods for hierarchical models using NIMBLE](https://doi.org/10.1002/ece3.6053) *Ecol Evol.* 10: 2385–2416 (2020).
+ Nimble workshop to come 26-28 May, check out [here](https://r-nimble.org/nimble-virtual-short-course-may-26-28).
+ Nimble workshop material online available [here](https://github.com/nimble-training).
+ Nimble [manual](https://r-nimble.org/html_manual/cha-welcome-nimble.html) and [cheatsheet](https://r-nimble.org/cheatsheets/NimbleCheatSheet.pdf).
## Tip by Cyril Milleret
- For a model that takes long to run, it might be useful to run MCMC in “bites”. This allows to check the results before the model is done running. For example we can save 80 bites each containing 250 iterations.
```
bite.size <- 250 # number of iterations in each bite to be exported and cleared
bites <- 80 # we run 80 bites containing of 250 iterations each.
for(nb in 1:80){
print(nb)
if(nb==1){# first bite
Cmcmc$run(bite.size)
}
if(nb>1){# second bite or more
Cmcmc$run(bite.size, reset = FALSE)
}
this.sample <- as.matrix(Cmcmc$mvSamples)# save the 250iterations
save(this.sample, RunTime, file=paste(”bite_”, nb, "RData”, sep=””))
}
```
Now imagine the MCMC is still running but 25 bites were finished. We can check the results of the 25 first bites (6250 iterations:
```
bites <- 25
MCMC <- list()
for(nb in 1: bites){
load(paste(”bite_”, nb, "RData”, sep=””))
MCMC[[nb]] <- this.sample
}
MCMCsamples <- as.mcmc(do.call(rbind, MCMC)))
```