Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions BayesianTools/R/mcmcRun.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ runMCMC <- function(bayesianSetup , sampler = "DEzs", settings = NULL){

if(restart == T) if(!getPossibleSamplerTypes()$restartable[which(getPossibleSamplerTypes()$BTname == settings$sampler)]) stop("This sampler can not be restarted")

########### Recursive call in case multiple chains are to be run
if(settings$nrChains >1){
########### Recursive call for multiple chains or restarting a sampler list
if(settings$nrChains > 1 ||
(restart && inherits(bayesianSetup, "mcmcSamplerList"))){

# Initialize output list
out<- list()
Expand Down
48 changes: 48 additions & 0 deletions BayesianTools/tests/testthat/test-runMCMC-singleton-list.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Suggested regression test for BayesianTools/tests/testthat/.
for (sampler in c("DEzs", "DREAMzs")) {
testthat::test_that(paste(sampler, "resumes singleton and longer sampler lists"), {
set.seed(1)
setup <- createBayesianSetup(
likelihood = function(x) sum(dnorm(x, log = TRUE)),
lower = rep(-10, 2),
upper = rep(10, 2)
)
fit <- runMCMC(
setup, sampler = sampler,
settings = list(iterations = 30, burnin = 0, nrChains = 1,
startValue = 3, message = FALSE)
)
testthat::expect_s3_class(fit, "mcmcSampler")

for (settings in list(NULL, list(iterations = 15, burnin = 0,
message = FALSE))) {
# A singleton list should resume just like the first entry of a longer list.
set.seed(2)
longer <- runMCMC(createMcmcSamplerList(list(fit, fit)), settings = settings)
set.seed(2)
singleton <- runMCMC(createMcmcSamplerList(list(fit)), settings = settings)

testthat::expect_s3_class(singleton, "mcmcSamplerList")
testthat::expect_length(singleton, 1L)
testthat::expect_s3_class(longer, "mcmcSamplerList")
testthat::expect_length(longer, 2L)
testthat::expect_identical(singleton[[1]]$chain, longer[[1]]$chain)
testthat::expect_identical(singleton[[1]]$X, longer[[1]]$X)
testthat::expect_identical(singleton[[1]]$Z, longer[[1]]$Z)

for (output in list(singleton, longer)) {
for (i in seq_along(output)) {
testthat::expect_s3_class(output[[i]], "mcmcSampler")
for (j in seq_along(fit$chain)) {
before <- as.matrix(fit$chain[[j]])
after <- as.matrix(output[[i]]$chain[[j]])
testthat::expect_gt(nrow(after), nrow(before))
testthat::expect_identical(
unname(after[seq_len(nrow(before)), , drop = FALSE]), unname(before)
)
}
}
}
}
})
}