Actually, the problem is because you didnt model a vector of theta
:
with pm.Model() as model:
# Uninformative prior for alpha and beta
u = pm.Uniform('u', 0, 1)
v = pm.Uniform('v', 0, 1)
alpha = pm.Deterministic('a', u / tt.pow(v, 2))
beta = pm.Deterministic('b', (1 - u) / tt.pow(v, 2))
# theta_i with i=1,2,...,J
theta = pm.Beta('theta', alpha=alpha, beta=beta, shape=len(n_rat))
p = pm.Binomial('y', p=theta, observed=n_tumour, n=n_rat)
trace = pm.sample(1000, tune=1000)
pm.traceplot(trace);
Alternatively, following the equation (5.9) in BDA 3 you can directly specify the prior of \alpha, \beta using a DensityDist
:
import pymc3.distributions.transforms as tr
def logp_ab(value):
return tt.log(tt.pow(tt.sum(value), -5/2))
with pm.Model() as model:
# Uninformative prior for alpha and beta
ab = pm.DensityDist('ab', logp_ab, transform=tr.log,
shape=2,
testval=np.asarray([1., 1.]))
theta = pm.Beta('theta', alpha=ab[0], beta=ab[1], shape=len(n_rat))
p = pm.Binomial('y', p=theta, observed=n_tumour, n=n_rat)