Bijectors.jl icon indicating copy to clipboard operation
Bijectors.jl copied to clipboard

Add support for distributions with monotonically increasing `bijector`

Open torfjelde opened this issue 1 year ago • 11 comments

Related: https://github.com/TuringLang/Bijectors.jl/issues/220 and https://github.com/TuringLang/Bijectors.jl/issues/295

torfjelde avatar Dec 03 '23 16:12 torfjelde

@sethaxen maybe you want to have a look at this

torfjelde avatar Dec 03 '23 17:12 torfjelde

Looks good to me, but maybe we want to wait for @sethaxen

sunxd3 avatar Dec 04 '23 15:12 sunxd3

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.

torfjelde avatar Dec 10 '23 14:12 torfjelde

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

torfjelde avatar Dec 10 '23 14:12 torfjelde

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).

sethaxen avatar Dec 10 '23 17:12 sethaxen

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:)

torfjelde avatar Dec 10 '23 19:12 torfjelde

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?

torfjelde avatar Apr 19 '24 08:04 torfjelde

@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.

torfjelde avatar May 07 '24 17:05 torfjelde

I will merge this at the end of the day unless anyone else has any objections @devmotion @sethaxen :)

torfjelde avatar May 08 '24 10:05 torfjelde

@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.

sethaxen avatar May 10 '24 08:05 sethaxen

Yeah was thinking the same; will do that :+1:

torfjelde avatar May 10 '24 08:05 torfjelde