Extract samples from vb() like StanFit4model.extract()

I usually use the extract() method on a StanFit4model returned from sampling() output, which returns a nice dictionary with keys as parameter names and multidimensional numpy arrays as values containing the posterior samples. However, the vb() returns a list of lists in a cumbersome flattened-like format, where each index in a multidimensional parameter has its own separate list of samples. I’m guessing this will be fixed some time in the near future, but until then I’d like to share a quick-and-dirty function I use so that I can basically do a drop-in replacement of vb() and sampling() by only changing 1 or 2 lines.

4 Likes

Thanks for sharing the workaround.

Yes, we should be doing a lot of work on I/O formats as part of the big Stan 3 push, which will get rolled out before Stan 3 with deprecation of old functions and a migration path forward.