Skip to content

Fix LKJ numerical stability with PDMats #395

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions 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.15.7"
version = "0.15.8"

[deps]
ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197"
Expand All @@ -14,6 +14,7 @@ IrrationalConstants = "92d709cd-6900-40b7-9082-c6be49f344b6"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
LogExpFunctions = "2ab3a3ac-af41-5b50-aa03-7779005ae688"
MappedArrays = "dbb5928d-eab1-5f90-85c2-b9b0edb7c900"
PDMats = "90014a1f-27ba-587c-ab20-58faa44d9150"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665"
Expand All @@ -37,8 +38,8 @@ BijectorsEnzymeCoreExt = "EnzymeCore"
BijectorsForwardDiffExt = "ForwardDiff"
BijectorsLazyArraysExt = "LazyArrays"
BijectorsMooncakeExt = "Mooncake"
BijectorsReverseDiffExt = "ReverseDiff"
BijectorsReverseDiffChainRulesExt = ["ChainRules", "ReverseDiff"]
BijectorsReverseDiffExt = "ReverseDiff"
BijectorsTrackerExt = "Tracker"
BijectorsZygoteExt = "Zygote"

Expand All @@ -59,6 +60,7 @@ LazyArrays = "2"
LogExpFunctions = "0.3.3"
MappedArrays = "0.2.2, 0.3, 0.4"
Mooncake = "0.4.95"
PDMats = "0.11.35"
Reexport = "0.2, 1"
ReverseDiff = "1"
Roots = "1.3.15, 2"
Expand Down
8 changes: 3 additions & 5 deletions src/bijectors/corr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function with_logabsdet_jacobian(ib::Inverse{CorrBijector}, y)
for j in 2:(K - 1)
logJ += (K - j) * log(U[j, j])
end
return pd_from_upper(U), logJ
return pdmat_from_upper(U), logJ
end

logabsdetjac(::Inverse{CorrBijector}, Y) = _logabsdetjac_inv_corr(Y)
Expand Down Expand Up @@ -136,14 +136,12 @@ function logabsdetjac(b::VecCorrBijector, x)
end

function with_logabsdet_jacobian(::Inverse{VecCorrBijector}, y)
U_logJ = _inv_link_chol_lkj(y)
# workaround for `Tracker.TrackedTuple` not supporting iteration
U, logJ = U_logJ[1], U_logJ[2]
U, logJ = _inv_link_chol_lkj(y)
K = size(U, 1)
for j in 2:(K - 1)
logJ += (K - j) * log(U[j, j])
end
return pd_from_upper(U), logJ
return pdmat_from_upper(U), logJ
end

function logabsdetjac(::Inverse{VecCorrBijector}, y)
Expand Down
12 changes: 11 additions & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using PDMats: PDMat

# `permutedims` seems to work better with AD (cf. KernelFunctions.jl)
aT_b(a::AbstractVector{<:Real}, b::AbstractMatrix{<:Real}) = permutedims(a) * b
# `permutedims` can't be used here since scalar output is desired
Expand All @@ -7,10 +9,16 @@ aT_b(a::AbstractVector{<:Real}, b::AbstractVector{<:Real}) = dot(a, b)
_vec(x::AbstractArray{<:Real}) = vec(x)
_vec(x::Real) = x

# using PDMats.PDMat improves numerical stability of downstream operations,
# most notably taking the determinant
pdmat_from_lower(X::AbstractMatrix) = PDMat(Cholesky(LowerTriangular(X)))
pdmat_from_upper(X::AbstractMatrix) = PDMat(Cholesky(UpperTriangular(X)))

# # Because `ReverseDiff` does not play well with structural matrices.
lower_triangular(A::AbstractMatrix) = convert(typeof(A), LowerTriangular(A))
upper_triangular(A::AbstractMatrix) = convert(typeof(A), UpperTriangular(A))

# TODO: Replace remaining uses of `pd_from_{lower,upper}` with
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this a big task / will it be part of this PR?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think so. That's partly why I kept this PR marked as a draft and didn't request reviews. (The other reason is the failing tests)

# `pdmat_from_{lower,upper}`.
function pd_from_lower(X)
L = lower_triangular(X)
return L * L'
Expand All @@ -35,6 +43,7 @@ rather than `LowerTriangular`.
that returns a `Matrix` rather than `LowerTriangular`.
"""
cholesky_lower(X::AbstractMatrix) = lower_triangular(parent(cholesky(Hermitian(X, :L)).L))
cholesky_lower(X::PDMat) = cholesky_lower(cholesky(X))
cholesky_lower(X::Cholesky) = X.L

"""
Expand All @@ -48,6 +57,7 @@ rather than `UpperTriangular`.
that returns a `Matrix` rather than `UpperTriangular`.
"""
cholesky_upper(X::AbstractMatrix) = upper_triangular(parent(cholesky(Hermitian(X)).U))
cholesky_upper(X::PDMat) = cholesky_upper(cholesky(X))
cholesky_upper(X::Cholesky) = X.U

"""
Expand Down
15 changes: 15 additions & 0 deletions test/bijectors/corr.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Bijectors, DistributionsAD, LinearAlgebra, Test
using Bijectors: VecCorrBijector, VecCholeskyBijector, CorrBijector
using Random: Xoshiro

@testset "CorrBijector & VecCorrBijector" begin
for d in [1, 2, 5]
Expand Down Expand Up @@ -43,6 +44,20 @@ using Bijectors: VecCorrBijector, VecCholeskyBijector, CorrBijector
@test size(dist_unconstrained) == size(x)
@test dist_unconstrained isa MatrixDistribution
end

@testset "Pathological samples for invlink" begin
# see https://github.com/TuringLang/Bijectors.jl/issues/387
d = LKJ(3, 3.0)
for i in 1:100
rng = Xoshiro(i)
y = randn(rng, 3) * 15
f_inv = inverse(bijector(d))
x = f_inv(y)
@test logpdf(d, x) isa Float64 # used to crash.
x, _ = with_logabsdet_jacobian(f_inv, y)
@test logpdf(d, x) isa Float64
end
end
end

@testset "VecCholeskyBijector" begin
Expand Down
Loading