Extract ELBO from VI fit

I’ve been working with cmdstanr to fit a model using variational inference. After the fitting process, I’m interested in extracting the ELBO samples, but I’ve been running into some challenges.

Here’s a brief overview of what I’ve tried:

  1. I used the variational method on my model to perform the fit.

  2. I attempted to access the ELBO directly using various methods, such as fit$elbo(), fit$output(), and looking into the metadata field, but haven’t had any luck.

I’ve also tried capturing the printed output of the fit object and parsing the ELBO value from there, but it seems that there should be a more direct or cleaner way to access the ELBO samples.

Could anyone provide guidance on how to extract the ELBO samples from a variational fit when using cmdstanr? Any help or pointers would be greatly appreciated.

Thank you in advance!

If you set the argument save_latent_dynamics = TRUE when running variational then there’s a separate CSV file that’s saved that I think contains this info (it’s an experimental feature so not fully documented at the moment):

fit <- mod$variational(..., save_latent_dynamics = TRUE)
fit$latent_dynamics_files() # gives you location of the CSV file

If I remember correctly for variational this extra file has a column of ELBO values.

2 Likes