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
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
2 changes: 1 addition & 1 deletion 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.2"
version = "0.15.3"

[deps]
ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197"
Expand Down
32 changes: 10 additions & 22 deletions src/bijectors/corr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
CorrBijector <: Bijector

A bijector implementation of Stan's parametrization method for Correlation matrix:
https://mc-stan.org/docs/2_23/reference-manual/correlation-matrix-transform-section.html
https://mc-stan.org/docs/reference-manual/transforms.html#correlation-matrix-transform.section

Basically, a unconstrained strictly upper triangular matrix `y` is transformed to
a correlation matrix by following readable but not that efficient form:
Expand Down Expand Up @@ -348,13 +348,12 @@ function _inv_link_chol_lkj(Y::AbstractMatrix)
T = float(eltype(W))
logJ = zero(T)

idx = 1
@inbounds for j in 1:K
log_remainder = zero(T) # log of proportion of unit vector remaining
for i in 1:(j - 1)
z = tanh(Y[i, j])
W[i, j] = z * exp(log_remainder)
log_remainder += log1p(-z^2) / 2
log_remainder -= LogExpFunctions.logcosh(Y[i, j])
logJ += log_remainder
end
logJ += log_remainder
Expand All @@ -380,10 +379,10 @@ function _inv_link_chol_lkj(y::AbstractVector)
log_remainder = zero(T) # log of proportion of unit vector remaining
for i in 1:(j - 1)
z = tanh(y[idx])
idx += 1
W[i, j] = z * exp(log_remainder)
log_remainder += log1p(-z^2) / 2
log_remainder -= LogExpFunctions.logcosh(y[idx])
logJ += log_remainder
idx += 1
end
logJ += log_remainder
W[j, j] = exp(log_remainder)
Expand Down Expand Up @@ -461,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])
end
return result
end
Expand All @@ -477,13 +471,8 @@ function _logabsdetjac_inv_corr(y::AbstractVector)

result = float(zero(eltype(y)))
for (i, y_i) in enumerate(y)
abs_y_i = abs(y_i)
row_idx = vec_to_triu1_row_index(i)
result +=
(K - row_idx + 1) * (
IrrationalConstants.logtwo -
(abs_y_i + LogExpFunctions.log1pexp(-2 * abs_y_i))
)
result -= (K - row_idx + 1) * LogExpFunctions.logcosh(y_i)
end
return result
end
Expand All @@ -496,10 +485,9 @@ 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 = log(1 - z^2)
result += logz + (tmp / 2)
tmp += logz
logcoshy = LogExpFunctions.logcosh(y[idx])
tmp -= logcoshy
result += tmp - logcoshy
idx += 1
end
end
Expand Down
Loading