MvNormalCDF.jl icon indicating copy to clipboard operation
MvNormalCDF.jl copied to clipboard

MvNormalCanon Extension

Open BA1437 opened this issue 2 years ago • 2 comments

Would it be impossible to extend this package to work with Distributions.MvNormalCanon? I realize one could take the precision matrix of an MvNormalCanon distribution, invert it and create a MvNormal distribution, but it feels like that may be missing out on some computational efficiencies that could be taken advantage of given we already know the precision matrix.

BA1437 avatar Aug 21 '23 02:08 BA1437

If you know how easy to get cholesky factor of correlation matrix that correspond to covariance matrix from precision matrix - it can be possible from the firs view. I'll try to think about it.

PharmCat avatar Aug 21 '23 21:08 PharmCat

Given the cholesky decomposition of the precision matrix, how about something like the following to get the cholesky decomposition of the corresponding covariance matrix:

P= [4. 12. -16.; 12. 37. -43.; -16. -43. 98.] # Precision matrix
C = cholesky(P)

R = zeros(size(P)) #Allocate for the cholesky decomposition of the covariance matrix
n = size(P,1)

for l=n:-1:1
    R[l,l] = sqrt(sum(C.U[1:l,l].^2)-sum(R[l,l+1:n].^2))

    for j=l:-1:1
        R[j,l] = (sum(C.U[1:j,l].*C.U[1:j,j]) - sum(R[l,l+1:n].*R[j,l+1:n]))/R[l,l]
    end

end

r = inv(R)

# check
b = cholesky(Hermitian(inv(P)))
isapprox(r,Matrix(b.U); rtol = 0.001)

BA1437 avatar Aug 22 '23 01:08 BA1437