我正在尝试使用pymc3构建分层逻辑回归,但似乎存在某种收敛或错误指定问题,因为跟踪输出仅为每个参数生成单个值,并在10秒内运行2000个样本。
这是模型,它有6组和不同的斜率/截距:
def invlogit(x):
return np.exp(x) / (1 + np.exp(x))
with Model() as logistic_reg:
# Priors
mu_a = Normal('mu_a', mu=0., sd=100)
sigma_a = Uniform('sigma_a', 0, 50)
mu_b = Normal('mu_b' , mu=0., sd=100)
sigma_b = Uniform('sigma_b', 0, 50)
mu_b_2 = Normal('mu_b_2', mu=0., sd=100)
sigma_b_2 = Uniform('sigma_b_2', 0, 50)
mu_b_3 = Normal('mu_b_3', mu=0., sd=100)
sigma_b_3 = Uniform('sigma_b_3', 0, 50)
# Random intercepts
a = Normal('a', mu=mu_a, sd=sigma_a, shape=group_n)
# Random slopes
b = Normal('b', mu=mu_b, sd=sigma_b, shape=group_n)
b2 = Normal('b2', mu=mu_b_2, sd=sigma_b_2, shape=group_n)
b3 = Normal('b3', mu=mu_b_3, sd=sigma_b_3, shape=group_n)
# Expected value
p = invlogit(a[group] + b[group] * norm_score + b2[group] * nine_dummy + b3[group] * nine_dummy * norm_score)
#Likelihood
y_obs = pm.Bernoulli('y_obs', p=p, observed=outcome)
with logistic_reg:
niter = 2000
start = pm.find_MAP()
step = pm.NUTS()
trace = pm.sample(niter, step, start, progressbar=True)
以上运行没有例外,但跟踪显示了为每个参数采集的2k样本中不变的值。
然而,当我运行没有分层先验的相同模型时,它似乎按预期运行非池化模型,没有任何问题:
def invlogit(x):
return np.exp(x) / (1 + np.exp(x))
with Model() as logistic_reg:
# Random intercepts
a = Normal('a', mu=0, sd=100, shape=group_n)
# Random slopes
b = Normal('b', mu=0, sd=100, shape=group_n)
b2 = Normal('b2', mu=0, sd=100, shape=group_n)
b3 = Normal('b3', mu=0, sd=100, shape=group_n)
# Expected value
p = invlogit(a[group] + b[group] * norm_score + b2[group] * nine_dummy + b3[group] * nine_dummy * norm_score)
#Likelihood
y_obs = pm.Bernoulli('y_obs', p=p, observed=outcome)
with logistic_reg:
niter = 2000
start = pm.find_MAP()
step = pm.NUTS()
trace = pm.sample(niter, step, start, progressbar=True)
鉴于非合并模型似乎工作正常,我确信我在某种程度上错误地指定了我的先验部分合并的模型,但我无法弄清楚如何。任何提示将不胜感激!