Bijectors.jl
This package implements a set of functions for transforming constrained random variables (e.g. simplexes, intervals) to Euclidean space. The 3 main functions implemented in this package are the link
, invlink
and logpdf_with_trans
for a number of distributions.
Bijectors.link
— Functionlink(d::Distribution, x)
Transforms the input x
using the constrained-to-unconstrained bijector for distribution d
.
See also: invlink
.
Example
julia> using Bijectors
julia> d = LogNormal() # support is (0, Inf)
LogNormal{Float64}(μ=0.0, σ=1.0)
julia> b = bijector(d) # log function transforms to unconstrained space
(::Base.Fix1{typeof(broadcast), typeof(log)}) (generic function with 1 method)
julia> b(1.0)
0.0
julia> link(LogNormal(), 1.0)
0.0
Bijectors.invlink
— Functioninvlink(d::Distribution, y)
Performs the inverse transform on a value y
that was transformed using the constrained-to-unconstrained bijector for distribution d
.
It should hold that invlink(d, link(d, x)) = x
.
See also: link
.
Example
julia> using Bijectors
julia> d = LogNormal() # support is (0, Inf)
LogNormal{Float64}(μ=0.0, σ=1.0)
julia> link(LogNormal(), 1.0) # uses a log transform
0.0
julia> invlink(LogNormal(), 0.0)
1.0
Bijectors.logpdf_with_trans
— Functionlogpdf_with_trans(d::Distribution, x, transform::Bool)
If transform
is false
, logpdf_with_trans
calculates the log probability density function (logpdf) of distribution d
at x
.
If transform
is true
, x
is transformed using the constrained-to-unconstrained bijector for distribution d
, and then the logpdf of the resulting value is calculated with respect to the unconstrained (transformed) distribution. Equivalently, if x
is distributed according to d
and y = link(d, x)
is distributed according to td = transformed(d)
, then logpdf_with_trans(d, x, true) = logpdf(td, y)
. This is accomplished by subtracting the log Jacobian of the transformation.
Example
julia> using Bijectors
julia> logpdf_with_trans(LogNormal(), ℯ, false)
-2.4189385332046727
julia> logpdf(LogNormal(), ℯ) # Same as above
-2.4189385332046727
julia> logpdf_with_trans(LogNormal(), ℯ, true)
-1.4189385332046727
julia> # If x ~ LogNormal(), then log(x) ~ Normal()
logpdf(Normal(), 1.0)
-1.4189385332046727
julia> # The difference between the two is due to the Jacobian
logabsdetjac(bijector(LogNormal()), ℯ)
-1
The distributions supported are:
RealDistribution
:Union{Cauchy, Gumbel, Laplace, Logistic, NoncentralT, Normal, NormalCanon, TDist}
,PositiveDistribution
:Union{BetaPrime, Chi, Chisq, Erlang, Exponential, FDist, Frechet, Gamma, InverseGamma, InverseGaussian, Kolmogorov, LogNormal, NoncentralChisq, NoncentralF, Rayleigh, Weibull}
,UnitDistribution
:Union{Beta, KSOneSided, NoncentralBeta}
,SimplexDistribution
:Union{Dirichlet}
,PDMatDistribution
:Union{InverseWishart, Wishart}
, andTransformDistribution
:Union{T, Truncated{T}} where T<:ContinuousUnivariateDistribution
.
All exported names from the Distributions.jl package are reexported from Bijectors
.
Bijectors.jl also provides a nice interface for working with these maps: composition, inversion, etc. The following table lists mathematical operations for a bijector and the corresponding code in Bijectors.jl.
Operation | Method | Automatic |
---|---|---|
b ↦ b⁻¹ | inverse(b) | ✓ |
(b₁, b₂) ↦ (b₁ ∘ b₂) | b₁ ∘ b₂ | ✓ |
(b₁, b₂) ↦ [b₁, b₂] | stack(b₁, b₂) | ✓ |
x ↦ b(x) | b(x) | × |
y ↦ b⁻¹(y) | inverse(b)(y) | × |
x ↦ log|det J(b, x)| | logabsdetjac(b, x) | AD |
x ↦ b(x), log|det J(b, x)| | with_logabsdet_jacobian(b, x) | ✓ |
p ↦ q := b_* p | q = transformed(p, b) | ✓ |
y ∼ q | y = rand(q) | ✓ |
p ↦ b such that support(b_* p) = ℝᵈ | bijector(p) | ✓ |
(x ∼ p, b(x), log|det J(b, x)|, log q(y)) | forward(q) | ✓ |
In this table, b
denotes a Bijector
, J(b, x)
denotes the Jacobian of b
evaluated at x
, b_*
denotes the push-forward of p
by b
, and x ∼ p
denotes x
sampled from the distribution with density p
.
The "Automatic" column in the table refers to whether or not you are required to implement the feature for a custom Bijector
. "AD" refers to the fact that it can be implemented "automatically" using automatic differentiation.