I am trying to model a bookish example of Hierarchical Bayesian Modelling in PyMC3. The code for the openbugs is given for reference along with the figure for overall concept.
The pymc3 construction is kind of reverse, I have a bit confusion on the
-loops (how to model i , j in the pymc3-system)
-implementations of the priors (with loops)
My first attempt is as following.
######## Try 1 #################
with pm.Model() as model:
# Hyperpriors
m_grnt=pm.HalfNormal('m_grnt ‘,1.0E–8)
tau_m_grnt=pm.Gamma(‘tau_m_grnt’,1.0E–8,1.0E–8)
sc_grnt=pm.HalfNormal(’ sc _grnt ',1.0E–8)
tau_sc_grnt=pm.Gamma(‘tau_m_grnt’,1.0E–8,1.0E–8)
stdev_m_grnt=1/np.sqrt(tau_m_grnt)
stdev_sc_grnt=1/ np.sqrt(tau_sc_grnt)
########DONT understand this part#################
#priors “At level J”
m = pm.HalfNormal('m', m_grnt, tau_m_grnt)
sc = pm.HalfNormal('sc ', sc_grnt, tau_sc_grnt)
tau= pm.Gamma(‘tau_m_grnt’,1.0E–8,1.0E–8)
stdev=1/np.sqrt(tau)
s=1
a=0·5
########DONT understand this part#################
mu(i,grp_no(i))=s3(i)+sc(grp_no(i))*((m(grp_no(i))/sc(grp_no(i))*s3(i)+s(grp_no(i)))^a(grp_no(i)))
#Likelihood
S1 = pm.Normal("y", mu=mu, tau=tau, observed=data, dims="data_id")
with model:
model_hier = pm.sample(1000,return_inferencedata=True,chains=1, )
#summary trace
summary_pt_3=az.summary(model_hier)