msteyvers / matjags

MATJAGS, a Matlab interface for JAGS
16 stars 10 forks source link

Adaption Steps #6

Closed NilsWinter closed 8 years ago

NilsWinter commented 8 years ago

Hi,

thanks for contributing this neat Matlab-JAGS interface! I'm currently trying to write a Matlab implementation of John K. Kruschke's R code that comes with his great book "Doing Bayesian Data Analysis: A Tutorial for R, JAGS, and Stan.". He is using Rjags and specifies some adaption steps, that JAGS uses to tune the MCMC chains. This is something different than the burn-in steps and increases efficiency of the chains.

I was wondering if it is possible to add that functionality to matjags.m. Do you have any idea on how to add that specification?

Cheers, Nils

drbenvincent commented 8 years ago

Personally, I'm not so familiar with the inner workings of JAGS and have just contributed a few things on the matlab side of matjags. But from an initial look, it seems possible by changing the JAGS file being created by matjags.

From the JAGS manual, it seems to want a script file in this form: screen shot 2016-03-29 at 16 08 18 where the first update line seems to specify both update and burnin. This is implemented in matjags here: screen shot 2016-03-29 at 16 09 13

From the JAGS manual:

By default, adaptive mode is turned off half way through first update of a JAGS model.

but

The adapt command (see section 3.1.7) can be used for more control over the adaptive phase. The adapt command updates the model but keeps it in adaptive mode. At the end of each update, the convergence test is called. The message “Adaptation successful” will be printed if the convergence test is successful, otherwise the message will read “Adaptation incomplete”. Successive calls to adapt are possible while keeping the model in adaptive mode. The next update command will immediately turn off adaptive mode.

So it looks fairly simple to implement. I'll wait and see if @msteyvers has something more authoritative to say before I do anything else. Reading section 3.17, it looks like the number of adapt steps have to be chosen with purpose. Maybe that would be clearer from looking at the Rjags code and examples of calling it.

NilsWinter commented 8 years ago

Thanks for answering! That cleared quite a few things up for me! So the first update, like you said, seems to implement both burnin and adaption, and by default, JAGS will use half of these update steps to adapt. So even if one doesn't specify any adaption steps, JAGS will do some adaption. I didn't know that...

Section 3.1.7 of the JAGS manual says: jags-manual-adapt

If I correctly understand that, you could start with the adapt command (i.e. "adapt 1000"), then do some additional burnin by using the update command (i.e. "update 1000") and after that just continue with the normal script, which will use the update command (i.e. "update 10000") for the iterations that will be saved. Does that make sense?

That, like you said, looks fairly simple to implement. Your comment was a great help!

drbenvincent commented 8 years ago

Yep, it's a one-liner, aside from setting defaults.

I've committed it - have a look and see if you have any comments before I do a pull request

The manual is a little unclear, but I think the adapt step is perhaps tuning the width of the proposal and the burnin is for convergence (although the STAN folks suggest the term warmup rather than burnin https://twitter.com/mcmc_stan/status/690582222588477440)

I've done a very quick test on a hierarchical model that JAGS fails (in terms of Rhat) in 10^5 samples. I can confirm that by adding 5000 adaption samples takes longer to compute (so I'm reasonably sure JAGS is doing the adaption step), but on this particular problem I did not see any improvement in convergence (still getting some unacceptably high Rhat values).

NilsWinter commented 8 years ago

Looks good. I wouldn't change anything.

The documentation for the R-package "rjags" by Martyn Plummer describes adaption like this: "When a JAGS model is compiled, it may require an initial sampling phase during which the samplers adapt their behaviour to maximize their efficiency (e.g. a Metropolis-Hastings random walk algorithm may change its step size). The sequence of samples generated during this adaptive phase is not a Markov chain, and therefore may not be used for posterior inference on the model."

Not sure if one would still need an additional burn-in period (in case adaption is used).

Maybe your test model is too complex for JAGS and Stan would - in that case - work better regardless of the adaption and burnin. I'm referring to John Kruschke (Doing Bayesian Data Analysis 2ed, 2015) who suggests to use Stan when it comes to complex hierarchical models.

Nonetheless nice to have that adaption option available with matjags.

drbenvincent commented 8 years ago

Great. Will submit pull request. I guess you are in charge of closing the issue once you're happy - I don't see an option to do that.

For the record, the models I use in this toolbox are the kind of complexity that JAGS starts having trouble with, especially with larger datasets. http://www.inferencelab.com/delay-discounting-analysis/ JAGS is a great place to start, but it's limitations are frustrating.

msteyvers commented 8 years ago

Thanks guys for clarifying this issue. I am traveling at the moment, so don't have time to contribute. I just accepted the merge request. Let me know if I need to close the issue.

NilsWinter commented 8 years ago

Thanks a lot. I'll now close this issue.