Skip to content

cov(d::MultivariateMixture) assumes overly specific typing on component covariance #1969

@btmit

Description

@btmit

The current cov method for MixtureModel assumes that the cov method for each component returns a type that has strides is defined for it. This is very specific and breaks in the following simple case.

MWE:

using Distributions, LinearAlgebra

struct Gaussian{T,M<:AbstractMatrix{T},V<:AbstractVector{T}} <: AbstractMvNormal
    μ::V
    Σ::Symmetric{T,M}
end

Distributions.mean(g::Gaussian) = g.μ
Distributions.cov(g::Gaussian) = g.Σ

Base.length(g::Gaussian) = length(mean(g))
Base.eltype(::Gaussian{T}) where {T} = T

g1 = Gaussian(rand(3), Symmetric(rand(3,3)))
g2 = Gaussian(rand(3), Symmetric(rand(3,3)))

gm = MixtureModel([g1, g2])

cov(gm)

ERROR: MethodError: no method matching strides(::Symmetric{Float64, Matrix{Float64}})

The issue is that this cov(d::MultivariateMixture) calls directly into BLAS.axpy!(pi, cov(c), V) without any sort of checks on cov(c). It seems there is a layer missing here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions