Constrain likelihood based on covariates

Helloooooo my dear Bayesians! I have a Categorical regression, and want to force some of the outcome categories to 0 for some combinations of the covariates. What’s an efficient way to do that in the current API?

In other words, I want to force e.g p[-1] = 0 when X_a < 5 and / or X_b < 0, where:

p = softmax(a_effect * X_a + b_effect * X_b) # shape (n_obs, n_categories)

I’m pretty sure I can do it with masking by adding a very big negative bias to the corresponding cases in my dataset, but I’m thinking pm.Potential may be helpful for that? And more principled that crude masking.

Thanks a lot in advance for any pointers, and PyMCheers :vulcan_salute:

1 Like

Masking would be my suggestion. Potential doesn’t buy you anything?

I think you have to mask in this case. I’m not sure how you can intervene on a single element of the p vector by adding a scalar term to the total model logp. It would end up being a complex switch I guess. Since your condition appears to be a function of data, it would be easiest to just pre-compute a bias matrix of shape (n_rows, n_outcomes) with values 0 and -9999.0 and add that to your logit_p

Ah well look at that! I thought my solution was sub-optimal. Thanks for chiming in @ricardoV94 and @jessegrabowski :pray:

The way I do it is indeed by defining a boolean mask on the Data container, then using pm.math.switch to inject a large negative value for impossible regions, and finally applying the biased mask to the given category with pt.set_subtensor