# Switch function has little constraint in sampling

**URL:** <https://discourse.pymc.io/t/switch-function-has-little-constraint-in-sampling/11684>\
**Category:** v3\
**Tags:** theano, modeling\
**Created:** [March 23, 2023, 2:20pm UTC](https://discourse.pymc.io/t/switch-function-has-little-constraint-in-sampling/11684 "2023-03-23T14:20:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SHCC](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@SHCC](https://discourse.pymc.io/u/SHCC)\
**Post date:** [March 23, 2023, 2:20pm UTC](https://discourse.pymc.io/t/switch-function-has-little-constraint-in-sampling/11684/1 "2023-03-23T14:20:48Z")

</div>

Hi there `:)`  
As the title says, I’m looking for a way to build a model where the variables are in same ranges with different values, however the result sampling suggest the sampling points are away from the switch constraints.  
So, what do you think about this?

```python
    with pm.Model() as model:
        rs = pm.Normal('rs', 800, 50)
        rp = pm.Normal('rp', 800, 50)
        re = pm.Normal('re', 800, 50)
        pm.Potential('const1', tt.switch(tt.gt(rp, rs), 0., -1E10))
        pm.Potential('const2', tt.switch(tt.gt(re, rp), 0., -1E10))
        rhop = pm.Uniform('rhop', 1, 5)
        rhoe = pm.Uniform('rhoe', 1, 5)
        pm.Potential('const3', tt.switch(tt.gt(rhop, rhoe), 0., -1E10))
        aa = pm.Deterministic('aa', np.sqrt(-np.log(rhoe / rhop)))
        trace = pm.sample(1000)
    data = az.from_pymc3(trace)
    pm.summary(data, hdi_prob=0.95)

```

```auto
         mean sd hdi_2.5% hdi_97.5% ... mcse_sd ess_bulk ess_tail r_hat
rs 793.549 48.173 698.735 888.375 ... 9.795 13.0 283.0 1.13
rp 803.784 49.538 710.618 904.680 ... 2.813 157.0 294.0 1.02
re 805.033 41.822 727.193 890.052 ... 2.876 105.0 188.0 1.00
rhop 2.821 1.165 1.000 4.724 ... 0.154 32.0 126.0 1.06
rhoe 3.064 1.203 1.165 4.995 ... 0.140 38.0 109.0 1.05
aa NaN NaN 0.027 NaN ... NaN NaN NaN NaN

```

---

<div class="post-metadata">

**Author:** ![cluhmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cluhmann/32/3083_2.png) [@cluhmann](https://discourse.pymc.io/u/cluhmann)\
**Post date:** [March 23, 2023, 6:28pm UTC](https://discourse.pymc.io/t/switch-function-has-little-constraint-in-sampling/11684/2 "2023-03-23T18:28:55Z")

</div>

What exactly do you expect to see from this model that you are not currently seeing?

---

<div class="post-metadata">

**Author:** ![cluhmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cluhmann/32/3083_2.png) [@cluhmann](https://discourse.pymc.io/u/cluhmann)\
**Post date:** [March 23, 2023, 6:44pm UTC](https://discourse.pymc.io/t/switch-function-has-little-constraint-in-sampling/11684/3 "2023-03-23T18:44:15Z")

</div>

![image](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/d/d1307f188fff9f3cdc90a9047d2e840d48adb588.png)

---

<div class="post-metadata">

**Author:** ![SHCC](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@SHCC](https://discourse.pymc.io/u/SHCC)\
**Post date:** [March 24, 2023, 5:31am UTC](https://discourse.pymc.io/t/switch-function-has-little-constraint-in-sampling/11684/4 "2023-03-24T05:31:46Z")

</div>

Thank you for your quickly answer. I want to transfer aa to another function. however the prior sampling points show Nan. do you have any idea about it?

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [March 24, 2023, 5:59am UTC](https://discourse.pymc.io/t/switch-function-has-little-constraint-in-sampling/11684/5 "2023-03-24T05:59:27Z")

</div>

Potentials don’t play a role in prior or posterior predictive sampling since they are only part of the logp graph, not the random one.

---

<div class="post-metadata">

**Author:** ![SHCC](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@SHCC](https://discourse.pymc.io/u/SHCC)\
**Post date:** [March 24, 2023, 6:43am UTC](https://discourse.pymc.io/t/switch-function-has-little-constraint-in-sampling/11684/6 "2023-03-24T06:43:42Z")

</div>

So if I want to transfer aa to a pm.Simulator module, how to add the constraints properly to make sure the aa is always float not nan?

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [March 24, 2023, 7:05am UTC](https://discourse.pymc.io/t/switch-function-has-little-constraint-in-sampling/11684/7 "2023-03-24T07:05:52Z")

</div>

You can try to parametrize your model in a way that it respects the constraints naturally. For instance, instead of modelling `rp` and `re` directly, you can model the shift from the previous variable.

```python
with pm.Model():
  rs = pm.Normal('rs', 800, 50)
  rp_shift = pm.LogNormal('rp_shift', 0, 1)
  re_shift = pm.Lognormal('re_shift', 0, 1)
  rp = rs + rp_shift
  re = rs + rp_shift + re_shift

```

The priors will be trickier to reason about, but it isn’t too bad. You can wrap `rp` and `re` in `pm.Deterministic` if you need them in the trace.

---

<div class="post-metadata">

**Author:** ![SHCC](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@SHCC](https://discourse.pymc.io/u/SHCC)\
**Post date:** [March 24, 2023, 8:15am UTC](https://discourse.pymc.io/t/switch-function-has-little-constraint-in-sampling/11684/8 "2023-03-24T08:15:22Z")

</div>

Thanks! I will try it.
