Inequality constraints on linear combinations of parameters

This seems to imply that your prior on the derivatives is in conflict with your “true” values. The prior is even not that spiky - it implies that 90% of values should lie between 0.15 and 4.14. I would double check that the derivatives actually are mostly between 0.1 and 0.6 for your “true” values.

This “soft-constraint” approach is pretty general and based on user reports on this forum often works reasonably well (see e.g. Test: Soft vs Hard sum-to-zero constrain + choosing the right prior for soft constrain).

But since you already are out of the realm of generative models, you can basically add any regularization you want, assuming it is smooth, it doesn’t have to be a valid distribution. So e.g. to just avoid dydx1 < lower_threshold you should be able to do things like:

for(i in 1:N) {
  if(dydx1[i] < lower_threshold) {
      target += normal_lpdf(dydx[i] - lower_threshold | 0, regularization_sigma);
  } else {
      // Adding a constant to ensure smoothness around `lower_threshold`
      target += normal_lpdf(0 | 0, regularization_sigma);     
  }
}

where regularization_sigma handles the strength of the regularization (lower = more regularization away from values below threshold). Though I’ve not tested this in any actual model, this is just a guess that should work.

A “nicer” way to solve this is to find a parametrization of \beta that satisfies the constraints by construction. The admisible region is going to be an intersection of several half-planes (possibly a convex polygon, but could also not be finite) and I am not aware of a natural well behaved parametrization of such a space - the problem is that at the intersections of the halfplanes, the constraints are not smooth, which could hinder sampling. Though it might exist, e.g. for quadrilaterals, there is one so maybe it generalizes.

The good part is that this is a problem that you can solve outside of Stan, since the x values are fixed. In principle, given x you should be able to solve for absolute lower and upper bound on \beta_{1}, then find a piecewise linear functions that provide upper and lower bounds on \beta_{11} given \beta_1 and then find piecewise linear functions that provide upper and lower bounds on \beta_{12} given \beta_1 and \beta_{11}. Those can then be directly implemented in Stan and as long as the changes at the intersections are not that large, things could work OK. You can reduce the chance of problems by suitably scaling and rotating the parametrization so that the earlier coefficients aligns with the larger dimensions of your polygon-like shape…

Hope that helps at least a bit

1 Like