That’s a really interesting question! I like using a “functional” pattern, though you could do this with graph rewriting in Theano if you have a really specific need. Here’s an example of my approach:
def hierarchical_normal(name, model):
with model:
mean = pm.Normal(f'{name}_mean', 0, 1)
sd = pm.HalfNormal(f'{name}_sd', 1)
return model
def make_priors(model):
model = hierarchical_normal('a', model)
model = hierarchical_normal('b', model)
return model
def model_version_one():
model = pm.Model()
model = make_priors(model)
with model:
c = pm.Normal('c', model['a'] + model['b'], observed=data)
return model