1

Am I missing something obvious here? Math.NET has a wealth of probability distribution sampling classes but no multivariate normal distribution. It has Normal and MatrixNormal classes — is there an easy way of adopting either of those to sample a multivariate normal distribution defined by a mean vector and a covariance matrix?

Don Reba
  • 13,814
  • 3
  • 48
  • 61
  • I don't know anything in particular about Math.NET, but isn't the MatrixNormal intended to be the multivariate normal distribution? – Robert Dodier Jun 20 '21 at 22:31
  • @RobertDodier `MatrixNormal` is the [matrix normal distribution](https://en.wikipedia.org/wiki/Matrix_normal_distribution). It is defined by a matrix-valued mean, a row variance matrix, and a column variance matrix. Sampling it produces matrices. – Don Reba Jun 21 '21 at 23:04
  • 2
    Oh, okay. Well, why not take p = 1, then it's identical to an ordinary mvn, isn't it? But if you don't want to go down that road, note that Y = L . X has a mvn distribution with covariance L . transpose(L) = Sigma, where Sigma is positive definite, i.e. L is the Cholesky decomposition of the covariance, and X = (x_1, ..., x_n) where the x_k are all independent zero mean, unit variance normally distributed. Then you can just add the mean vector to Y. – Robert Dodier Jun 21 '21 at 23:35

1 Answers1

1

As per @robert-dodier's suggestion, the MatrixNormal distribution becomes the multivariate normal at p = 1. This is more verbose than if there were a native Multinormal distribution class, but not by much:

using MathNet.Numerics.LinearAlgebra;

Vector Sample<T>(System.Random random, Vector<T> mean, Matrix<T> cov)
{
    return MatrixNormal.Sample(
        random, mean.ToColumnMatrix(), cov, Matrix<T>.Build.DenseIdentity(1)).Column(0);

}

However, only positive-definite covariance matrices are allowed, since the distribution performs the Cholesky decomposition.

Don Reba
  • 13,814
  • 3
  • 48
  • 61