Skip to content

Commit 9f1a2a7

Browse files
oschulzdevmotion
andauthored
Add support for ChangesOfVariables.with_logabsdet_jacobian (#30)
* Support ChangesOfVariables.with_logabsdet_jacobian * Document support for InverseFunctions and ChangesOfVariables * Neatify with_logabdet_jacobian defs and tests Co-authored-by: David Widmann <[email protected]> * Fix with_logabsdet_jacobian Co-authored-by: David Widmann <[email protected]>
1 parent d0c80b7 commit 9f1a2a7

File tree

6 files changed

+56
-0
lines changed

6 files changed

+56
-0
lines changed

Project.toml

+2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ version = "0.3.4"
55

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

1314
[compat]
1415
ChainRulesCore = "1"
16+
ChangesOfVariables = "0.1"
1517
DocStringExtensions = "0.8"
1618
InverseFunctions = "0.1"
1719
IrrationalConstants = "0.1"

docs/src/index.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Various special functions based on `log` and `exp` moved from [StatsFuns.jl](htt
44

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

7+
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`.
8+
79
```@docs
810
xlogx
911
xlogy

src/LogExpFunctions.jl

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ using DocStringExtensions: SIGNATURES
44
using Base: Math.@horner
55

66
import ChainRulesCore
7+
import ChangesOfVariables
78
import InverseFunctions
89
import IrrationalConstants
910
import LinearAlgebra
@@ -16,5 +17,6 @@ include("basicfuns.jl")
1617
include("logsumexp.jl")
1718
include("chainrules.jl")
1819
include("inverse.jl")
20+
include("with_logabsdet_jacobian.jl")
1921

2022
end # module

src/with_logabsdet_jacobian.jl

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function ChangesOfVariables.with_logabsdet_jacobian(::typeof(log1pexp), x::Real)
2+
y = log1pexp(x)
3+
return y, x - y
4+
end
5+
6+
function ChangesOfVariables.with_logabsdet_jacobian(::typeof(logexpm1), x::Real)
7+
y = logexpm1(x)
8+
return y, x - y
9+
end
10+
11+
function ChangesOfVariables.with_logabsdet_jacobian(::typeof(log1mexp), x::Real)
12+
y = log1mexp(x)
13+
return y, x - y
14+
end
15+
16+
function ChangesOfVariables.with_logabsdet_jacobian(::typeof(log2mexp), x::Real)
17+
y = log2mexp(x)
18+
return y, x - y
19+
end
20+
21+
function ChangesOfVariables.with_logabsdet_jacobian(::typeof(logit), x::Real)
22+
y = logit(x)
23+
y, -log(x * (1 - x))
24+
end
25+
26+
function ChangesOfVariables.with_logabsdet_jacobian(::typeof(logistic), x::Real)
27+
y = logistic(x)
28+
y, log(y * (1 - y))
29+
end

test/runtests.jl

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using LogExpFunctions
22
using ChainRulesTestUtils
3+
using ChangesOfVariables
34
using InverseFunctions
45
using OffsetArrays
56

@@ -11,3 +12,4 @@ Random.seed!(1234)
1112
include("basicfuns.jl")
1213
include("chainrules.jl")
1314
include("inverse.jl")
15+
include("with_logabsdet_jacobian.jl")

test/with_logabsdet_jacobian.jl

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@testset "with_logabsdet_jacobian" begin
2+
derivative(f, x) = ChainRulesTestUtils.frule((ChainRulesTestUtils.NoTangent(), 1), f, x)[2]
3+
4+
x = randexp()
5+
6+
ChangesOfVariables.test_with_logabsdet_jacobian(log1pexp, x, derivative)
7+
ChangesOfVariables.test_with_logabsdet_jacobian(log1pexp, -x, derivative)
8+
9+
ChangesOfVariables.test_with_logabsdet_jacobian(logexpm1, x, derivative)
10+
11+
ChangesOfVariables.test_with_logabsdet_jacobian(log1mexp, -x, derivative)
12+
13+
ChangesOfVariables.test_with_logabsdet_jacobian(log2mexp, log(2) - x, derivative)
14+
15+
ChangesOfVariables.test_with_logabsdet_jacobian(logistic, -x, derivative)
16+
ChangesOfVariables.test_with_logabsdet_jacobian(logistic, x, derivative)
17+
18+
ChangesOfVariables.test_with_logabsdet_jacobian(logit, rand(), derivative)
19+
end

0 commit comments

Comments
 (0)