Bijectors.jl
Bijectors.jl copied to clipboard
Add support for distributions with monotonically increasing `bijector`
Related: https://github.com/TuringLang/Bijectors.jl/issues/220 and https://github.com/TuringLang/Bijectors.jl/issues/295
@sethaxen maybe you want to have a look at this
Looks good to me, but maybe we want to wait for @sethaxen
Is there a reason this PR doesn't also add is_monotonically_decreasing?
I did consider this, but AFAIK the only monotonically decreasing bijectors we have right now is Scale
with negative coefficients which will require runtime checks and thus made me hesitant (was going to raise an issue about this).
But it's probably worth it, so I'll add that too :+1:
Do we have any way to statically detect if a bijector is univariate?
Not at the moment, no.
All univariate bijections are strictly monotonic. So we could define
And because we don't, I'd prefer to make it all explicit so we end up with a method error / always return false
instead of silently doing something strange.
Ah okay so now I remember another reason why I was holding back on is_monotonically_decreasing
: AFAIK Scale
is the only monotonically decreasing function we can have, but how do we implement is_monotonically_decreasing
for ComposedFunction
?
The condition
is_monotonically_decreasing(f.inner) && is_monotonically_decreasing(f.outer)
won't be correct, e.g. Scale(-1)
and Scale(-1)
are both monotonically decreasing, but their composition is not.
EDIT: Though this is of course also an issue for is_monotonically_increasing
...
EDIT 2: Nvm, it all just boils down to
inner \ outer | inc | dec | other |
---|---|---|---|
inc | inc | dec | NA |
dec | dec | inc | NA |
other | NA | NA | NA |
I don't understand the table, but I believe it amounts to first checking that all bijectors are (elementwise) univariate with all(x -> is_monotonically_increasing(x) | is_monotonically_decreasing(x), bijectors)
and then checking that there are an odd number of decreasing bijectors with mapreduce(is_monotonically_decreasing, xor, bijectors)
.
My table is conveying the same idea, just on a per-composition-basis (since we're defining the method for ComposedFunction
) :)
But I've now added support for monotonically decreasing functions too + tests:)
I was trying to replicate a Markov Switching GARCH model and ran into wanting ordered
for a positively-constrained distriibutions. Hence, I sort of need this PR :grimacing: Any chance we could get it through?
@yebai @devmotion @sethaxen I believe this should be good to go
EDIT: Note the error in the tests has no relevance to this PR, so should probably just merge it. Though currently looking at whether this is reproducible.
I will merge this at the end of the day unless anyone else has any objections @devmotion @sethaxen :)
@torfjelde I haven't had a chance to check this directly myself, but does this do the right thing for products of heterogenous univariate distributions? It would be nice if a few cases were numerically checked via MC sampling. E.g. you could get exact MC draws with rejection sampling and then compare to MCMC draws and check expectations are similar using the MCSE.
Yeah was thinking the same; will do that :+1: