# Setting max time for sampling?

**URL:** <https://discourse.mc-stan.org/t/setting-max-time-for-sampling/5013>\
**Category:** Modeling\
**Tags:** fitting-issues\
**Created:** [July 28, 2018, 7:26pm UTC](https://discourse.mc-stan.org/t/setting-max-time-for-sampling/5013 "2018-07-28T19:26:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanOvando](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/danovando/32/2570_2.png) [@DanOvando](https://discourse.mc-stan.org/u/DanOvando)\
**Post date:** [July 28, 2018, 7:26pm UTC](https://discourse.mc-stan.org/t/setting-max-time-for-sampling/5013/1 "2018-07-28T19:26:13Z")

</div>

I’m simulation testing a Stan model across several thousand draws from an operating model. 99% of the time the model works great and converges within about 2 minutes. However, sometimes the model has a really hard time with a specific simulation, and goes from a 2 minute runtime to a 3.5 hour runtime.

Obviously there’s something wrong with my model in those circumstances, but rather than tie up a core for 3.5 hours on that run, I’d rather allow the model to move on after a specified amount of time and just store as many iterations as it got through by that point, so that I can then take a look at those runs and figure out what went wrong after the fact (obviously not to actually use the estimates from that run). I could just cap the max treedepth, but I’m worried that that would mess up my successful runs that sometime need higher treedepths?

I don’t see any options for that in the calls for rstan or sampling, any thoughts?

Thanks!

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [July 29, 2018, 3:48am UTC](https://discourse.mc-stan.org/t/setting-max-time-for-sampling/5013/2 "2018-07-29T03:48:48Z")

</div>

Does not currently exist. I would try starting with a smaller `init_r` than 2 in order to get more consistent wall times.

---

<div class="post-metadata">

**Author:** ![DanOvando](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/danovando/32/2570_2.png) [@DanOvando](https://discourse.mc-stan.org/u/DanOvando)\
**Post date:** [July 29, 2018, 5:20am UTC](https://discourse.mc-stan.org/t/setting-max-time-for-sampling/5013/3 "2018-07-29T05:20:09Z")

</div>

Thanks! I’ll give that a shot

---

<div class="post-metadata">

**Author:** ![mike-lawrence](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mike-lawrence/32/59_2.png) [@mike-lawrence](https://discourse.mc-stan.org/u/mike-lawrence)\
**Post date:** [July 29, 2018, 11:05pm UTC](https://discourse.mc-stan.org/t/setting-max-time-for-sampling/5013/4 "2018-07-29T23:05:47Z")

</div>

This could be achieved by tweaking [ezStan::watchStan](https://github.com/mike-lawrence/ezStan/blob/master/R/watch_stan.R) to kill after a supplied time. I don’t have time to implement this for a couple weeks (defending my dissertation Aug 14!), but if you look at how I handle the “kill\_on\_divergence” argument it should be straightforward to emulate/tweak.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [August 1, 2018, 6:46pm UTC](https://discourse.mc-stan.org/t/setting-max-time-for-sampling/5013/5 "2018-08-01T18:46:14Z")

</div>

> [@mike-lawrence](#):
>
> defending my dissertation Aug 14!

Not to hijack the thread, but congratulations.

> [@DanOvando](#):
>
> Obviously there’s something wrong with my model in those circumstances, but rather than tie up a core for 3.5 hours on that run, I’d rather allow the model to move on after a specified amount of time and just store as many iterations as it got through by that point,

You can set streaming output, too, so that if you kill the process, it won’t kill your existing output. You can also store the random seed so that you can reproduce without storing anything.

---

<div class="post-metadata">

**Author:** ![DanOvando](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/danovando/32/2570_2.png) [@DanOvando](https://discourse.mc-stan.org/u/DanOvando)\
**Post date:** [August 1, 2018, 7:30pm UTC](https://discourse.mc-stan.org/t/setting-max-time-for-sampling/5013/6 "2018-08-01T19:30:45Z")

</div>

Thanks Mike, congratulations indeed! I feel you, defending on the 22nd myself, so I’ll take a look at this sometime after that

---

<div class="post-metadata">

**Author:** ![DanOvando](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/danovando/32/2570_2.png) [@DanOvando](https://discourse.mc-stan.org/u/DanOvando)\
**Post date:** [August 1, 2018, 7:37pm UTC](https://discourse.mc-stan.org/t/setting-max-time-for-sampling/5013/7 "2018-08-01T19:37:58Z")

</div>

Good to know, I’ll give that a shot, though would still need to find some way, maybe using Mike’s suggestion, to kill the process after some period of time right? Thanks!

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [August 1, 2018, 7:39pm UTC](https://discourse.mc-stan.org/t/setting-max-time-for-sampling/5013/8 "2018-08-01T19:39:08Z")

</div>

> [@DanOvando](#):
>
> I feel you, defending on the 22nd myself

A second congratulations. Hope you’re at institutions where the defense is pretty much a done deal. It was actually nerve-wracking in the UK getting outside examiners.
