Hi! Here is the smallest reproducible code unit. I am working on the following fully conjugate normal model:
In my toy model, I am setting
and creating a toy dataset by drawing 1000 samples from a
model. I am trying to compare the true posterior density defined by my toy samples and prior to the posterior density output by stan’s variational run on this data.
I am able to get the posterior mean from the created CmdStanVB object directly, but there does not seem to be any way to get the posterior variance other than calling np.var on the posterior samples returned by the CmdStanVB object. I know that the converged posterior variance from the ADVI algorithm is likely slightly different, by randomness, than the sample variance of the posterior samples but I need the ADVI’s posterior variance value at convergence and not an estimation.
How can I get the actual ADVI posterior variance? I am eventually going to need this for other fully conjugate models and I’m concerned after learning that this was possibly removed 6 years ago.
Here is my reproducible code:
When including Stan code in your post it really helps if you make it as readable as possible by using Stan code chunks (```stan) with clear spacing and indentation. For example, use
// The input data is a vector 'y' of length 'N'.
data {
int<lower=0> N;
vector[N] y;
real<lower=0> sigma;
real mu_0;
real<lower=0> sigma_0;
}
// The parameters accepted by the model. Our model
// accepts two parameters 'mu' and 'sigma'.
parameters {
real mu;
}
// The model to be estimated. We model the output
// 'y' to be normally distributed with mean 'mu'
// and standard deviation 'sigma'.
model {
mu ~ normal(mu_0, sigma_0);
y ~ normal(mu, sigma);
}
import os
from cmdstanpy import CmdStanModel
import numpy as np
# STAN_MODEL = path_to_stan_model
# set the parameters we will work with
n = 1000
mu = 0.3
sigma = 10
mu_0 = 0
sigma_0 = 1
NP_SEED = 10
stan_model = CmdStanModel(stan_file=STAN_MODEL)
# fix np seed and draw data
np.random.seed(NP_SEED)
y = np.random.normal(
loc = mu,
scale = sigma,
size = n)
stan_data = {
'N': n,
'y': y,
'sigma': sigma,
'mu_0': mu_0,
'sigma_0': sigma_0
}
vb_fit = stan_model.variational(
data = stan_data,
)
# vb_fit.mu returns the posterior mean, but nothing returns the posterior variance.
One of the reasons I’m choosing to use variational inference is that it directly gives me the density of the posterior rather than samples from MCMC. I fear, however, that cmdstanpy’s variational inference implementation is not going to work in that regard. Is there any way to get the ADVI posterior variance other than calling np.var on the posterior samples?
Thanks in advance!