VI in stan: codes and tutorials


  1. could someone point me to simple tutorials for VI in Stan in R and Python ?

  2. is the (R?) code used in the paper “Automatic Variational Inference in Stan” for the linear regression example with ARD available somewhere ?


1 Like

I’ll add one after the VI diagnostics is available in RStan.


Does this mean there’s an advisor rewrite somewhere in the Stan repo or otherwise in the works?

I never finished my attempt…

I don’t know what do you mean by advisor, but there is a pull request for VI diagnostics

In the meantime: It’s not Stan, but there exists a partial port of the “yes but did it work” paper in PyMC3:

Apparently the PyMC3 interface already exposes all the relevant ingredients. I think this is also a good tutorial to learn about how to do robust VI, in general.

Phone keyboard. Sorry.

I had started to clean up ADVI and I heard you and some others were wanting to clean up that code at the Stan conference. Just curious if that happened.

Isin’t the code in the supplement of their paper?

The authors of the paper do provide the stan code for the model in Appendix Figure 6. Though this is useful, this is only part of what I want: The full R code to run to reproduce the results. This would also have the benefit of exposing the VI API. All the experiments they run could be turned into tutorials. This would be great.

The link GitHub - yao-yl/Evaluating-Variational-Inference: Evaluating variational inference using Pareto-smoothed importance sampling and simulation-based calibration to the R code to reproduce all the results in the article is also given in the appendix.

Unfortunately no. It is possible to do many things with Stan and R without making such interfaces that we would recommend them for others. You can reproduce the article results, but not all results were computed using Stan and some were computed using R code for ADVI and log density. Before we advertise that the diagnostic is part of Stan, we need to integrate it into Stan. The related pull request is adding to the Stan output the log density with respect to approximation taking into account the Jacobians of parameter transformations. Without this, the Jacobian has to be re-computed outside of Stan, and thus the code used for the article it is not a good generic solution.

Enabling VI diagnostic doesn’t require exposing covariance matrix of the VI solution. Other changes to VI, like outputting covariance, are better left for the future rewrite.

I fully agree and they will be turned to tutorials, when the necessary parts are in integrated in Stan. We have pull request, so this is likely to happen. We ask for patience (or help with reviewing the pull request).

How would that work for non-Stan-dev members?

@ avehtari
I was in fact referring to the paper “variational inference in stan”, the link you mention is from the paper “yes, but did it work?..”; I find this link useful. Thanks.

Oops, sorry for the confusion. Just to clarify, when the diagnostics is in we’ll add examples from the “yes, but did it work? paper and some other examples, but not necessarily the examples from the"variational inference in Stan” paper.

In StanCon the discussion I was involved was more like “yes, we all agree that the current code is messy, and it would be good if it would be refactored”, but I don’t know anyone else than you working on it :)

I assume non-Stan-dev members are also allowed to comment pull requests. If not, let me know. So you could read the code and check that it is understandable and correct. You could also clone the repo and compile Stan from that branch to check that it works. If you are not familiar with Stan code guidelines, you can leave that part of the review for others.

Thanks for the reply, I think “working on it” might be a strong phrasing but seems like it’s worth it to dust off the branch

Yes please keep working on it! Being able to employ these diagnostics more broadly would greatly facilitate using VI in practical implementations.