Ensure an ordering to RVs

Hello,

My model contains arrays of stochastic univariate normal RVs and I want to constrain them to be in a monotonic order, x_0<x_1<x_2, and so on. I don’t care whether they are increasing or decreasing (so it can be x_0>x_1>x_2), they just need to be monotonic. Any idea how to code this?

Thanks!

Yes, you can pass a Ordered transformation to the init of the random variable.

Thanks! Do you have a trivial example? Will this allow a positive or negative order?

There is an example in https://docs.pymc.io/notebooks/api_quickstart.html

From my reading of the source code it looks like Ordered only allows ordering in one direction, or am I reading it incorrectly?

EDIT: My reading was correct, Ordered only allows monotonically increasing RVs. This minimal example,

import pymc3 as pm


if __name__ == '__main__':

    with pm.Model():

        ordered = pm.distributions.transforms.Ordered()
        x = pm.Normal(
            name="x",
            mu=0,
            sd=1,
            shape=2,
            transform=ordered,
            testval=[0.9, 0.1],  # wrong order
        )
        pm.sample()

yields pymc3.parallel_sampling.ParallelSamplingError: Bad initial energy.

You are right ordered transformation returns a sorted random variable. I think you would actually want it sorted as otherwise you will have multimodal that makes sampling difficult.