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

Questions on custom bijectors #234

Open vboussange opened 1 year ago

vboussange commented 1 year ago

Hey guys, I really like that you provide this package independently from Turing.jl. I am actually making intensive use of Bijectors.jl for a project that I have called PiecewiseInference.jl.

I need to define few custom bijectors, and I would like to make sure that I am doing things right. Here is one that transforms a random variable with values in $\mathbb{R}$ to a random variable with values in $[C, \infty)$. Here is my code for it.

struct AbsCap{N,C} <: Bijector{N} 
    c::C
end
(a::AbsCap)(x) = x .- a.c # transform itself, "forward"
(a::Inverse{<: AbsCap})(y) = abs.(y) .+ a.orig.c # inverse transform, "backward"
logabsdetjac(::Abs{0}, y::Real) = zero(eltype(y))

Is everything alright in this piece of code?

Thanks a lot for your help!

torfjelde commented 1 year ago

Hi!

Sorry, this went completely under my radar.

It's difficult for me to say what's "right" here as I don't know the transformation you're trying to implement.

But the transformation you've implemented is not bijective (abs maps both -1 and 1 to the same number).