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

MvNormalCanon Extension #10

Open
BA1437 opened this issue Aug 21, 2023 · 2 comments
Open

MvNormalCanon Extension #10

BA1437 opened this issue Aug 21, 2023 · 2 comments

Comments

@BA1437
Copy link

BA1437 commented Aug 21, 2023

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.

@PharmCat
Copy link
Owner

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.

@BA1437
Copy link
Author

BA1437 commented Aug 22, 2023

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants