Distributions.jl icon indicating copy to clipboard operation
Distributions.jl copied to clipboard

Add documentation about the algorithm behind computing MvNormal samples.

Open ignace-computing opened this issue 4 years ago • 3 comments

In my very humble opinion, it would be useful to add some information about which algorithm is used for the generation of MvNormal samples. For instance, is Cholesky decomposition used internally? I acknowledge that there might exist good reasons not to add this kind of information to the docs,... not even sure if this is the right place to discuss this :).

Have a nice day.

ignace-computing avatar Sep 15 '21 12:09 ignace-computing

It depends :slightly_smiling_face:

MvNormal works with covariance matrices of type PDMats.AbstractPDMat from PDMats.jl. You can provide such a positive definite matrix type explicitly when constructing an MvNormal distribution, otherwise the default constructors will do it for you: general matrices will yield a covariance matrix of type PDMats.PDMat, diagonal matrices (of type Diagonal) a covariance matrix of type PDMats.PDiagMat, and scaled identity matrices (of type UniformScaling) or diagonal matrices with identical entries (of type Diagonal{...,<:FillArrays.Fill}) are converted to a PDMats.ScalMat. These different matrix types store different things internally, and indeed PDMats.PDMat computes and stores a Cholesky decomposition (you can also provide it if you have it precomputed). The other two matrix types only store the diagonal entries or the scaling factor, so no Cholesky decomposition is performed.

Sampling is based on generating standard normally distributed samples and then transforming them with PDMats.unwhiten! (see https://github.com/JuliaStats/PDMats.jl#common-interface) which adjusts the covariance . For all PDMats.PDMat, PDMats.PDiagMat and PDMats.ScalMat different optimized implementations of PDMats.unwhiten! exist, and of course only for PDMats.PDMat the stored Cholesky decomposition is used (https://github.com/JuliaStats/PDMats.jl/blob/213c37a2a94e7d14c21b54b2eeded4cb76270488/src/pdmat.jl#L68-L71).

devmotion avatar Sep 15 '21 12:09 devmotion

And here's the implementation in Distributions: https://github.com/JuliaStats/Distributions.jl/blob/ed52948fb47e7e0aecaf81e99ebb090c2738cf0e/src/multivariate/mvnormal.jl#L258-L267

devmotion avatar Sep 15 '21 12:09 devmotion

Agreed. But if you want you can add a reference to PDMATs in the docs, just similar to this one: https://juliastats.org/Distributions.jl/latest/multivariate/#Distributions.AbstractMvNormal

mschauer avatar Sep 15 '21 13:09 mschauer