PyMC: summary statistics for a subset of variables

291 views Asked by At

I'm using PyMC 2.3.4 and am trying to get summary statistics for a subset of my model variables, but can't seem to do so using the method in the docs.

Model-building code:

import pymc

a = pymc.Normal('a',0,1)
b = pymc.Normal('b',0,1)
myModel = pymc.Model((a,b))
M = pymc.MCMC(myModel)
M.sample(1000)

Per the documentation at https://pymc-devs.github.io/pymc/database.html, I should be able to run

M.a.summary() -> summary statistics for a

but instead, I get

AttributeError: 'MCMC' object has no attribute 'a'

However, M.summary() gives summary statistics for all variables.

1

There are 1 answers

0
Abraham D Flaxman On BEST ANSWER

There are perhaps too many ways to create a model in PyMC2. The one you used, passing an iterable of pymc.Node instances, does not record the names, so the model doesn't have an M.a, even though M.nodes contains a stochastic named 'a'.

If you prefer to create your model this way, you can get a summary from a directly, with

a.summary()

For me, this prints

a:

    Mean             SD               MC Error        95% HPD interval
    ------------------------------------------------------------------
    [[-0.016]]       [[ 0.992]]       [[ 0.031]]       [-1.986  1.939]


    Posterior quantiles:

    2.5             25              50              75             97.5
     |---------------|===============|===============|---------------|
    [[-2.047]]       [[-0.665]]      [[-0.058]]     [[ 0.672]]    [[ 1.937]]

I find it convenient to have the attribute M.a available sometimes, and you can get it by using a dictionary instead of a list when constructing the model:

M2 = pymc.MCMC({'a':a, 'b':b})
M2.sample(1000)
M2.a.summary()