# Variational Bayes results seems sensible, but vary - What to change?

**URL:** <https://discourse.mc-stan.org/t/variational-bayes-results-seems-sensible-but-vary-what-to-change/18119>\
**Category:** Modeling\
**Tags:** variational-bayes\
**Created:** [September 22, 2020, 7:22am UTC](https://discourse.mc-stan.org/t/variational-bayes-results-seems-sensible-but-vary-what-to-change/18119 "2020-09-22T07:22:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bjoern](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bjoern/32/5429_2.png) [@Bjoern](https://discourse.mc-stan.org/u/Bjoern)\
**Post date:** [September 22, 2020, 7:22am UTC](https://discourse.mc-stan.org/t/variational-bayes-results-seems-sensible-but-vary-what-to-change/18119/1 "2020-09-22T07:22:39Z")

</div>

I am running a model that works nicely using NUTS (but is a bit slow) using variational Bayes (mostly because its a pretty simple model and I hope it would be a lot faster using VB, which for the particular use case would be really useful). I am getting sensible answers in the posterior samples, but I noticed that with different random number seeds I get more variation in e.g. median estimates that I would have expected by Monte-Carlo error alone (e.g. NUTS gives me more stable results). Thus, I suspect the algorithm did not truly converge or did not converge to the same (local?) optimum each time.

So, some of my ideas were:

1. I could change some parameters of VB (like one often ends up putting adapt\_delta to a higher value than the default and it just improves things). However, I am not sure what parameters I would logically try changing/making more stringent first? elbo samples? Tolerance? Something else?

2. Just run the VB several times (given that full NUTS takes several min and VB a few seconds that could be an option) and average outputs (or pick some based on some criterion of better fit?)?

Does someone have some experience with this and some recommendations on what they would try?

---

<div class="post-metadata">

**Author:** ![avehtari](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/avehtari/32/5935_2.png) [@avehtari](https://discourse.mc-stan.org/u/avehtari)\
**Post date:** [September 22, 2020, 5:38pm UTC](https://discourse.mc-stan.org/t/variational-bayes-results-seems-sensible-but-vary-what-to-change/18119/2 "2020-09-22T17:38:02Z")

</div>

For background on the problems with the current implementation and potential improvement see [https://arxiv.org/abs/2009.00666](https://arxiv.org/abs/2009.00666). These improvements are not yet implemented in Stan, but you can see which parameters to vary and how that could affect the results. Also the paper discusses why averaging of variational parameters is sensible.

---

<div class="post-metadata">

**Author:** ![songpeng](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/songpeng/32/1967_2.png) [@songpeng](https://discourse.mc-stan.org/u/songpeng)\
**Post date:** [November 6, 2020, 3:51pm UTC](https://discourse.mc-stan.org/t/variational-bayes-results-seems-sensible-but-vary-what-to-change/18119/3 "2020-11-06T15:51:33Z")

</div>

Hi @avehtari  
The samples using drew from the variational inference is coming from after the optimization or during the optimization ?

Is that possible to get the learned parameters of the approximated distribution?

---

<div class="post-metadata">

**Author:** ![avehtari](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/avehtari/32/5935_2.png) [@avehtari](https://discourse.mc-stan.org/u/avehtari)\
**Post date:** [November 6, 2020, 4:00pm UTC](https://discourse.mc-stan.org/t/variational-bayes-results-seems-sensible-but-vary-what-to-change/18119/4 "2020-11-06T16:00:29Z")

</div>

During the stochastic optimization draws from the approximation are used to compute gradient and elbo, but not stored. After the stochastic optimization the draws from the approximation are stored, but the parameters of the approximation are not currently stored.

---

<div class="post-metadata">

**Author:** ![songpeng](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/songpeng/32/1967_2.png) [@songpeng](https://discourse.mc-stan.org/u/songpeng)\
**Post date:** [November 6, 2020, 4:05pm UTC](https://discourse.mc-stan.org/t/variational-bayes-results-seems-sensible-but-vary-what-to-change/18119/5 "2020-11-06T16:05:21Z")

</div>

Thank you so much!

One more question. I know under hierarchical modeling, we’d better use reparameterization for HMC. But for VI, since we directly sample from the approximated distribution, my understanding is that the reparameterization is not needed or helps a little for VI. Am I right?

---

<div class="post-metadata">

**Author:** ![avehtari](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/avehtari/32/5935_2.png) [@avehtari](https://discourse.mc-stan.org/u/avehtari)\
**Post date:** [November 6, 2020, 4:06pm UTC](https://discourse.mc-stan.org/t/variational-bayes-results-seems-sensible-but-vary-what-to-change/18119/6 "2020-11-06T16:06:50Z")

</div>

Reparameterization is even more important for VI, as the approximate distribution is (often) normal distribution and then we want the true posterior to be close to normal.

---

<div class="post-metadata">

**Author:** ![songpeng](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/songpeng/32/1967_2.png) [@songpeng](https://discourse.mc-stan.org/u/songpeng)\
**Post date:** [November 6, 2020, 8:21pm UTC](https://discourse.mc-stan.org/t/variational-bayes-results-seems-sensible-but-vary-what-to-change/18119/7 "2020-11-06T20:21:49Z")

</div>

Thank you very much!
