Obtaining posterior draws from optimize in CmdStan

I am using Stan’s optimizer in a setting where speed (and system stability) is essential. In R, I can use the draws argument to rstan::optimizing to obtain a set of approximate posterior draws – which is great. (RStan produces draws “from a multivariate normal distribution whose parameters are the mean vector and the inverse negative Hessian in the unconstrained space.”)

Unfortunately, RStan and CmdStanR has some trouble reading large arrays of draws in and out of memory when I use the fixed_param method to generate predictions, which forces me to use optimize from CmdStan for at least some Stan files. The organization I am doing this work for is generally reluctant to use R in production due to these kinds of issues.

Is there a way to obtain posterior draws using optimize from CmdStan?

The answer is currently no, but this will be available in the next release (which comes out this month): Laplace approximation sampler · Issue #3146 · stan-dev/stan · GitHub

How exactly this will be exposed in CmdStanR will probably take a little bit of extra time to shake out.

3 Likes

Ok, this sounds great. Thanks for the quick answer!

I see CmdStan v2.32.0 was just pre-released, but I guess there is no updated manual yet?

How would I specify that want to use Laplace approximation to get posterior draws? Judging from the error messages I receive, I guess it is something like the following, but I haven’t been able to get it right: my_model optimize method=laplace draws=2000 data file=data.json output file=output.csv

Thank you for your help!

Currently it is a two step process for CmdStan. CmdStanR/Py may wrap it up into one, but for now, you need to do something like:

./bernoulli optimize data file=bernoulli.data.json output file=opt_out.csv
./bernoulli laplace mode=opt_out.csv data file=bernoulli.data.json output file=laplace_out.csv

This should do the same thing that RStan does, but note that this may not be what you expect. Per some discussion in the original feature thread, you may want to pass jacobian=1 following optimize and laplace in the above commands to enable the Jacobian adjustment for constrained variables.

1 Like