如何使用pymc2.3.7在Python中计算边际似然?

2024-06-26 14:11:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我想计算一个给定数据集的模型的边际可能性,以便与另一个进行比较,这要归功于Bayes因子。你知道吗

我用pymc2得到了每个模型中每个参数的后分布。你知道吗

下面是原理(我用的是MCMC):

## PRIOR
myPar  = pymc.Normal( name='Parameter',  mu=0.3,    tau=1/0.2**2,     value=0.3)

## LOG LIKELIHOOD
@pymc.stochastic(observed=True)
def mesLL(myPar = myPar, value = Obs):
    loglike = 0.0
    for i in range(len(value)):
        myMean = model(myPar)
        myStd2 = sigMes**2
        loglike += pymc.normal_like(value[i], mu = myMean, tau = 1./myStd2)
    return loglike

## SAMPLER
np.random.seed(123456)
pymc.numpy.random.seed(123456)
#
ModBayes = pymc.Model([myPar,mesLL])
sampler  = pymc.MCMC(ModBayes)
sampler.use_step_method(pymc.AdaptiveMetropolis, [myPar])
sampler.sample(iter = 10000, burn = 4000, thin = 3)

现在我不知道如何实现边际可能性。你知道吗

先谢谢你。你知道吗


Tags: 模型valuerandom可能性pymcsamplermcmc边际
1条回答
网友
1楼 · 发布于 2024-06-26 14:11:22

每个采样参数的近似边缘分布是参数采样值的频率图。PyMC2缺少PyMC3(现在是ArviZ)中更完整的绘图工具,但是您可以简单地使用matplotlib(类似于the example in the docs中所做的)。在这种情况下

from matplotlib.pyplot import hist

hist(ModBayes.trace('myPar')[:], density=True)
hist(ModBayes.trace('mesLL')[:], density=True)

相关问题 更多 >