How to find the mean and the variance of the normal distribution obtained using the advi method in PyMC?

Viewed 50

I am using the advi method to find the posterior distribution. How can I find the mean and the std of the normal posterior distribution that we get using the advi and not that of the samples obtained using advi in PyMC?

1 Answers

You can call approx.mean.eval(), approx.std.eval(), and then reference approx.ordering to map index-slices to parameters of Your model.

Using pymc==4.1.6, and having defined a pm.Model called model, I can do the following:

with model:
    approx = pm.fit(method="advi")
    
approx_mu = approx.mean.eval()
approx_mu_dict = {
    param: approx_mu[slice_]
    for (param, (_, slice_, _, _))
    in approx.ordering.items()}

approx_std = approx.std.eval()
approx_std_dict = {
    param: approx_std[slice_]
    for (param, (_, slice_, _, _))
    in approx.ordering.items()}
Related