filtron / MarkovKernels.jl

Marginal distributions and Markov kernels that play nice with each other for the purpose of Bayesian state estimation.
MIT License
17 stars 2 forks source link

How to use `PSDMatrices.PSDMatrix` as a covariance parameter #101

Closed nathanaelbosch closed 3 months ago

nathanaelbosch commented 3 months ago

In my code I rely on PSDMatrices.jl to represent covariace parameters of Gaussian distributions. I would also like to use these as covariance parameters for MarkovKernels.Normal. When I try this it works, but the covariance parameter gets transformed into a Symmetric:

2×2 Symmetric{Float64, PSDMatrix{Float64, Matrix{Float64}}}

But by construction, PSDMatrix is already symmetric. I tried overloading Symmetric in PSDMatrices.jl with

Symmetric(M::PSDMatrix) = M

but then I get stack overflow issues as the Normal constructor repeatedly tries to call Symmetric. What do you think would be the best way to fix this and allow for PSDMatrices?

One proposal would be to write a new constructor for Normal that does not attempt to transform a PSDMatrix into Symmetric, e.g. with

function Normal(μ::AbstractVector, Σ::PSDMatrix)
    T = promote_type(eltype(μ), eltype(Σ))
    return Normal{T}(convert(AbstractVector{T}, μ), convert(PSDMatrix{T}, Σ))
end

This could then be a package extension. But maybe a different approach makes more sense so let me know what you think.

filtron commented 3 months ago

An alternative would be to remove the constructor with signature Normal(::AbstractVector, ::AbstractMatrix)

nathanaelbosch commented 3 months ago

I would be fine with that too.

filtron commented 3 months ago

Let's do that then.