Skip to content

Commit

Permalink
Use logcosh
Browse files Browse the repository at this point in the history
Co-authored-by: David Widmann <devmotion@users.noreply.github.com>
  • Loading branch information
penelopeysm and devmotion committed Nov 30, 2024
1 parent 571aefa commit 81bedf2
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 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 += IrrationalConstants.logtwo + Y[i, j] - LogExpFunctions.log1pexp(2 * 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 += IrrationalConstants.logtwo + y[idx] - LogExpFunctions.log1pexp(2 * 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]))
end
return result
end
Expand Down Expand Up @@ -495,7 +490,7 @@ function _logabsdetjac_inv_chol(y::AbstractVector)
@inbounds for j in 2:K
tmp = zero(result)
for _ in 1:(j - 1)
logz = 2 * (IrrationalConstants.logtwo + y[idx] - LogExpFunctions.log1pexp(2 * y[idx]))
logz = -2 * LogExpFunctions.logcosh(y[idx])
result += logz + (tmp / 2)
tmp += logz
idx += 1
Expand Down

0 comments on commit 81bedf2

Please sign in to comment.