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

cov of diagonal normal dist should be diagonal #1503

Closed
oschulz opened this issue Feb 2, 2022 · 3 comments
Closed

cov of diagonal normal dist should be diagonal #1503

oschulz opened this issue Feb 2, 2022 · 3 comments

Comments

@oschulz
Copy link
Contributor

oschulz commented Feb 2, 2022

Currently we have

julia> d = product_distribution(Normal.(randn(3), rand(3)))
DiagNormal(...)

julia> cov(d) isa Matrix
true

But we'd like to have cov(d) isa Diagonal, of course, so that cov doesn't blow up our memory for (very) large products of Normal (resp. a large diagonal MvNormal).

This happens because we currently define

cov(d::MvNormal) = Matrix(d.Σ)

(so cov will annoyingly also silently convert a GPU array to a CPU array). Do we have a better generic mechanism to turn an AbstractPDMat into a "normal matrix" that's not automatically a Matrix? We could introduce something like

without_chol(A::AbstractMatrix) = A
without_chol(A::PDMat) = A.mat
without_chol(A::PDiagMat) = Diagonal(diag(A))
without_chol(A::PDSparseMat) = A.mat
without_chol(A::ScalMat) = Diagonal(Fill(A.value, A.dim))
@devmotion
Copy link
Member

Fixed by #1373 (I assume).

@devmotion
Copy link
Member

Although that won't give you a Diagonal but whatever the covariance matrix is. I.e., an AbstractPDMat for MvNormal. IMO this is fine though since they are also optimized AbstractArrays.

@oschulz
Copy link
Contributor Author

oschulz commented Feb 2, 2022

IMO this is fine though since they are also optimized AbstractArrays.

Oh yes, that's much better, since it preserves the Cholesky decomposition (for non-diagonals). I'll close this, then.

@oschulz oschulz closed this as completed Feb 2, 2022
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