Custom transformations for positive random variables?

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,

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

1 Like

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:

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.

1 Like

+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/ at main · pymc-devs/pymc · GitHub

1 Like

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.

1 Like

fwiw, statsmodels uses the square root transform for ML estimation of variance parameters, see this paper, page 38. I guess it is bijective as long as the parameter is constrained to be positive.

1 Like

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!