(如果您可以访问Kruschke的“做贝叶斯数据分析”一书,这个问题可能更容易理解,因为我正在尝试在第493页上构建模型)
基本上我正在尝试使用此博客文章https://drive.google.com/open?id=0BxzmgRR6TC-2dUN3VEs4enZZQ1U
中的方法在PyMC3中构建此模型http://twiecki.github.io/blog/2014/03/17/bayesian-glms-3/这是我到达的代码
with pm.Model() as multi_level_model:
# Top level
mu_0 = pm.Normal('mu_0', mu=0, sd=100**2)
sigma_0 = pm.Uniform('sigma_0', lower=0, upper=100)
mu_1 = pm.Normal('mu_1', mu=0, sd=100**2)
sigma_1 = pm.Uniform('sigma_1', lower=0, upper=100)
# Middle
beta_0 = pm.Normal('beta_0', mu=mu_0, sd=sigma_0, shape=n_subj)
beta_1 = pm.Normal('beta_1', mu=mu_1, sd=sigma_1, shape=n_subj)
nu = pm.Exponential('nu', lam=1/29.) + 1
sigma = pm.Uniform('sigma', lower=0, upper=100)
# subj_idx is a vector with what subject each observation comes from
# data_hier['X'] is the predictor
mu_ij = beta_0[subj_idx] + beta_1[subj_idx]*data_hier['X'].values
# Bottom
# data_hier['Y'] is the prdicted values
obs = pm.StudentT('obs', mu = mu_ij, sd=sigma, nu=nu, observed=data_hier['Y'].values)
#start = pm.find_MAP() This seems to never finish
step1 = pm.Metropolis([sigma, sigma_0, sigma_1])
step2 = pm.NUTS([mu_0, mu_1, nu, beta_0, beta_1])
multi_level_trace = pm.sample(draws=2000, step=[step1, step2])
然而,它并不像在pystan中的相同模型构建那样收敛。 (并且运行时间更长)。关于如何改进模型的任何提示?
可在此处找到数据https://drive.google.com/file/d/0BxzmgRR6TC-2Qk5WcjhaaGJSTmM/view?usp=sharing