Skip to content

Commit 1771aab

Browse files
authored
Merge pull request #117 from tpapp/tp/fix-l2-remainder-singularity
Fix l2 remainder singularity, use tanh for calculations.
2 parents 4b4d0a6 + 088c11c commit 1771aab

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

Project.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "TransformVariables"
22
uuid = "84d833dd-6860-57f9-a1a7-6da5db126cff"
33
authors = ["Tamas K. Papp <tkpapp@gmail.com>"]
4-
version = "0.8.8"
4+
version = "0.8.9"
55

66
[deps]
77
ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197"
@@ -11,7 +11,6 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
1111
InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"
1212
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1313
LogExpFunctions = "2ab3a3ac-af41-5b50-aa03-7779005ae688"
14-
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
1514
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1615
SimpleUnPack = "ce78b400-467f-4804-87d8-8f486da07d0a"
1716
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
@@ -22,7 +21,9 @@ ChangesOfVariables = "0.1"
2221
DocStringExtensions = "0.8, 0.9"
2322
ForwardDiff = "0.10"
2423
InverseFunctions = "0.1"
24+
LinearAlgebra = "1.6"
2525
LogExpFunctions = "0.3"
26+
Random = "1.6"
2627
SimpleUnPack = "1"
2728
StaticArrays = "1"
2829
julia = "1.6"

src/special_arrays.jl

+15-5
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,39 @@ export UnitVector, UnitSimplex, CorrCholeskyFactor, corr_cholesky_factor
44
#### building blocks
55
####
66

7+
"""
8+
$(SIGNATURES)
9+
10+
`log(abs(…))` of the derivative of `tanh`, calculated accurately.
11+
"""
12+
function _tanh_logabsderiv(x)
13+
d = 2*x
14+
log(4) + d - 2 * log1pexp(d)
15+
end
16+
717
"""
818
(y, r, ℓ) = $SIGNATURES
919
1020
Given ``x ∈ ℝ`` and ``0 ≤ r ≤ 1``, return `(y, r′)` such that
1121
12-
1. ``y² + r′² = r²``,
22+
1. ``y² + (r′)² = r²``,
1323
1424
2. ``y: |y| ≤ r`` is mapped with a bijection from `x`.
1525
1626
`ℓ` is the log Jacobian (whether it is evaluated depends on `flag`).
1727
"""
1828
@inline function l2_remainder_transform(flag::LogJacFlag, x, r)
19-
z = 2*logistic(x) - 1
20-
(z * r, r*(1 - abs2(z)),
21-
flag isa NoLogJac ? flag : log(2) + logistic_logjac(x) + 0.5*log(r))
29+
# note that 1-tanh(x)^2 = sech(x)^2
30+
(tanh(x) * r, r*sech(x)^2,
31+
flag isa NoLogJac ? flag : _tanh_logabsderiv(x) + 0.5*log(r))
2232
end
2333

2434
"""
2535
(x, r′) = $SIGNATURES
2636
2737
Inverse of [`l2_remainder_transform`](@ref) in `x` and `y`.
2838
"""
29-
@inline l2_remainder_inverse(y, r) = logit((y/√r+1)/2), r-abs2(y)
39+
@inline l2_remainder_inverse(y, r) = atanh(y/√r), r-y^2
3040

3141
####
3242
#### UnitVector

test/Project.toml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
88
LogDensityProblems = "6fdf6af0-433a-55f7-b3ed-c6c6e0b8df7c"
99
LogDensityProblemsAD = "996a588d-648d-4e1f-a8f0-a84b347e47b1"
1010
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
11+
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
1112
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1213
SimpleUnPack = "ce78b400-467f-4804-87d8-8f486da07d0a"
1314
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

test/runtests.jl

+12
Original file line numberDiff line numberDiff line change
@@ -659,3 +659,15 @@ end
659659
y.b[3] += 1
660660
@test x[4] == z + 1
661661
end
662+
663+
@testset "near-singular Cholesky factor" begin
664+
x = [8.348500225024523, -3.80486310849193, -15.115725300837742, 5.840812234057503,
665+
7.548980701857334, 0.6546495312434718, 2.863837638357627, 1.0081703617568052,
666+
-38.543769810398466, -14.252165683848483, -22.75952203884357, 1.9543987098768612,
667+
5.415229912144962, -1.4360948924991273, 4.957606068283541, -5.443369115798325,
668+
-2.536087079311158, -2.0710241403850635, -0.982209305513312, 6.821758239096414,
669+
5.925173901833287]
670+
t = corr_cholesky_factor(7)
671+
U = transform(t, x)
672+
@test isfinite(logabsdet(U)[1])
673+
end

0 commit comments

Comments
 (0)