# Start from previous simulation

**URL:** <https://discourse.mc-stan.org/t/start-from-previous-simulation/8282>\
**Category:** Modeling\
**Created:** [March 28, 2019, 9:58pm UTC](https://discourse.mc-stan.org/t/start-from-previous-simulation/8282 "2019-03-28T21:58:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhunfini](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/dhunfini/32/16362_2.png) [@dhunfini](https://discourse.mc-stan.org/u/dhunfini)\
**Post date:** [March 28, 2019, 9:58pm UTC](https://discourse.mc-stan.org/t/start-from-previous-simulation/8282/1 "2019-03-28T21:58:02Z")

</div>

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?

```R
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

---

<div class="post-metadata">

**Author:** ![increasechief](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/increasechief/32/2039_2.png) [@increasechief](https://discourse.mc-stan.org/u/increasechief)\
**Post date:** [March 28, 2019, 11:40pm UTC](https://discourse.mc-stan.org/t/start-from-previous-simulation/8282/2 "2019-03-28T23:40:37Z")

</div>

Where did you learn about this workflow?

---

<div class="post-metadata">

**Author:** ![dhunfini](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/dhunfini/32/16362_2.png) [@dhunfini](https://discourse.mc-stan.org/u/dhunfini)\
**Post date:** [March 29, 2019, 12:38am UTC](https://discourse.mc-stan.org/t/start-from-previous-simulation/8282/3 "2019-03-29T00:38:19Z")

</div>

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

> **[Estimating transmission by fitting mechanistic models in Stan](https://jrmihalj.github.io/estimating-transmission-by-fitting-mechanistic-models-in-Stan/)**
>
> Mechanistic disease models are at the heart of disease ecology and have generated fundamental biological insights, ranging from our understanding of disease-density thresholds to the influence of host heterogeneity on the spread of disease. The...

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

thanks

---

<div class="post-metadata">

**Author:** ![dhunfini](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/dhunfini/32/16362_2.png) [@dhunfini](https://discourse.mc-stan.org/u/dhunfini)\
**Post date:** [March 29, 2019, 3:02am UTC](https://discourse.mc-stan.org/t/start-from-previous-simulation/8282/4 "2019-03-29T03:02:22Z")

</div>

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.

```stan
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:

```R
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

---

<div class="post-metadata">

**Author:** ![dhunfini](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/dhunfini/32/16362_2.png) [@dhunfini](https://discourse.mc-stan.org/u/dhunfini)\
**Post date:** [March 29, 2019, 3:45am UTC](https://discourse.mc-stan.org/t/start-from-previous-simulation/8282/5 "2019-03-29T03:45:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anon75146577](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon75146577](https://discourse.mc-stan.org/u/anon75146577)\
**Post date:** [March 29, 2019, 2:41pm UTC](https://discourse.mc-stan.org/t/start-from-previous-simulation/8282/6 "2019-03-29T14:41:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dhunfini](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/dhunfini/32/16362_2.png) [@dhunfini](https://discourse.mc-stan.org/u/dhunfini)\
**Post date:** [April 1, 2019, 12:06am UTC](https://discourse.mc-stan.org/t/start-from-previous-simulation/8282/7 "2019-04-01T00:06:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon75146577](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon75146577](https://discourse.mc-stan.org/u/anon75146577)\
**Post date:** [April 1, 2019, 12:12am UTC](https://discourse.mc-stan.org/t/start-from-previous-simulation/8282/8 "2019-04-01T00:12:36Z")

</div>

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.
