Hi,

I am using Rstan. I have simulation result from previous a simulation called “fit_model” which used 3 chains.

Now, i would like to start another simulation with 3 chains whose initial values start from last values of “fit_model” and with no warm-up iterations. If I run the simulation with code as below. Would that mean I can start the second simulation from the last values of previous simulation?

```
fit_model2 = stan(fit=fit_model,
data = stan_data,
chains = 3,
warmup = 0,
iter = 2500,
refresh = 100,
control = list(adapt_delta = 0.9, max_treedepth = 10))
```

Thanks

Where did you learn about this workflow?

Thanks @increasechief, I am not sure about the workflow, and that’s why i have asked.

I have seen an example

which uses previous test run in the next simulation and was wondering if this doing exactly what i asked?

thanks

Ideally, I would like to start a second simulation from the last posterior value of previous run.

For this, I tried to specify **init** as a function. Here is a simple coin flip example I tried.

```
data {
int N;
int<lower =0, upper =1> x[N];
}
parameters {
real<lower=0, upper =1> theta;
}
model {
x ~ bernoulli(theta);//this is vectorised sampling
}
```

The R code with out put from previous and final simulation is:

```
coin_dat = list(N = 10,
x = c(0,1,0,0,0,0,0,0,0,1))
fit = stan("coin_flip.stan", data=coin_dat, seed=4838282)
post <- as.data.frame(fit)
tail(post, 1) #= 0.3293066
#set initial condition as last posterior value of "fit"
initf1 <- function() {
list(theta = 0.3293066)
}
fit2 = stan("coin_flip.stan", data=coin_dat, init = initf1, chains=1, iter = 100, warmup = 0, seed=4838282)
post_fit2 <- as.data.frame(fit2)
head(post_fit2, 1) #= 0.3019219
```

Would this be the right method to do this? If not, how do i improve the code? Why wouldn’t the starting value of post_fit2 same as 0.3293066, since there are no warmup iterations?

Thanks

I figured out that I did not use the correct function to check initial value of *fit2* above.

Using *get_inits(fit2)* produced the value 0.3293066, which is same as the last posterior value of previous simulation.

1 Like

This is not safe unless you also specify the mass matrix to be the same (because otherwise you’re not continuing the same Markov chain). I am not sure if that is currently possible in RStan

Just to clarify: You can start your chain wherever you want (including previous values). What is **not safe** is running Stan with no warmup except in the very special situation where you are continuing the Markov chain, but this requires all of the MCMC tuning parameters (which are found adaptively during Warm Up and will be different for each run and each chain) to be set to their previous value.

2 Likes

Thanks @anon75146577.

What is **not safe** is running Stan with no warmup except in the very special situation where you are continuing the Markov chain, but this requires all of the MCMC tuning parameters (which are found adaptively during Warm Up and will be different for each run and each chain) to be set to their previous value.

Does this mean that starting the simulation from the end values of previous posterior output, with warmup =0 is incorrect?

This is not safe unless you also specify the mass matrix to be the same

Would anyone please let me know if it’s possible to specify mass matrix. If so, would you mind giving an example.

Thanks

It might be helpful to tag this as “rstan” so that the relevant people see it to help with mass matrix stuff.

For your other question, the main reason to start a Markov chain at a previous output is to continue it. In that case you need to make sure the mass matrices are correct.

Otherwise you are just starting a chain and you need warm up just like always. In Stan, the warm up not only gives the chain time to find the typical set, but it is also where all the adaptation happens. So it’s not an optional step.