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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ version = "0.3.4"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
ChangesOfVariables = "9e997f8a-9a97-42d5-a9f1-ce6bfc15e2c0"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"
IrrationalConstants = "92d709cd-6900-40b7-9082-c6be49f344b6"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[compat]
ChainRulesCore = "1"
ChangesOfVariables = "0.1"
DocStringExtensions = "0.8"
InverseFunctions = "0.1"
IrrationalConstants = "0.1"
Expand Down
2 changes: 2 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Various special functions based on `log` and `exp` moved from [StatsFuns.jl](htt

The original authors of these functions are the StatsFuns.jl contributors.

LogExpFunctions supports [`InverseFunctions.inverse`](https://github.com/JuliaMath/InverseFunctions.jl) and [`ChangesOfVariables.test_with_logabsdet_jacobian`](https://github.com/JuliaMath/ChangesOfVariables.jl) for `log1mexp`, `log1pexp`, `log1pexp`, `log2mexp`, `log2mexp`, `logexpm1`, `logistic`, `logistic` and `logit`.

```@docs
xlogx
xlogy
Expand Down
2 changes: 2 additions & 0 deletions src/LogExpFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using DocStringExtensions: SIGNATURES
using Base: Math.@horner

import ChainRulesCore
import ChangesOfVariables
import InverseFunctions
import IrrationalConstants
import LinearAlgebra
Expand All @@ -16,5 +17,6 @@ include("basicfuns.jl")
include("logsumexp.jl")
include("chainrules.jl")
include("inverse.jl")
include("with_logabsdet_jacobian.jl")

end # module
43 changes: 43 additions & 0 deletions src/with_logabsdet_jacobian.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function ChangesOfVariables.with_logabsdet_jacobian(::typeof(log1pexp), x::Real)
y = log1pexp(x)
return y, x - y
end

function ChangesOfVariables.with_logabsdet_jacobian(::typeof(logexpm1), x::Real)
y = logexpm1(x)
return y, (x - y)
oschulz marked this conversation as resolved.
Show resolved Hide resolved
end

function ChangesOfVariables.with_logabsdet_jacobian(::typeof(log1mexp), x::Real)
y = log1mexp(x)
return y, (x - y)
oschulz marked this conversation as resolved.
Show resolved Hide resolved
end

function ChangesOfVariables.with_logabsdet_jacobian(::typeof(log2mexp), x::Real)
y = log2mexp(x)
return y, (x - y)
oschulz marked this conversation as resolved.
Show resolved Hide resolved
end

function ChangesOfVariables.with_logabsdet_jacobian(::typeof(logit), x::Real)
y = logit(x)
y, log(inv(x * (1 - x)))
oschulz marked this conversation as resolved.
Show resolved Hide resolved
end

function ChangesOfVariables.with_logabsdet_jacobian(::typeof(logistic), x::Real)
y = logistic(x)
y, log(y * (1 - y))
end


#=
ChainRulesCore.@scalar_rule(log1pexp(x::Real), (logistic(x),))
ChainRulesCore.@scalar_rule(logexpm1(x::Real), (exp(x - Ω),))

ChainRulesCore.@scalar_rule(log1mexp(x::Real), (-exp(x - Ω),))

ChainRulesCore.@scalar_rule(log2mexp(x::Real), (-exp(x - Ω),))

ChainRulesCore.@scalar_rule(logistic(x::Real), (Ω * (1 - Ω),))

ChainRulesCore.@scalar_rule(logit(x::Real), (inv(x * (1 - x)),))
=#
oschulz marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using LogExpFunctions
using ChainRulesCore
oschulz marked this conversation as resolved.
Show resolved Hide resolved
using ChainRulesTestUtils
using ChangesOfVariables
using InverseFunctions
using OffsetArrays

Expand All @@ -11,3 +13,4 @@ Random.seed!(1234)
include("basicfuns.jl")
include("chainrules.jl")
include("inverse.jl")
include("with_logabsdet_jacobian.jl")
20 changes: 20 additions & 0 deletions test/with_logabsdet_jacobian.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@testset "with_logabsdet_jacobian.jl" begin
derivative(f, x) = ChainRulesCore.frule((nothing,1), f, x)[2]
oschulz marked this conversation as resolved.
Show resolved Hide resolved

x = randexp()

ChangesOfVariables.test_with_logabsdet_jacobian(log1pexp, +x, derivative)
oschulz marked this conversation as resolved.
Show resolved Hide resolved
ChangesOfVariables.test_with_logabsdet_jacobian(log1pexp, -x, derivative)

ChangesOfVariables.test_with_logabsdet_jacobian(logexpm1, +x, derivative)
oschulz marked this conversation as resolved.
Show resolved Hide resolved

ChangesOfVariables.test_with_logabsdet_jacobian(log1mexp, -x, derivative)

ChangesOfVariables.test_with_logabsdet_jacobian(log2mexp, +log(2) - x, derivative)
oschulz marked this conversation as resolved.
Show resolved Hide resolved
ChangesOfVariables.test_with_logabsdet_jacobian(log2mexp, -log(2) + x, derivative)
oschulz marked this conversation as resolved.
Show resolved Hide resolved

ChangesOfVariables.test_with_logabsdet_jacobian(logistic, -x, derivative)
ChangesOfVariables.test_with_logabsdet_jacobian(logistic, +x, derivative)
oschulz marked this conversation as resolved.
Show resolved Hide resolved

ChangesOfVariables.test_with_logabsdet_jacobian(logit, rand(), derivative)
end