Basic usage
Other than the logpdf_with_trans
methods, the package also provides a more composable interface through the Bijector
types. Consider for example the one from above with Beta(2, 2)
.
julia> using Random;
Random.seed!(42);
julia> using Bijectors;
using Bijectors: Logit;
julia> dist = Beta(2, 2)
Beta{Float64}(α=2.0, β=2.0)
julia> x = rand(dist)
0.36888689965963756
julia> b = bijector(dist) # bijection (0, 1) → ℝ
Logit{Float64}(0.0, 1.0)
julia> y = b(x)
-0.5369949942509267
In this case we see that bijector(d::Distribution)
returns the corresponding constrained-to-unconstrained bijection for Beta
, which indeed is a Logit
with a = 0.0
and b = 1.0
. The resulting Logit <: Bijector
has a method (b::Logit)(x)
defined, allowing us to call it just like any other function. Comparing with the above example, b(x) ≈ link(dist, x)
. Just to convince ourselves:
julia> b(x) ≈ link(dist, x)
true
Transforming distributions
We can create a transformed Distribution
, i.e. a Distribution
defined by sampling from a given Distribution
and then transforming using a given transformation:
julia> dist = Beta(2, 2) # support on (0, 1)
Beta{Float64}(α=2.0, β=2.0)
julia> tdist = transformed(dist) # support on ℝ
UnivariateTransformed{Beta{Float64}, Bijectors.Logit{Float64, Float64}}( dist: Beta{Float64}(α=2.0, β=2.0) transform: Bijectors.Logit{Float64, Float64}(0.0, 1.0) )
julia> tdist isa UnivariateDistribution
true
We can the then compute the logpdf
for the resulting distribution:
julia> # Some example values x = rand(dist)
0.5150799400519378
julia> y = tdist.transform(x)
0.06033805951663279
julia> logpdf(tdist, y)
-0.9826493176551532