Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a constraint to a PyMC3 model?

Tags:

pymc3

If we consider the following linear regression example for PyMC3:

https://www.pymc.io/projects/docs/en/v3/pymc-examples/examples/getting_started.html#A-Motivating-Example:-Linear-Regression

How would we include a constraint such as a + b1 + b2 = 1 or a^2 + b1^2 = 25?

I understand that we can use Bound to create bounds for variables, but I wasn't sure how to add a more complex constraint.

Thanks for the help!

like image 312
Burton Mendonca Avatar asked Oct 15 '25 03:10

Burton Mendonca


1 Answers

A general solution will be to use a Potential.

const = pm.Potential('const', pm.math.switch(pm.math.eq(a**2 + b1**2, 25),
                                             0,
                                             -np.inf))

A potential is an arbitrary factor that you can add to the model likelihood. In this example if the parameters satisfies you constraints you add nothing otherwise you add -inf.

For future reference you can also ask questions here

like image 90
aloctavodia Avatar answered Oct 17 '25 12:10

aloctavodia