# Non linear constraints

Hi all, my name is Alejandro. I would like to know if there is a possibility to adjust a Bayesian model with a set of parameters constrained by a non-linear equation. For example, I have this set of parameters

parameters {
real beta0;
real beta1;
real q;
real beta2;
real<lower=0> alpha1;
}


and I want q and \alpha to be restricted by:

\frac{1}{q^{1/ \alpha}}-e^{-f(\alpha|q)}=1

for a specific function f.

2 Likes

Hi Alejandro,

For this model, I believe you don’t need to specify both \alpha and q as parameters with constraints. I think you should be able to specify one of them as a parameter, and then solve for the other in the transformed parameters block.

In other words, given the function:

\frac{1}{q^{1/a}} - e^{-f(a|q)} = 1

If you re-arrange/solve the function to give the value of either a or q, given the other, you can use this expression in your stan model. As a simpler example, let’s say your constraint was:

\frac{1}{q^{1/a}} - e^{-a} = 1

You can rearrange this (courtesy of Wolfram Alpha) to:

q = (e^{-a} +1)^{-a}

Then your Stan code would specify a as a parameter and q and as a transformed parameter given by this function:

parameters {
real a;
}

transformed parameters {
real q = (exp(-a) + 1)^(-a);
}


If you can’t analytically derive this for your given function f, Stan has an algebraic solver available that could do it for you. Some examples on how to use it are in the User’s Guide: https://mc-stan.org/docs/2_25/stan-users-guide/algebra-solver-chapter.html

@nhuurre You always have great insights on these kinds of constraints, does this sound like the best approach to you?

3 Likes

Yes, algebraic solver seems to be the way to go. Whether you put alpha or q in the transformed parameters depends on the prior you want.

3 Likes