JuliaStats / Distributions.jl

A Julia package for probability distributions and associated functions.
Other
1.08k stars 410 forks source link

rand gives wrong results for MvNormal with PSDMat covariance #1776

Closed knuesel closed 9 months ago

knuesel commented 9 months ago

As mentioned in #1602, #1596 and #791 a naive MvNormal([0 0; 0 1]) fails with

ERROR: PosDefException: matrix is not positive definite; Cholesky factorization failed.

The proposed workaround for rand is to use PSDMat but that gives wrong results. Here's a MWE with Distribution 0.25.100 and PDMatsExtras 2.6.3:

julia> using Distributions, PDMatsExtras

julia> x = MvNormal([0, 0], PSDMat([0. 0; 0 1]))
MvNormal{Float64, PSDMat{Float64, Matrix{Float64}}, Vector{Float64}}(
dim: 2
μ: [0.0, 0.0]
Σ: [0.0 0.0; 0.0 1.0]
)

julia> rand(x)
2-element Vector{Float64}:
 -1.045836214782949
  0.0

It looks like the output values are swapped.

knuesel commented 9 months ago

Should be fixed by https://github.com/invenia/PDMatsExtras.jl/pull/33

oxinabox commented 9 months ago

I believe this can now be closed as fixed in PDMatsExtras

knuesel commented 9 months ago

Thanks for the quick fix!