Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug with adaptInterval=1 for RW_block #1493

Open
paciorek opened this issue Oct 10, 2024 · 1 comment
Open

bug with adaptInterval=1 for RW_block #1493

paciorek opened this issue Oct 10, 2024 · 1 comment
Labels

Comments

@paciorek
Copy link
Contributor

It runs (which I was thinking it might not because of 1-d vs 2-d allocation for the samples saved for calculating the covariance), but in a simple example, there are no acceptances. @danielturek or I should look into for 1.3.0 release.

code <- nimbleCode({
    y[1:2] ~ dmnorm(z[1:2],pr[1:2,1:2])
})
m <- nimbleModel(code, inits = list(z=rep(0,2), pr=diag(2)))
m$y = rnorm(2)

conf  <- configureMCMC(m, nodes=NULL)
conf$addSampler(target='y', type='RW_block', control = list(adaptInterval=1))
mcmc <- buildMCMC(conf)
cm <- compileNimble(m)
cmcmc <- compileNimble(mcmc,project=m)
cmcmc$run(1000)
tmp=as.matrix(cmcmc$mvSamples)
@paciorek paciorek added the bug label Oct 10, 2024
@danielturek
Copy link
Member

@paciorek The issue is essentially, when RW_block sampler estimates the empirical covariance from the recent samples (specifically, using the samples collected during the most recent adaptation period), that estimation of a 2nd order statistic (variance-covariance matrix) will fail when there's only 1 sample in each adaptation period (when adaptInterval = 1).

Looking the the code for the RW_block sampler, this problem manifests itself around line 745 or so, when first the "centering" of the empircal samples (here, a single sample), centers them to become all zero:

empirSamp[, i] <<- empirSamp[, i] - mean(empirSamp[, i])

and then worse, when it divides by $n-1$, or here timesRan-1, that's dividing by 0, which seems to give NaN, or something similar, and then the chol fails, and I guess it's downhill from there.

Maybe we could restrict adaptInterval to be at least 2? Or, set adaptScaleOnly = TRUE in the case when adaptInterval = 1, so that no adaptation of the proposal covariance happens? Or, modify the RW_block sampler it always uses more than 1 sample for this estimation? Just ideas..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants