# Modelling/transferring a BUGS example to PyMC3

**URL:** <https://discourse.pymc.io/t/modelling-transferring-a-bugs-example-to-pymc3/7140>\
**Category:** Questions\
**Created:** [April 1, 2021, 10:09am UTC](https://discourse.pymc.io/t/modelling-transferring-a-bugs-example-to-pymc3/7140 "2021-04-01T10:09:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arsl](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@Arsl](https://discourse.pymc.io/u/Arsl)\
**Post date:** [April 1, 2021, 10:09am UTC](https://discourse.pymc.io/t/modelling-transferring-a-bugs-example-to-pymc3/7140/1 "2021-04-01T10:09:25Z")

</div>

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](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/a/a40ba3b77b6581dfe24947bd821d95a19c2fb6e7.png) ![grafik](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/4/452e98c47d936d17086443a27edc2fe871f9a275.png)

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)
```

---

<div class="post-metadata">

**Author:** ![jonsedar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jonsedar/32/2590_2.png) [@jonsedar](https://discourse.pymc.io/u/jonsedar)\
**Post date:** [April 1, 2021, 3:10pm UTC](https://discourse.pymc.io/t/modelling-transferring-a-bugs-example-to-pymc3/7140/2 "2021-04-01T15:10:33Z")

</div>

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](https://docs.pymc.io/pymc-examples/examples/generalized_linear_models/GLM-hierarchical.html)
