Is there an efficient way to get all of my linear models parameters distributions mean? I.e. analogue to sklearn's .coef_?

Hi, I’m new to pymc3 but I’ve done a lot of tutorials so far. I’ve built a model with 21 parameters from 20 normal and one uniform distribution. I would like to get the posterior means of these variables after I’ve done the Nuts Sampling.
I now I can gen get it with accessing them each one by one through model.parameter_x. Which seems to give me the fitted mu and sigma.
Is there a way I can all get them at once? I thought pm.summary() should do it, but it seems like that module is not there anymore?

Thanks so much for helping, I’ve been searching the docs for 3,4 hours and I couldn’t quite find the answer.

The summary function got moved to Arviz. You can still use it from there - see the code below.

import pymc3 as pm
import arviz as az

with pm.Model() as model:
    x = pm.Normal('x')
    y = pm.Deterministic('y', x**2)
    trace = pm.sample()
    
az.summary(trace)
1 Like

To add some more context to @ckrapu answer, take a look at Did something happen to plot_posterior?, TL;DR updating to the latest pymc3 version will have the alias like pm.summary back. They have not been present in the documentation for a while though, they only have links to the respective ArviZ docs: Stats — PyMC3 3.10.0 documentation.

To get the most out of ArviZ functions, you should convert your trace to inferencedata. The best way to do that is directly from pm.sample(return_inferencedata=True) which will become the default in pymc3 >=4.0

Also, summary will compute the means of all the variables in your trace, but not only that, it will also calculate rhat, ess and other quantities. With 20 parameters it won’t really matter, but for a large number of parameters, if you only want the means, it would be better to compute those with idata.posterior.mean(dim=("chain", "draw")). Or follow the guidance on arviz.summary — ArviZ dev documentation to have summary calculate only a subset of the columns.

2 Likes

Thank you, after some tinkering I remembered that the plotting moved and I also found this one there. Thanks a lot though it was exactly what I was looking for, along with the find_MAP function I believe

Thank you so much! It’s been a bit slow aleady I will try the mean approach you provided.
Is idata part of Arviz or of pymc3?
Thanks also for the tip of return_inferencedata = True

I’m just getting started but I’m having a lot of fun with the pymc3 package

1 Like

InferenceData is part of ArviZ, and the converter from pymc3 to inferencedata is also part of ArviZ currently, but it is being moved to pymc3 for better compatibility when it becomes the default in v4: Port InferenceData conversion code to pymc3 codebase by OriolAbril · Pull Request #4489 · pymc-devs/pymc3 · GitHub

1 Like