问题描述
我有一个简单的层次模型,其中包含很多个体,而我有一些来自正态分布的小样本.这些分布的均值也服从正态分布.
I have a simple hierarchical model with lots of individuals for which I have small samples from a normal distribution. The means of these distributions also follow a normal distribution.
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从样本中计算模型参数.
I want to use PyMC3 to compute the model parameters from the sample.
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()
我期望model_means
的后验像我的原始均值分布.但这似乎收敛到30
均值的均值.如何从pymc3模型中恢复均值的原始标准差(本例中为12)?
I was expecting the posterior of model_means
to look like my original distribution of means. But it seems to converge to 30
the mean of the means. How do I recover the original standard deviation of the means (12 in my example) from the pymc3 model?
推荐答案
这个问题让我在PyMC3的概念上苦苦挣扎.
This question was me struggling with the concepts of PyMC3.
我需要n_individuals
观察到的随机变量来对y
进行建模,而n_individual
随机随机变量来对means
进行建模.这些还需要先验hyper_mean
和hyper_sigma
作为其参数. sigmas
是y
的标准偏差的先验.
I need n_individuals
observed random variables to model the y
and n_individual
stochastic random variables to model the means
. These also need priors hyper_mean
and hyper_sigma
for their parameters. sigmas
is the prior for the standard deviation of 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()
这篇关于pymc3:具有多个混淆变量的层次模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!