Closed Junyi-Que closed 1 year ago
Thanks for looking into this. I thought the code accounted for the difference, and we also test for this. Currently the tests use ForwardDiff
, but we should probably move to FD. See eg
using TransformVariables, FiniteDifferences, LinearAlgebra
t = CorrCholeskyFactor(3)
x = [-0.12833806457027003, 0.24721188811516634, -0.24578748441593382]
_, lj = TransformVariables.transform_and_logjac(t, x)
above_diag(M::AbstractMatrix) = [M[i, j] for (i, j) in map(Tuple, eachindex(M)) if i < j]
j_fd = jacobian(central_fdm(5, 1), x -> above_diag(transform(t, x)), x)[1]
isapprox(logabsdet(j_fd)[1], lj)
tests true
.
Dear Tamas,
Sorry for the false alarm. I think what I computed was the log absolute Jacobian determinant of transforming to the correlation matrix instead of the Cholesky factor. Thank you for your patience.
Best, Junyi
The way TransformVariables.jl constructs the Cholesky factor of a correlation seems similar to what's given here https://mc-stan.org/docs/reference-manual/correlation-matrix-transform.html, with the only difference being $z = tanh(y/2)$ instead of $z = tanh(y)$. The log absolute Jacobian determinant should be $$\sum{i=1}^{K-1}\sum{j=i+1}^{K}\frac{K-i-1}{2}\ln[1-(z{i,j})^2]+\sum{i=1}^{K-1}\sum{j=i+1}^{K}\ln\frac{\partial z{i,j}}{\partial y{i,j}}$$ If $K=3$, it's $$\frac{1}{2}(\ln[1-(z{1,2})^2]+\ln[1-(z{1,3})^2])+\sum{i=1}^{2}\sum{j=i+1}^{3}\ln\frac{\partial z{i,j}}{\partial y_{i,j}}$$
But the calculation in the library misses $\frac{1}{2}\ln[1-(z_{1,2})^2]$ when K=3, and this problem generalizes to larger Ks. The code below illustrates this problem for $K=3$.