I’m new to PyMC3 and have previously been using JAGS. To convert over to PyMC3 I wanted to convert a previous JAGS script of mine, but have been having many difficulties… here is a simplified diagram of the hierarchical model I want.

The 50 refers to 50 people, and the 14,000 refers to the total number of experimental trials (summing across people).

Here is my working JAGS model:

```
model{
for(i in ntrials) { # for every trial...
x[i] ~ dvonmises(mn[i],60) # x is the observed data, trying to approximate it using VonMises distribution
mn[i] <- ifelse(z[i]==1, m[i,1]+mushift[subj[i]], m[i,2])
z[i] ~ dbern(pt[subj[i]])
}
for (j in nsubj) { # for every subject...
pt[j] ~ dbeta(CmA, CmB)
mushift[j] ~ dnorm(Cshift,Ctau)
}
# Hyper-priors
CmA ~ dexp(.1)
CmB ~ dexp(.1)
Cshift ~ dnorm(0,.5)
Ctau ~ dgamma(2,.01)
}
```

The subj variable is a vector of length 14,000 that refers to which of the 50 people a certain trial came from (e.g., subj[1000] = 42 means the 1000th cell of vector subj came from person #42).

The difficulty I have is with the trial-based for loop at the top. I want the mu parameter of the VonMises distribution to change depending on whether z is 0 or 1. Here is my attempt so far to code it using PyMC3:

```
with pm.Model() as hierarchical_model:
# Hyper-priors
Cshift = pm.Normal('Cshift',0,.5)
Ctau = pm.Gamma('Ctau', 2, .01)
CmA = pm.Exponential('CmA',.1)
CmB = pm.Exponential('CmB',.1)
# Subject-level
pt = pm.Beta('pt', alpha=CmA, beta=CmB, shape=nsubj)
mushift = pm.Normal('mushift',mu=Cshift,sd=Ctau,shape=nsubj)
# Trial-level
# z = pm.Bernoulli('z', pt, shape=ntrials)
# mn = pm.math.switch(z, m[i,0]+mushift[subj[i]], m[i,1])
y = pm.VonMises('y', mu=mn, kappa=60, observed=x)
```

Lines are commented out where I’ve been facing problems. Can PyMC3 support this sort of hierarchical model? Does anyone have any advice or resources to point me towards? Thanks very much for any help.