@@ -316,7 +316,7 @@ test_that('HMC on conjugate Wishart', {
316316 OmegaSimTrueSDs <- apply(wishRV , c(1 ,2 ), sd )
317317 # #
318318 expect_equal(as.numeric(apply(samples , 2 , mean )), as.numeric(OmegaTrueMean ), tol = 0.1 )
319- expect_equal(as.numeric(apply(samples , 2 , sd )), as.numeric(OmegaSimTrueSDs ), tol = 0.02 )
319+ expect_equal(as.numeric(apply(samples , 2 , sd )), as.numeric(OmegaSimTrueSDs ), tol = 0.1 )
320320})
321321
322322test_that(' HMC on LKJ' , {
@@ -564,8 +564,8 @@ test_that('HMC runs with various non-differentiable constructs', {
564564})
565565
566566test_that(' HMC results for CAR match non-HMC' , {
567- set.seed(1 )
568- code <- nimbleCode({
567+ set.seed(1 )
568+ code <- nimbleCode({
569569 S [1 : N ] ~ dcar_normal(adj [1 : L ], weights [1 : L ], num [1 : N ], tau )
570570 tau ~ dunif(0 , 5 )
571571 for (i in 1 : N )
@@ -575,32 +575,31 @@ test_that('HMC results for CAR match non-HMC', {
575575 Y [i ] ~ dpois(lambda [i ])
576576 }
577577 })
578-
578+ # #
579579 constants <- list (N = 6 ,
580580 num = c(1 ,2 ,2 ,2 ,2 ,1 ),
581581 adj = c(2 , 1 ,3 , 2 ,4 , 3 ,5 , 4 ,6 , 5 ),
582582 weights = rep(1 , 10 ),
583583 L = 10 )
584584 data <- list (Y = c(1 ,0 ,2 ,1 ,4 ,3 ))
585585 inits <- list (tau = 1 , S = c(0 ,0 ,0 ,0 ,0 ,0 ))
586-
586+ # #
587587 Rmodel <- nimbleModel(code , constants , data , inits , buildDerivs = TRUE )
588588 conf <- configureMCMC(Rmodel , monitors = c(' tau' ,' S' ))
589589 Rmcmc <- buildMCMC(conf )
590590 Cmodel <- compileNimble(Rmodel )
591591 Cmcmc <- compileNimble(Rmcmc , project = Rmodel )
592592 out <- runMCMC(Cmcmc , niter = 505000 , nburnin = 5000 , thin = 500 )
593-
593+ # #
594594 Rmodel <- nimbleModel(code , constants , data , inits , buildDerivs = TRUE )
595595 conf <- configureHMC(Rmodel , monitors = c(' tau' ,' S' ))
596596 Rmcmc <- buildMCMC(conf )
597597 Cmodel <- compileNimble(Rmodel )
598598 Cmcmc <- compileNimble(Rmcmc , project = Rmodel )
599599 outHMC <- runMCMC(Cmcmc , niter = 22000 , nburnin = 2000 , thin = 20 )
600-
600+ # #
601601 expect_equal(apply(out [,1 : 6 ],2 ,mean ), apply(outHMC [,1 : 6 ],2 ,mean ), tolerance = .06 )
602602 expect_equal(mean(out [,7 ]),mean(outHMC [,7 ]), tolerance = .15 )
603-
604603 expect_equal(apply(out ,2 ,quantile ,c(.1 ,.9 )), apply(outHMC ,2 ,quantile ,c(.1 ,.9 )), tolerance = 0.15 )
605604})
606605
@@ -624,35 +623,33 @@ test_that('HMC results for mixture model match non-HMC', {
624623 mu <- c(0 ,2 ,4 )
625624 data <- list (y = sample(c(rnorm(50 ,mu [1 ],.35 ), rnorm(250 ,mu [2 ],.35 ), rnorm(200 ,mu [3 ],.35 )), n , replace = FALSE ))
626625 inits <- list (k = sample(1 : K ,n ,replace = T ),mu = c(- 1 ,2 ,6 ),mu0 = 1 )
627-
626+ # #
628627 m <- nimbleModel(code , constants = constants , data = data ,
629628 inits = inits , buildDerivs = TRUE )
630629 conf <- configureMCMC(m , monitors = c(' mu' ,' mu0' ,' p' ))
631630 mcmc <- buildMCMC(conf )
632631 cm <- compileNimble(m )
633632 cmcmc <- compileNimble(mcmc )
634633 out <- runMCMC(cmcmc , niter = 50000 , nburnin = 10000 , thin = 40 )
635-
634+ # #
636635 m <- nimbleModel(code , constants = constants , data = data ,
637636 inits = inits , buildDerivs = TRUE )
638637 conf <- configureMCMC(m , nodes = c(' k' ), monitors = c(' mu' ,' mu0' ,' p' ))
639- conf $ addSampler(c(' mu0' ,' mu' ,' p' ),' NUTS' )
638+ conf $ addSampler(c(' mu0' ,' mu' ,' p' ), ' NUTS' )
640639 mcmc <- buildMCMC(conf )
641640 cm <- compileNimble(m )
642641 cmcmc <- compileNimble(mcmc )
643642 outHMC <- runMCMC(cmcmc , niter = 22000 , nburnin = 2000 , thin = 20 )
644-
645- # # Deal with label switching.
643+ # # deal with label switching
646644 sorter <- function (row ) {
647645 ord <- order(row [1 : 3 ])
648646 return (c(row [1 : 3 ][ord ], row [4 ], row [5 : 7 ][ord ]))
649647 }
650648 out <- t(apply(out , 1 , sorter ))
651649 outHMC <- t(apply(outHMC , 1 , sorter ))
652-
653- expect_equal(apply(out ,2 ,mean ), apply(outHMC ,2 ,mean ), tolerance = .1 )
650+ # #
651+ expect_equal(apply(out ,2 ,mean ), apply(outHMC ,2 ,mean ), tolerance = 0 .1 )
654652 expect_equal(apply(out ,2 ,quantile ,c(.1 ,.9 )), apply(outHMC ,2 ,quantile ,c(.1 ,.9 )), tolerance = 0.1 )
655-
656653})
657654
658655
0 commit comments