Skip to content

Commit 8502487

Browse files
committed
added test of gamma - car_normal conjugacy
1 parent 161775d commit 8502487

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

packages/nimble/tests/testthat/test-mcmc.R

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3021,7 +3021,42 @@ test_that('assigning samplers to data and allowData argument', {
30213021
expect_true(samps[[4]]$name == 'posterior_predictive')
30223022
})
30233023

3024-
3024+
test_that('asymptotic correct results from conjugate gamma - CAR_normal sampler', {
3025+
num1 <- c(1, 2, 2, 1)
3026+
adj1 <- c(2, 1, 3, 2, 4, 3)
3027+
num2 <- c(4, 2, 3, 1, 2, 2)
3028+
adj2 <- c(2, 4, 5, 6, 1, 3, 2, 5, 6, 1, 1, 3, 1, 3)
3029+
constants <- list(num1=num1, adj1=adj1, N1=length(num1), L1=length(adj1),
3030+
num2=num2, adj2=adj2, N2=length(num2), L2=length(adj2))
3031+
data <- list(y = 1:6)
3032+
inits <- list(t=1, x1=1:length(num1), x2=1:length(num2))
3033+
code <- nimbleCode({
3034+
t ~ dgamma(2, 5)
3035+
x1[1:N1] ~ dcar_normal(adj=adj1[1:L1], num=num1[1:N1], tau=t/2)
3036+
x2[1:N2] ~ dcar_normal(adj=adj2[1:L2], num=num2[1:N2], tau=t/3)
3037+
y[1] ~ dnorm(x1[1], 1)
3038+
y[2] ~ dnorm(x2[1], 1)
3039+
y[3] ~ dnorm(x1[1]+x1[4], 1)
3040+
y[4] ~ dnorm(x2[1]+2*x2[6], 1)
3041+
y[5] ~ dexp(5*t)
3042+
y[6] ~ dpois(2*t)
3043+
})
3044+
##
3045+
Rmodel <- nimbleModel(code, constants, data, inits)
3046+
conf <- configureMCMC(Rmodel, monitors = c('t','x1','x2'), useConjugacy = FALSE)
3047+
Rmcmc <- buildMCMC(conf)
3048+
compiledList <- compileNimble(list(model=Rmodel, mcmc=Rmcmc))
3049+
summary_RW <- runMCMC(compiledList$mcmc, 500000, nburnin = 100000, samples = FALSE, summary = TRUE, setSeed = 0)
3050+
##
3051+
Rmodel <- nimbleModel(code, constants, data, inits)
3052+
conf <- configureMCMC(Rmodel, monitors = c('t','x1','x2'))
3053+
expect_true(grepl('^conjugate_dgamma_dcar_normal', conf$getSamplers('t')[[1]]$name))
3054+
Rmcmc <- buildMCMC(conf)
3055+
compiledList <- compileNimble(list(model=Rmodel, mcmc=Rmcmc))
3056+
summary_conj <- runMCMC(compiledList$mcmc, 500000, nburnin = 100000, samples = FALSE, summary = TRUE, setSeed = 0)
3057+
##
3058+
expect_true(all(abs(summary_RW - summary_conj) < 0.04))
3059+
})
30253060

30263061
sink(NULL)
30273062

0 commit comments

Comments
 (0)