pymc3:具有多个obsesrved变量的分层模型

时间:2015-11-11 22:28:25

标签: bayesian mcmc pymc3

我有一个简单的分层模型,有很多个人,我从正态分布中得到了很少的样本。这些分布的均值也遵循正态分布。

import numpy as np

n_individuals = 200
points_per_individual = 10
means = np.random.normal(30, 12, n_individuals)
y = np.random.normal(means, 1, (points_per_individual, n_individuals))

我想使用PyMC3从样本中计算模型参数。

import pymc3 as pm
import matplotlib.pyplot as plt

model = pm.Model()
with model:
    model_means = pm.Normal('model_means', mu=35, sd=15)

    y_obs = pm.Normal('y_obs', mu=model_means, sd=1, shape=n_individuals, observed=y)

    trace = pm.sample(1000)

pm.traceplot(trace[100:], vars=['model_means'])
plt.show()

mcmc samples

我期待model_means的后验看起来像我原来的手段分布。但它似乎收敛于30均值的均值。如何从pymc3模型中恢复平均值的原始标准差(在我的例子中为12)?

1 个答案:

答案 0 :(得分:6)

这个问题让我苦苦挣扎于PyMC3的概念。

我需要n_individuals个观察到的随机变量来模拟yn_individual随机随机变量来建模means。这些参数还需要先验hyper_meanhyper_sigmasigmas是标准偏差y的先验。

import matplotlib.pyplot as plt

model = pm.Model()
with model:
    hyper_mean = pm.Normal('hyper_mean', mu=0, sd=100)
    hyper_sigma = pm.HalfNormal('hyper_sigma', sd=3)

    means = pm.Normal('means', mu=hyper_mean, sd=hyper_sigma, shape=n_individuals)
    sigmas = pm.HalfNormal('sigmas', sd=100)

    y = pm.Normal('y', mu=means, sd=sigmas, observed=y)

    trace = pm.sample(10000)

pm.traceplot(trace[100:], vars=['hyper_mean', 'hyper_sigma', 'means', 'sigmas'])
plt.show()

posteriors