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

**BREAKING** Fix loss of structure in cov() and invcov() of AbstractMvNormal subtypes #1373

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions src/multivariate/mvnormal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,10 @@ function kldivergence(p::AbstractMvNormal, q::AbstractMvNormal)
# This is the generic implementation for AbstractMvNormal, you might need to specialize for your type
length(p) == length(q) ||
throw(DimensionMismatch("Distributions p and q have different dimensions $(length(p)) and $(length(q))"))
# logdetcov is used separately from _cov for any potential optimization done there
return (tr(_cov(q) \ _cov(p)) + sqmahal(q, mean(p)) - length(p) + logdetcov(q) - logdetcov(p)) / 2
# logdetcov is used for any potential optimization done there
return (tr(cov(q) \ cov(p)) + sqmahal(q, mean(p)) - length(p) + logdetcov(q) - logdetcov(p)) / 2
end

# This is a workaround to take advantage of the PDMats objects for MvNormal and avoid copies as Matrix
# TODO: Remove this once `cov(::MvNormal)` returns the PDMats object
_cov(d::AbstractMvNormal) = cov(d)

"""
invcov(d::AbstractMvNormal)

Expand Down Expand Up @@ -256,10 +252,9 @@ params(d::MvNormal) = (d.μ, d.Σ)
@inline partype(d::MvNormal{T}) where {T<:Real} = T

var(d::MvNormal) = diag(d.Σ)
cov(d::MvNormal) = Matrix(d.Σ)
_cov(d::MvNormal) = d.Σ
cov(d::MvNormal) = d.Σ

invcov(d::MvNormal) = Matrix(inv(d.Σ))
invcov(d::MvNormal) = inv(d.Σ)
logdetcov(d::MvNormal) = logdet(d.Σ)

### Evaluation
Expand Down
4 changes: 2 additions & 2 deletions src/multivariate/mvnormalcanon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ params(d::MvNormalCanon) = (d.μ, d.h, d.J)
Base.eltype(::Type{<:MvNormalCanon{T}}) where {T} = T

var(d::MvNormalCanon) = diag(inv(d.J))
cov(d::MvNormalCanon) = Matrix(inv(d.J))
invcov(d::MvNormalCanon) = Matrix(d.J)
cov(d::MvNormalCanon) = inv(d.J)
invcov(d::MvNormalCanon) = d.J
logdetcov(d::MvNormalCanon) = -logdet(d.J)


Expand Down
Loading