JuliaStats / Distributions.jl

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

MvNormal undocumented constructor #1624

Open Nimrais opened 1 year ago

Nimrais commented 1 year ago

I found that there is an undocumented working constructor, MvNormal(x::Vector), that works strangely: it converts vector values to their absolute values and puts it on the diagonal and sets the mean vector to zero vector.

julia> MvNormal(zeros(2))
ZeroMeanDiagNormal(
dim: 2
μ: Zeros(2)
Σ: [0.0 0.0; 0.0 0.0]
)

julia> MvNormal([-1,0])
MvNormal{Int64, PDMats.PDiagMat{Int64, Vector{Int64}}, FillArrays.Zeros{Int64, 1, Tuple{Base.OneTo{Int64}}}}(
dim: 2
μ: Zeros(2)
Σ: [1 0; 0 0]
)

Moreover, it is possible to sample from these distributions:

julia> rand(MvNormal([0, 0]), 10)
2×10 Matrix{Float64}:
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0

 julia> rand(MvNormal([1, 0]), 10)
2×10 Matrix{Float64}:
 1.03324  -2.2997  0.140153  -0.878153  -0.461629  -0.425138  -1.10402  1.03676  -0.745389  1.05095
 0.0       0.0     0.0        0.0        0.0        0.0        0.0      0.0       0.0       0.0

Even more it a bit unintuitive that rand(MvNormal([1, 1]), 10_000), dims = 2) will give samples from zero centered distribution and not ones centered.

sethaxen commented 1 year ago

It's not documented because it's deprecated and will be removed: https://github.com/JuliaStats/Distributions.jl/blob/7831193ecbb70967b9f1af71e1c8f0850b89f3a5/src/multivariate/mvnormal.jl#L223

Nimrais commented 1 year ago

I see now! But not all use Julia with the deprecation flag: if it were possible to make it more visible, it would be nice. Thanks for the clarification.