Skip to content

Commit

Permalink
Default bijector implementation for TransformedDistribution (#187)
Browse files Browse the repository at this point in the history
* added default bijector impl for TransformedDistribution

* patch-version bump

* added a couple of transformed dists to test it

* also test multivariate transformed

* fix interface tests

* same as previous commit but for multivariate distributions
  • Loading branch information
torfjelde authored Jun 14, 2021
1 parent a64750b commit 4fcda27
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Bijectors"
uuid = "76274a88-744f-5084-9051-94815aaf08c4"
version = "0.9.5"
version = "0.9.6"

[deps]
ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197"
Expand Down
1 change: 1 addition & 0 deletions src/transformed_distribution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ transformed(d) = transformed(d, bijector(d))
Returns the constrained-to-unconstrained bijector for distribution `d`.
"""
bijector(td::TransformedDistribution) = bijector(td.dist) inv(td.transform)
bijector(d::DiscreteUnivariateDistribution) = Identity{0}()
bijector(d::DiscreteMultivariateDistribution) = Identity{1}()
bijector(d::ContinuousUnivariateDistribution) = TruncatedBijector(minimum(d), maximum(d))
Expand Down
16 changes: 12 additions & 4 deletions test/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ end
Rayleigh(1.0),
TDist(2),
truncated(Normal(0, 1), -Inf, 2),
transformed(Beta(2,2)),
transformed(Exponential()),
]

for dist in uni_dists
Expand Down Expand Up @@ -106,8 +108,10 @@ end
b = bijector(d)
x = rand(d)
y = b(x)
@test log(abs(ForwardDiff.derivative(b, x))) logabsdetjac(b, x)
@test log(abs(ForwardDiff.derivative(inv(b), y))) logabsdetjac(inv(b), y)
# `ForwardDiff.derivative` can lead to some numerical inaccuracy,
# so we use a slightly higher `atol` than default.
@test log(abs(ForwardDiff.derivative(b, x))) logabsdetjac(b, x) atol=1e-6
@test log(abs(ForwardDiff.derivative(inv(b), y))) logabsdetjac(inv(b), y) atol=1e-6
end

@testset "$dist: ForwardDiff AD" begin
Expand Down Expand Up @@ -401,6 +405,8 @@ end
MvLogNormal(MvNormal(randn(10), exp.(randn(10)))),
Dirichlet([1000 * one(Float64), eps(Float64)]),
Dirichlet([eps(Float64), 1000 * one(Float64)]),
transformed(MvNormal(randn(10), exp.(randn(10)))),
transformed(MvLogNormal(MvNormal(randn(10), exp.(randn(10)))))
]

for dist in vector_dists
Expand Down Expand Up @@ -446,9 +452,11 @@ end
b = bijector(dist)
x = rand(dist)
y = b(x)
# `ForwardDiff.derivative` can lead to some numerical inaccuracy,
# so we use a slightly higher `atol` than default.
@test b(param(x)) isa TrackedArray
@test log(abs(det(ForwardDiff.jacobian(b, x)))) logabsdetjac(b, x)
@test log(abs(det(ForwardDiff.jacobian(inv(b), y)))) logabsdetjac(inv(b), y)
@test log(abs(det(ForwardDiff.jacobian(b, x)))) logabsdetjac(b, x) atol=1e-6
@test log(abs(det(ForwardDiff.jacobian(inv(b), y)))) logabsdetjac(inv(b), y) atol=1e-6
end
end
end
Expand Down

2 comments on commit 4fcda27

@torfjelde
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/38793

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.9.6 -m "<description of version>" 4fcda273c3a317677aa1bc96434597a39b5f459a
git push origin v0.9.6

Please sign in to comment.