Take the 8schools_ncp example:
library(cmdstanr)
example_program = 'schools_ncp.stan'
example_data = 'schools_ncp.data.json'
tmp <- file.path(tempdir(), example_program)
if (!file.exists(tmp)) {
file.copy(system.file(example_program, package = "cmdstanr"), tmp)
}
mod <- cmdstan_model(tmp)
data_file <- system.file(example_data, package = "cmdstanr")
seed = 1
iter_warmup = 1e3
iter_sample = 1e3
parallel_chains = 4
#first the typical combined warmup-and-sample run:
both = mod$sample(
data = data_file
, chains = parallel_chains
, parallel_chains = parallel_chains
, refresh = 0
, show_messages = F
, seed = seed
, iter_warmup = iter_warmup
, iter_sampling = iter_sample
)
Which yields no divergences. Now an attempt at doing warmup and sampling separately, using the adapted info from the former in the latter:
warmup = mod$sample(
data = data_file
, chains = parallel_chains
, parallel_chains = parallel_chains
, refresh = 0
, show_messages = F
, seed = seed
, iter_warmup = iter_warmup
, save_warmup = T #for inits
, sig_figs = 18
, iter_sampling = 0
)
get_sampling_inits_from_warmup = function(chain_id){
warmup_draws = warmup$draws(inc_warmup=T)
final_warmup_value = warmup_draws[iter_warmup,chain_id,]
init_list = as.list(final_warmup_value)
names(init_list) = dimnames(final_warmup_value)[[3]]
init_list = init_list[names(init_list)!='lp__']
return(init_list)
}
samples = mod$sample(
data = data_file
, chains = parallel_chains
, parallel_chains = parallel_chains
, refresh = 0
, show_messages = F
, seed = seed
, iter_warmup = 0
, adapt_engaged = FALSE
, inv_metric = warmup$inv_metric(matrix=F)
, step_size = warmup$metadata()$step_size_adaptation
, iter_sampling = iter_sample
, init = get_sampling_inits_from_warmup
)
And now we get divergences from the sampling run. I have more rigorous testing of this detailed here, but the gist is that doing the warmup-then-sample approach is somehow generating more divergences than the combined warmup-and-sample approach despite to all examination all the inputs being as expected. I don't believe this is an issue with cmdstanr's passing of the pertinent info to cmdstan because I've checked the init jsons and output csvs and they have all the expected content.
Take the 8schools_ncp example:
Which yields no divergences. Now an attempt at doing warmup and sampling separately, using the adapted info from the former in the latter:
And now we get divergences from the sampling run. I have more rigorous testing of this detailed here, but the gist is that doing the warmup-then-sample approach is somehow generating more divergences than the combined warmup-and-sample approach despite to all examination all the inputs being as expected. I don't believe this is an issue with cmdstanr's passing of the pertinent info to cmdstan because I've checked the init jsons and output csvs and they have all the expected content.