如何从PyMC的后验分布中获取参数?

时间:2014-08-14 18:17:13

标签: python pymc

我有以下用PyMC编写的程序:

import pymc
from pymc.Matplot import plot as mcplot

def testit( passed, test_p = 0.8, alpha = 5, beta = 2):
    Pi = pymc.Beta( 'Pi', alpha=alpha, beta=beta)
    Tj = pymc.Bernoulli( 'Tj', p=test_p)

    @pymc.deterministic
    def flipper( Pi=Pi, Tj=Tj):
            return Pi if Tj else (1-Pi)
            # Pij = Pi if Tj else (1-Pi)
            # return pymc.Bernoulli( 'Rij', Pij)

    Rij = pymc.Bernoulli( 'Rij', p=flipper, value=passed, observed=True)

    model = pymc.MCMC( [ Pi, Tj, flipper, Rij])
    model.sample(iter=10000, burn=1000, thin=10)

    mcplot(model)

testit( 1.)

它似乎工作正常,但我想从后验分布中提取参数。如何从p获取来自Tjalpha / beta的后验Pi

1 个答案:

答案 0 :(得分:1)

你非常接近。如果你重构一点,以便在函数之外有Pi和Tj对象,你可以直接从(近似)后验分布中访问MCMC样本:

import pymc

def testit(passed, test_p = 0.8, alpha = 5, beta = 2):
    Pi = pymc.Beta( 'Pi', alpha=alpha, beta=beta)
    Tj = pymc.Bernoulli( 'Tj', p=test_p)

    @pymc.deterministic
    def flipper( Pi=Pi, Tj=Tj):
            return Pi if Tj else (1-Pi)
            # Pij = Pi if Tj else (1-Pi)
            # return pymc.Bernoulli( 'Rij', Pij)

    Rij = pymc.Bernoulli( 'Rij', p=flipper, value=passed, observed=True)

    return locals()

vars = testit(1.)
model = pymc.MCMC(vars)
model.sample(iter=10000, burn=1000, thin=10)

然后,您可以使用TiPj方法检查.trace().stats()的边际后验分布:

In [12]: model.Pi.stats()
Out[12]:
{'95% HPD interval': array([ 0.43942434,  0.9910729 ]),
 'mc error': 0.0054870077893956213,
 'mean': 0.7277823553617826,
 'n': 900,
 'quantiles': {2.5: 0.3853555534589701,
  25: 0.62928387568176036,
  50: 0.7453244339604943,
  75: 0.84835518829619661,
  97.5: 0.95826093368693854},
 'standard deviation': 0.15315966296243455}
In [13]: model.Tj.stats()
Out[13]:
{'95% HPD interval': array([ 0.,  1.]),
 'mc error': 0.011249691353790801,
 'mean': 0.89666666666666661,
 'n': 900,
 'quantiles': {2.5: 0.0, 25: 1.0, 50: 1.0, 75: 1.0, 97.5: 1.0},
 'standard deviation': 0.30439375084839554}