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

Cholesky numerical stability: inverse transform #356

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
16 changes: 5 additions & 11 deletions src/bijectors/corr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ function _inv_link_chol_lkj(Y::AbstractMatrix)
for i in 1:(j - 1)
z = tanh(Y[i, j])
W[i, j] = z * exp(log_remainder)
log_remainder += log(2 / (exp(Y[i, j]) + exp(-Y[i, j])))
log_remainder -= LogExpFunctions.logcosh(Y[i, j])
logJ += log_remainder
end
logJ += log_remainder
Expand All @@ -380,7 +380,7 @@ function _inv_link_chol_lkj(y::AbstractVector)
for i in 1:(j - 1)
z = tanh(y[idx])
W[i, j] = z * exp(log_remainder)
log_remainder += log(2 / (exp(y[idx]) + exp(-y[idx])))
log_remainder -= LogExpFunctions.logcosh(y[idx])
logJ += log_remainder
idx += 1
end
Expand Down Expand Up @@ -460,13 +460,8 @@ function _logabsdetjac_inv_corr(Y::AbstractMatrix)
K = LinearAlgebra.checksquare(Y)

result = float(zero(eltype(Y)))
for j in 2:K, i in 1:(j - 1)
@inbounds abs_y_i_j = abs(Y[i, j])
result +=
(K - i + 1) * (
IrrationalConstants.logtwo -
(abs_y_i_j + LogExpFunctions.log1pexp(-2 * abs_y_i_j))
)
@inbounds for j in 2:K, i in 1:(j - 1)
result += (K - i + 1) * (-LogExpFunctions.logcosh(Y[i, j]))
penelopeysm marked this conversation as resolved.
Show resolved Hide resolved
end
return result
end
Expand Down Expand Up @@ -495,8 +490,7 @@ function _logabsdetjac_inv_chol(y::AbstractVector)
@inbounds for j in 2:K
tmp = zero(result)
for _ in 1:(j - 1)
z = tanh(y[idx])
logz = 2 * log(2 / (exp(y[idx]) + exp(-y[idx])))
logz = -2 * LogExpFunctions.logcosh(y[idx])
Copy link
Member

Choose a reason for hiding this comment

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

The name logz is a bit meaningless now I guess 🙂

result += logz + (tmp / 2)
tmp += logz
idx += 1
Expand Down
Loading