Doc about distributions.transforms

Hi,

I’ve been looking for the doc about pymc3.distributions.transforms.
There is an example in the api_qickstart page, but I could not find anything else. Is there something that I missed or does it remain to be done?

I am particularly interested by the transform that can be used to impose an ordered constraint on two variable, as x_1<x_2. I looked at the source code but I recognize that I didn’t understand it!:sweat_smile:

Thanks a lot

Hi Timothé!
Usually, you can use a potential to enforce a constraint. In your case, it would go like:

    import theano.tensor as tt

    means = pm.Normal("means", mu=np.array([0.9, 1]), sd=1, shape=2)
    pm.Potential("order_means", tt.switch(means[1] - means[0] < 0, -np.inf, 0))

When \mu_1 < \mu_0, the model adds - \infty to the likelihood and will ignore the combinations of parameters that verify this condition. Otherwise, the model lives its life as usual.
This solution with pm.Potential is very flexible, but the ordered constraint is so common that you can also enforce it directly with:

    means = pm.Normal('means', mu=np.array([0.9, 1]), sd=1, shape=2, 
                      transform=pm.distributions.transforms.ordered)

These examples come from Osvaldo Martin’s very good book and notebooks.
Hope this helps and answers your question :vulcan_salute:

1 Like

Thanks for the answer and the link to Osvaldo Martin’s notebook! It helps a lot!
And what about the documentation about pymc3.distributions.transforms?

I don’t really know about that, but the ressource I gave you should get you started.
Maybe on the website? Otherwise, I think they would welcome a PR