Modelling/transferring a BUGS example to PyMC3

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)

grafik grafik

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)

At a first glance this ought to be very doable using pymc3. By convention (and sheer practicality) you’ll want to vectorise away those loops, and the best practice method for hierarchical models uses (at least?) two main concepts: partial-pooling and indexing.

This example notebook steps through the classic Radon toy dataset in detail to describe those concepts, hopefully a good place to form some ideas? GLM: Hierarchical Linear Regression — PyMC3 3.11.2 documentation