Search code examples
rsamplingmcmcstan

Stan MCMC chains switching back and forth between warmup and sampling


I am currently using R combining with Stan to conduct MCMC sampling for obtaining posterior distribution of a certain demand variable d, given historical demand dH and currently observed variable x (so the formulation is figuring out P(d|dH, x), which is proportional to P(x|d)P(d|dH).

My question

I found it really weird that the sampling process shows MCMC jumping back and forth between warmup and sampling (isn't it the case that the first nth iterations are always in warmup stage, followed by actual sampling stage?) At the same time, it skipped Chain 1 completely(?!). Below is the picture of the progress it shows:

Mix of warmup and sampling

My code

for(i in 1:365){
  nrow = nrow(rte_m[[i]]);
  ncol = ncol(rte_m[[i]]);
  A <- as.matrix(rte_m[[i]]);
  sigma_x <- as.vector(sample.int(10, nrow(kf_vect[[i]]), replace=TRUE))
  sigma_y <- as.vector(eps_vect[[i]])
  yH <- as.vector(dh_vect[[i]]);
  yT <- yH + as.vector(eps_vect[[i]]); 
  epsilon <- sample.int(10, nrow(kf_vect[[i]]), replace=TRUE)
  x <- as.vector(as.matrix(rte_m[[i]])%*%yT) + epsilon
  iterations = 500;

  #input data into a list called stan_data
  stan_data = list(nrow = nrow, ncol = ncol,
                   yH = yH, 
                   x = x, epsilon = epsilon,
                   A = A, sigma_x = sigma_x, sigma_y = sigma_y);
  #input it into our Stan model file "stamodeling.stan"
  stanmodel1 <- stan_model(file = "stamodeling.stan",
                           model_name = "stanmodel1");

  #MCMC sampling
  stanfit <- sampling(stanmodel1, data = list(ncol = ncol,nrow = nrow,
                                              yH = yH, 
                                              x=x, epsilon = epsilon,
                                              A = A, sigma_x = sigma_x, sigma_y = sigma_y)
                      ,iter=iterations, warmup = 200, chains = 4, cores = 2);

Stan Modeling File

Data Files


Solution

  • What's happening isn't that a given chain is switching between warmup and sampling. Instead, what's happening is that the progress messages from the various chains are being interspersed with one another.

    So, for example, when you see the following:

    [Iteration:] 50/500 [0%] (Warmup)
    [Iteration:] 50/500 [0%] (Warmup)
    

    You're actually seeing two messages, one from Chain A and the second from Chain B.