# Custom transformations for positive random variables?

**URL:** <https://discourse.pymc.io/t/custom-transformations-for-positive-random-variables/10248>\
**Category:** version agnostic\
**Created:** [August 28, 2022, 12:06pm UTC](https://discourse.pymc.io/t/custom-transformations-for-positive-random-variables/10248 "2022-08-28T12:06:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Ingram](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/martin_ingram/32/3535_2.png) [@Martin\_Ingram](https://discourse.pymc.io/u/Martin_Ingram)\
**Post date:** [August 28, 2022, 12:06pm UTC](https://discourse.pymc.io/t/custom-transformations-for-positive-random-variables/10248/1 "2022-08-28T12:06:20Z")

</div>

Hi all,

I’m using the JAX support in PyMC and I have a question. I believe that by default, PyMC transforms positive random variables to the full real line by using the logarithm. I’d be interested whether it’s possible to use a different transformation instead, like maybe a square root (though that might involve some Jacobian shenanigans). Is there easy support for doing this, or would it be a bit tricky?

Thanks for your help,  
All the best,  
Martin

---

<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:** [August 28, 2022, 1:50pm UTC](https://discourse.pymc.io/t/custom-transformations-for-positive-random-variables/10248/2 "2022-08-28T13:50:41Z")

</div>

Other transformations can be done, but why a square root transform?

---

<div class="post-metadata">

**Author:** ![Martin\_Ingram](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/martin_ingram/32/3535_2.png) [@Martin\_Ingram](https://discourse.pymc.io/u/Martin_Ingram)\
**Post date:** [August 28, 2022, 2:16pm UTC](https://discourse.pymc.io/t/custom-transformations-for-positive-random-variables/10248/3 "2022-08-28T14:16:54Z")

</div>

Thanks Ricardo! The reason is that I think a particular variational approximation might be more reasonable for the square root transformation than for the log transformation. There’s a discussion on Section 3.3 here for example of why different transformations might be worth exploring: [https://arxiv.org/pdf/1603.00788.pdf](https://arxiv.org/pdf/1603.00788.pdf)

---

<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:** [August 28, 2022, 3:04pm UTC](https://discourse.pymc.io/t/custom-transformations-for-positive-random-variables/10248/4 "2022-08-28T15:04:12Z")

</div>

The square root is not a 1-1 mapping, not sure what problems that could pose. I assume you want to sample in the whole real line and then take the square to go to constrained space? Sounds like things could get messy around zero due to the symmetry.

---

<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:** [August 28, 2022, 4:34pm UTC](https://discourse.pymc.io/t/custom-transformations-for-positive-random-variables/10248/5 "2022-08-28T16:34:27Z")

</div>

+1 that a none bijective transformation should not be used.

In general, you can supply custom transformation by supplying it to `transform` kwarg:  
`pm.Uniform('a', ..., transform=...)`, the transformation PyMC currently provides are in [pymc/transforms.py at main · pymc-devs/pymc · GitHub](https://github.com/pymc-devs/pymc/blob/main/pymc/distributions/transforms.py)

---

<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:** [August 28, 2022, 4:36pm UTC](https://discourse.pymc.io/t/custom-transformations-for-positive-random-variables/10248/6 "2022-08-28T16:36:18Z")

</div>

A very good alternative for transforming positive random variables is softplus, which is almost linear for value \> 10. it is excellent for modeling RV with large value.

---

<div class="post-metadata">

**Author:** ![jessegrabowski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jessegrabowski/32/5010_2.png) [@jessegrabowski](https://discourse.pymc.io/u/jessegrabowski)\
**Post date:** [August 28, 2022, 5:04pm UTC](https://discourse.pymc.io/t/custom-transformations-for-positive-random-variables/10248/7 "2022-08-28T17:04:18Z")

</div>

fwiw, statsmodels uses the square root transform for ML estimation of variance parameters, see [this paper](http://www.chadfulton.com/files/fulton_statsmodels_2017_v1.pdf), page 38. I guess it is bijective as long as the parameter is constrained to be positive.

---

<div class="post-metadata">

**Author:** ![Martin\_Ingram](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/martin_ingram/32/3535_2.png) [@Martin\_Ingram](https://discourse.pymc.io/u/Martin_Ingram)\
**Post date:** [August 28, 2022, 5:42pm UTC](https://discourse.pymc.io/t/custom-transformations-for-positive-random-variables/10248/8 "2022-08-28T17:42:22Z")

</div>

Thanks everyone. @junpenglao That’s great that you can just pass it as a `kwarg`, terrific. I’ve marked that as the solution, thanks a lot!

I’m interested in modelling something that might have a lot of density around zero, so I thought of the square root as a possible option. But I do also see the possible problems of it not being bijective. I’ll have to think a bit!
