I have two codes that I think should correspond to the implementation of the same hierarchical model. However, the solutions are quite different. Does someone know why?
Thanks in advance!
Version 1
with pm.Model() as model:
# Intercept
a_pop = pm.Normal('a_pop', 100.0, 30.0)
a_pop_sigma = pm.HalfNormal('a_pop_sigma', 30.0)
a_local = pm.Normal('a_local', mu=a_pop, sigma=a_pop_sigma, shape=n_countries)
b_pop = pm.Normal('b_pop', 0.0, 10.0)
b_pop_sigma = pm.HalfNormal('b_pop_sigma', 10.0)
b_local = pm.Normal('b_local', mu=b_pop, sigma=b_pop_sigma,shape=n_countries)
sigma = pm.HalfCauchy('sigma', 50.0, shape=n_countries)
# Likelihood for each country
for i, country in enumerate(countries):
x = pm.Data(country + "x_data", xdata[country])
y = pm.Data(country + "y_data", ydata[country])
# Likelihood
country = pm.NegativeBinomial(country, (a_local[i] * np.exp(b_local[i]*x)), sigma[i], observed=y)
Version 2
with pm.Model() as model:
# Intercept
a_pop = pm.Normal('a_pop', 100.0, 30.0)
a_pop_sigma = pm.HalfNormal('a_pop_sigma', 30.0)
a_local = pm.Normal('a_local', mu=a_pop, sigma=a_pop_sigma, shape=n_countries)
b_pop = pm.Normal('b_pop', 0.0, 10.0)
b_pop_sigma = pm.HalfNormal('b_pop_sigma', 10.0)
b_local = pm.Normal('b_local', mu=b_pop, sigma=b_pop_sigma,shape=n_countries)
sigma = pm.HalfCauchy('sigma', 50., shape=n_countries)
# Uses an exponential regression
exp = a_local[countries_idx] + np.exp(b_local[countries_idx] * df.x.values)
like = pm.NegativeBinomial('like', exp , sigma[countries_idx], observed=df.y.values)