# Constrained (Ordered) Random Variable

**URL:** <https://discourse.pymc.io/t/constrained-ordered-random-variable/2359>\
**Category:** Questions\
**Created:** [December 10, 2018, 11:20pm UTC](https://discourse.pymc.io/t/constrained-ordered-random-variable/2359 "2018-12-10T23:20:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nilavro](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/nilavro/32/603_2.png) [@Nilavro](https://discourse.pymc.io/u/Nilavro)\
**Post date:** [December 10, 2018, 11:20pm UTC](https://discourse.pymc.io/t/constrained-ordered-random-variable/2359/1 "2018-12-10T23:20:11Z")

</div>

What should be the correct implementation for declaring two random variables such that x \< y ?

My equation is somewhat like this -

\mu + \gamma\_a \* (X\_a - T )^+ + \gamma\_b \* (T - X\_b)^+

where the ()^+ indicates the value is zero if negative and ensures either of **a** terms or **b** terms are selected. Here X\_a, X\_b both are uniform distributions such that X\_a \< X\_b.

I used the [Sampling uniformly in a triangular support](https://discourse.pymc.io/t/sampling-uniformly-in-a-triangular-support/765/12) example to impose the constraint and my code is as follows –

```python
with pm.Model() as model:
    
    boundedNormal = pm.Bound(pm.Normal, lower= 0)
    
    
    Ebase = boundedNormal('Eb', mu = 20 , sd = 20 )
    γ_heat = boundedNormal('γ_h', mu = 0, sd = 4)
    γ_cool = boundedNormal('γ_c', mu = 0, sd = 4)
     
    
    the_transform = Composed(pm.distributions.transforms.Interval(32,100), Ordered())
    T_setpoint = pm.Uniform('x',
                     lower = 32,
                     upper = 100,
                     shape = 2,
                     transform=the_transform,
                     testval=[68, 85])
    
    T_heat = T_setpoint[0]
    T_cool = T_setpoint[1]
    σ = pm.HalfCauchy('σ', 10)
    
    E_heat = pm.math.switch( T_heat > Td, γ_heat * (T_heat - Td ), 0)
    E_cool = pm.math.switch( Td > T_cool, γ_cool * (Td - T_cool), 0)
    
    E = pm.Normal('E', mu = Ebase + E_heat + E_cool, sd = σ, observed = E_tot)

```

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [December 11, 2018, 5:31am UTC](https://discourse.pymc.io/t/constrained-ordered-random-variable/2359/2 "2018-12-11T05:31:35Z")

</div>

Your model looks right to me.

---

<div class="post-metadata">

**Author:** ![Nilavro](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/nilavro/32/603_2.png) [@Nilavro](https://discourse.pymc.io/u/Nilavro)\
**Post date:** [December 11, 2018, 3:30pm UTC](https://discourse.pymc.io/t/constrained-ordered-random-variable/2359/3 "2018-12-11T15:30:20Z")

</div>

The problem with this section seems to be causing bad initial energy. I have to manually set the testval for different datasets to make it work.

> [@Nilavro](#):
>
> T\_setpoint = pm.Uniform(‘x’, lower = 32, upper = 100, shape = 2, transform=the\_transform, testval=[68, 85])

I want an uninformed prior such that

T\_heat = pm.Uniform (‘Th’, lower = 32, upper = 100)  
T\_cool = pm.Uniform(‘Tc’, lower = T\_heat, upper = 100)

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [December 12, 2018, 7:01am UTC](https://discourse.pymc.io/t/constrained-ordered-random-variable/2359/4 "2018-12-12T07:01:17Z")

</div>

You definitely need to set a test value when using ordered transformation, it is a current limitation. The second way you want to model is not necessarily uninformative, actually there will be bias if you sample like that, you can try simulating it in scipy.
