How to add constraints to Stan model

No, you can’t specify constraints in this way. To specify these relatively exotic constraints, you need to come up with a continuously differentiable one-to-one mapping between some parameters you declare and the set of simplexes that satisfy your constraint, and then implement this tranformation in either the transformed parameters or model blocks, and include an appropriate Jacobian adjustment as part of your prior specification.

For a simplex w such that w[2] < w[4] < w[6] and w[3] > w[5] > w[7], one solution would be to create an unordered 3-simplex S whose elements represent w[1], w[2] + w[4] + w[6], and w[3] + w[5] + w[7], and then two ordered 3-simplexes T and U, and then let w[1] = S[1], w[6,4,2] = S[2] * T, and w[3, 4, 5] = S[3] * U.

The Jacobians here will all be readily computable.

Edit: see here (particularly posts downthread) for details of how to construct ordered simplexes Ordered simplex constraint transform - #18 by martinmodrak

1 Like