Monotonic categorical effect?

Hi all,

Is there a way to do something like this in pymc3?
https://paul-buerkner.github.io/brms/articles/brms_monotonic.html

Briefly, I want to model the effect of a monotonic effect of a categorical(ordinal?) variable.
As in the link above, Y is life-satisfaction and X is income bracket, for example
‘below 20k’, ‘between 20k and 40k’, ‘between 40k and 100k’ and ‘above 100k’.
I want to impose the constraint that on average a higher income bracket leads to a higher life-satisfaction.

Thank you!

PS: I couldn’t figure out how to include equations in my post :frowning:

1 Like

I have an exact port of this: https://github.com/junpenglao/Planet_Sakaar_Data_Science/blob/master/Ports/brms_monotonic_compare.ipynb

3 Likes

Thanks for making this.

Which version of pymc was this using? It doesn’t run in 5.8.2, a problem with the concatenate.

I got that notebook to run work in pymc 4.4.0

I also managed to make it work in pymc 5 by removing the aesara imports and switching

a = aesara.shared(np.zeros(1,))
with pm.Model() as fit1:
    temp_Intercept = pm.StudentT('intercept', mu=mu_t_i, sigma=sd_t, nu=df_t)
    beta = pm.Normal('bmo', mu=0., sigma=100.)
    simo_1 = pm.Dirichlet('simo_1', a=con_simo_1)
    simo = at.concatenate([a, simo_1])
    sigma = pm.HalfStudentT('sigma', sigma=sd_t, nu=df_t)
    mu = temp_Intercept + beta * at.sum(simo[Xmo], axis=1)
    obs = pm.Normal('Y', mu=mu, sigma=sigma, observed=dat['ls'].values)
    trace1 = pm.sample(return_inferencedata=True)

to:

with pm.Model() as fit1:
    a = pm.ConstantData('a', np.zeros(1,))
    temp_Intercept = pm.StudentT('intercept', mu=mu_t_i, sigma=sd_t, nu=df_t)
    beta = pm.Normal('bmo', mu=0., sigma=100.)
    simo_1 = pm.Dirichlet('simo_1', a=con_simo_1)
    simo = pm.math.concatenate([a, simo_1])
    sigma = pm.HalfStudentT('sigma', sigma=sd_t, nu=df_t)
    mu = temp_Intercept + beta * pm.math.sum(simo[Xmo], axis=1)
    obs = pm.Normal('Y', mu=mu, sigma=sigma, observed=dat['ls'].values)
    trace1 = pm.sample(return_inferencedata=True)
1 Like