Hierarchical model with Additional Features for Certain Groups

Hi there,

Is it possible to build a multi-level model with additional features for some of the groups and not others?

For example, in the simplest case I can think of, say I had two groups and I wanted a hierarchy on an intercept term and one feature, and I also wanted group 1 to have one additional feature.

def model(X, y, group_idx):
    b0 = pm.Normal(0,1, shape=2)
    b1 = pm.Normal(0,1,shape=2)
    b2 = pm.Normal(0,1)
    mu = b0[group_idx] + b1[group_idx]*X[0] *  ??b2?? * X[1]
    sigma = pm.HalfNormal(5)
    y = pm.Normal(mu, sigma, observed=y)

I made this code up, so apologies if any mistakes, but I think it conveys the point. If anything is unclear I would be happy to clarify, thanks!

You can use pm.math.where(pm.math.eq(group_idx, 1), b2 * X[1], 0)

1 Like

That’s pm.math, just to be clear, not the python built-in math library

1 Like

Thanks!

Do you know if these types of implementations would lead to slower sampling with NUTS?

Not sure what your concern is?

It’s fine to have variables that only affect a subset of observations. The where can be thought of as a vectorized if statement that does this. It’s pretty cheap under the hood.

1 Like