Dealing with multiple closely related models

That looks like a huge model and I don’t read Python or PyMC well enough to make a lot of sense of it.

I was thinking specifically about shared and reusable model patterns. For example, I’d like to be able to write a function hierarchical_normal where I could then write

alpha = hierarchical_normal("alpha", dims=(5,), center=False)

rather than

mu_alpha = pm.Normal("mu_alpha", mu=0, sigma = 4)
sigma_alpha = pm.LogNormal("sigma_alpha", mu=0, sigma=1)
alpha_std = pm.Normal(mu=0, sigma=1, dims=(5,))
alpha = mu_alpha + sigma_alpha * alpha_std

Please forgive any PyMC or Python errors here—I hope the intent is clear. I’m not exactly sure how to write the function, but I’m imagining something like this:

def hierarchical_normal(name, dims=(1,), center=False):
    if not center:
        mu = pm.Normal("mu_alpha", mu=0, sigma = 4)
        sigma = pm.LogNormal("sigma_alpha", mu=0, sigma=1)
        alpha_std = pm.Normal("name" + "_std", mu=0, sigma=1, dims=(5,))
        alpha = mu_alpha + sigma_alpha * alpha_std
    else:
        mu = pm.Normal("mu_" + name, mu=0, sigma = 4)
        sigma = pm.LogNormal("sigma_" + name, mu=0, sigma=1)
        alpha = pm.Normal(name, mu=mu_population sigma="sigma_population")
    return alpha

I’m unsure what this would do to the with pm.Model() scoping.

I’m pretty sure I asked a question very much like this a year or so ago.

1 Like