From 9d11ba4350697dff04c1dd6196e825782595e9c8 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Sat, 30 Nov 2024 16:19:17 +0000 Subject: [PATCH] Improve numerical stability of Cholesky invlink --- Project.toml | 2 +- src/bijectors/corr.jl | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index 97a65aff..84b1b77a 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/bijectors/corr.jl b/src/bijectors/corr.jl index 93a5b089..184d5791 100644 --- a/src/bijectors/corr.jl +++ b/src/bijectors/corr.jl @@ -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 += log(2 / (exp(Y[i, j]) + exp(-Y[i, j]))) logJ += log_remainder end logJ += log_remainder @@ -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 += log(2 / (exp(y[idx]) + exp(-y[idx]))) logJ += log_remainder + idx += 1 end logJ += log_remainder W[j, j] = exp(log_remainder)