# Parameter-dependent constraints

**URL:** <https://discourse.mc-stan.org/t/parameter-dependent-constraints/2166>\
**Category:** Modeling\
**Created:** [October 10, 2017, 6:30am UTC](https://discourse.mc-stan.org/t/parameter-dependent-constraints/2166 "2017-10-10T06:30:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![wzhong](https://avatars.discourse-cdn.com/v4/letter/w/ba8739/32.png) [@wzhong](https://discourse.mc-stan.org/u/wzhong)\
**Post date:** [October 10, 2017, 6:30am UTC](https://discourse.mc-stan.org/t/parameter-dependent-constraints/2166/1 "2017-10-10T06:30:48Z")

</div>

Hi,

I am wondering if it is possible to specify parameter constraints based on other parameters?

Suppose I have

```
parameters{
    real<lower=0, upper=1> alpha;
    real delta;
}

```

Is it possible to put a constraint on `delta`, so that it satisfies `alpha + delta` is bounded between 0 and 1?

Thanks!

---

<div class="post-metadata">

**Author:** ![groceryheist](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/groceryheist/32/1016_2.png) [@groceryheist](https://discourse.mc-stan.org/u/groceryheist)\
**Post date:** [October 10, 2017, 7:54am UTC](https://discourse.mc-stan.org/t/parameter-dependent-constraints/2166/2 "2017-10-10T07:54:48Z")

</div>

This has worked just fine for me.

`real<lower = -alpha, upper = 1-alpha> delta;`

---

<div class="post-metadata">

**Author:** ![syclik](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/syclik/32/6_2.png) [@syclik](https://discourse.mc-stan.org/u/syclik)\
**Post date:** [October 10, 2017, 1:23pm UTC](https://discourse.mc-stan.org/t/parameter-dependent-constraints/2166/3 "2017-10-10T13:23:35Z")

</div>

@groceryheist’s answer will work, but I’d usually just use a simplex:

```stan
parameters {
  simplex[2] beta;
}

```

---

<div class="post-metadata">

**Author:** ![wzhong](https://avatars.discourse-cdn.com/v4/letter/w/ba8739/32.png) [@wzhong](https://discourse.mc-stan.org/u/wzhong)\
**Post date:** [October 10, 2017, 4:11pm UTC](https://discourse.mc-stan.org/t/parameter-dependent-constraints/2166/4 "2017-10-10T16:11:58Z")

</div>

Thanks a lot for the answer. I have a follow-up question, would this work for a vector? So if I have  
`vector<lower = 0, upper = 1>[N] alpha;`  
Can I define a vector of delta with  
`vector<lower = -alpha, upper = 1-alpha>[N] delta;`  
Or should I declare each element in a loop? (Not sure if variable declaration can be done in a loop.)

Thanks!

---

<div class="post-metadata">

**Author:** ![groceryheist](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/groceryheist/32/1016_2.png) [@groceryheist](https://discourse.mc-stan.org/u/groceryheist)\
**Post date:** [October 10, 2017, 4:42pm UTC](https://discourse.mc-stan.org/t/parameter-dependent-constraints/2166/5 "2017-10-10T16:42:44Z")

</div>

I’m not exactly sure what you mean, but if you have a set of values that  
all sum to a constant you very likely want a simplex.

---

<div class="post-metadata">

**Author:** ![wzhong](https://avatars.discourse-cdn.com/v4/letter/w/ba8739/32.png) [@wzhong](https://discourse.mc-stan.org/u/wzhong)\
**Post date:** [October 10, 2017, 4:51pm UTC](https://discourse.mc-stan.org/t/parameter-dependent-constraints/2166/6 "2017-10-10T16:51:14Z")

</div>

I do not want the set of values to sum to a fixed constant.

Basically, I wanted to model the difference between alpha and alpha2, such that delta = alpha2 - alpha. Both alpha and alpha2 should be between 0 and 1 (because they are learning rates), hence I wanted to see if I can constrain delta based on alpha and alpha2. I am not modeling alpha and alpha2 seperately, because this is a within subject hierarchical model and I am modeling the difference parameter delta for each individual to be drawn from a group hyperparameter distribution.

---

<div class="post-metadata">

**Author:** ![groceryheist](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/groceryheist/32/1016_2.png) [@groceryheist](https://discourse.mc-stan.org/u/groceryheist)\
**Post date:** [October 10, 2017, 5:12pm UTC](https://discourse.mc-stan.org/t/parameter-dependent-constraints/2166/7 "2017-10-10T17:12:55Z")

</div>

You can work out what the constraint should be with basic algebra.

`d = a2 - a1 <-> d + a1 = a2 <-> a2 - d = a1`

since `0 < a1 < 1`,  
`0 < a2 - d <= 1 <-> a2 - 1 < d < a2'`

and since `0 < a2 < 1`  
`0 < d + a1 < 1 <-> -a1 < d <1 + a1`

So to satisfy both constraints we have

`max(a2-1,-a1) < d < min(a2, 1 + a1)`

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [October 14, 2017, 8:35pm UTC](https://discourse.mc-stan.org/t/parameter-dependent-constraints/2166/8 "2017-10-14T20:35:11Z")

</div>

> [@wzhong](#):
>
> would this work for a vector

Not yet, but it’s on our to-do list.

> [@wzhong](#):
>
> should I declare each element in a loop

That is not supported and not going to be supported. Scopes are all wrong to do that.

If you need varying upper and lower bounds, you have to do it yourself for now, including the Jacobian adjustment.

> [@wzhong](#):
>
> I am not modeling alpha and alpha2 seperately, because this is a within subject hierarchical model and I am modeling the difference parameter delta

Even if you’re ultimately interested in the difference, it is usually easier scientifically to model the individuals individually, then derive quantities like differences in abilities to measure.

If you have parameters `alpha` and `alpha2`, then you can just define a transformed parameter `delta = alpha2 - alpha`.

---

<div class="post-metadata">

**Author:** ![Anuj\_Mishra](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@Anuj\_Mishra](https://discourse.mc-stan.org/u/Anuj_Mishra)\
**Post date:** [November 27, 2018, 9:26am UTC](https://discourse.mc-stan.org/t/parameter-dependent-constraints/2166/9 "2018-11-27T09:26:42Z")

</div>

Is this possible now?..as you have mentioned that its in your to do list…

---

<div class="post-metadata">

**Author:** ![increasechief](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/increasechief/32/2039_2.png) [@increasechief](https://discourse.mc-stan.org/u/increasechief)\
**Post date:** [December 17, 2018, 7:44pm UTC](https://discourse.mc-stan.org/t/parameter-dependent-constraints/2166/10 "2018-12-17T19:44:54Z")

</div>

[https://github.com/stan-dev/stan/issues/2656](https://github.com/stan-dev/stan/issues/2656)
