TuringLang / Bijectors.jl

Implementation of normalising flows and constrained random variable transformations
https://turinglang.org/Bijectors.jl/
MIT License
199 stars 32 forks source link

Adding bijectors for OrderStatistic and JointOrderStatistics #258

Open sethaxen opened 1 year ago

sethaxen commented 1 year ago

https://github.com/JuliaStats/Distributions.jl/pull/1668 added distributions for sample order statistics" OrderStatistic and JointOrderStatistics.

OrderStatistic has the same constraints as the distribution it wraps and should get the same bijector.

bijector(d::OrderStatistic) = bijector(d.dist)

The support of JointOrderStatistics is the sorted vectors whose elements are in the support of the wrapped distribution. An example bijector could be:

function bijector(d::JointOrderStatistics)
    dist = d.dist
    binner = bijector(dist)
    bunsort = inverse(OrderedBijector())
    # assume all upper bounded distributions have monotonically decreasing bijectors.
    scale = (binner isa Union{typeof(identity),Truncated} || islowerbounded(dist)) ? 1 : -1
    return bunsort ∘ Scale(scale) ∘ binner
end

To set scale correctly, we need to know if the bijector being used is monotonically increasing or decreasing. While we can hardcode a check for all bijectors defined in this package, to support user-implemented bijectors, we would seem to need an isdecreasing(::Bijector) API function.

Note that with a correct bijector defined for JointOrderStatistics, in a PPL like Turing, a user could use JointOrderStatistics(Exponential(), 100) to infer a sorted vector of parameters from the same distribution, instead of Bijectors.ordered(filldist(Exponential(), 100)), which as noted in https://github.com/TuringLang/Bijectors.jl/issues/220#issuecomment-1409115410 does the wrong thing (and now raises an error).

torfjelde commented 1 year ago

I'm personally happy to include a isdecreasing function or something similar. This will also be useful for some additional functionality, e.g. support for univariate distributions.