Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从PyMC中的后验分布获取参数?_Python_Pymc - Fatal编程技术网

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

Python 如何从PyMC中的后验分布获取参数?,python,pymc,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 fl

我用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.)

它似乎工作正常,但我想从后验分布中提取参数。如何从
Tj
alpha
/
beta
Pi
获得后
p
。如果稍微重构一点,使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)
然后,您可以使用
.trace()
.stats()
方法检查
Ti
Pj
的边际后验分布:

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}

好的,很酷,谢谢。然后我可以用类似的方法得到alpha和beta。