Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ChangesOfVariables.with_logabsdet_jacobian #30

Merged
merged 4 commits into from
Nov 8, 2021

Conversation

oschulz
Copy link
Contributor

@oschulz oschulz commented Nov 7, 2021

This PR adds support for ChangesOfVariables.test_with_logabsdet_jacobian to functions in LogExpFunctions that already support InverseFunctions.inverse.

This complements #29: InverseFunctions and ChangesOfVariables were designed in tandem (both are extremely lightweight packages) to support variable transformations. Applications that perform variable transformations often need the volume element of the transformation in addition to the result and the inverse of the transformation. ChangesOfVariables.with_logabsdet_jacobian(f, x) delivers that capability, it returns the equivalent of f(x), logabsdet(jacobian(f, x))

With this PR, fun stuff like this works out of the box:

using LogExpFunctions, InverseFunctions, ChangesOfVariables

X = rand(5)
trafo = Base.Fix1(broadcast, logit)

Y, ladj = with_logabsdet_jacobian(trafo, X)

X2, ladj2 = with_logabsdet_jacobian(inverse(trafo), Y)

@assert X  X2  logistic.(Y) && ladj  -ladj2

Packages like Bijectors that use logistic and logit in transformations can profit directly from implementing with_logabsdet_jacobian in LogExpFunctions (Bijectors will support InverseFunctions and ChangesOfVariables soon, see TuringLang/Bijectors.jl#199).

This PR has no significant effect on the load time of LogExpFunctions (@time using LogExpFunctions showed 0.47 s without and 0.45 s with this PR).

CC @devmotion, @torfjelde, @willtebbutt

src/with_logabsdet_jacobian.jl Outdated Show resolved Hide resolved
src/with_logabsdet_jacobian.jl Outdated Show resolved Hide resolved
src/with_logabsdet_jacobian.jl Outdated Show resolved Hide resolved
src/with_logabsdet_jacobian.jl Outdated Show resolved Hide resolved
src/with_logabsdet_jacobian.jl Outdated Show resolved Hide resolved
test/with_logabsdet_jacobian.jl Outdated Show resolved Hide resolved
test/with_logabsdet_jacobian.jl Outdated Show resolved Hide resolved
test/with_logabsdet_jacobian.jl Outdated Show resolved Hide resolved
test/with_logabsdet_jacobian.jl Outdated Show resolved Hide resolved
test/with_logabsdet_jacobian.jl Outdated Show resolved Hide resolved
Co-authored-by: David Widmann <devmotion@users.noreply.github.com>
@oschulz
Copy link
Contributor Author

oschulz commented Nov 7, 2021

Thanks for the improvements @devmotion . Can you start CI?

@oschulz
Copy link
Contributor Author

oschulz commented Nov 7, 2021

Can you start CI again?

@oschulz
Copy link
Contributor Author

oschulz commented Nov 7, 2021

Looks good.

@devmotion
Copy link
Member

Looks good, thanks @oschulz!

@devmotion devmotion merged commit 9f1a2a7 into JuliaStats:master Nov 8, 2021
@oschulz
Copy link
Contributor Author

oschulz commented Nov 9, 2021

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants