Random multivariate normal distribution

2019-06-24 12:44发布

问题:

I've run into a problem where I have to be able to generate a set of randomly chosen numbers of a multivariate normal distribution with mean 0 and a given 3*3 variance-covariance matrix in Java.

Is there an easy way as to do this?

回答1:

1) Use a library implementation, as suggested by Dima.

Or, if you really feel a burning need to do this yourself:

2) Assuming you want to generate normals with a mean vector M and variance/covariance matrix V, perform Cholesky Decomposition on V to come up with lower triangular matrix L such that V=LLt (where the superscript t indicates transpose). Generate a vector Z of three independent standard normals (using Random.nextGaussian() to get the individual elements). Then LZ + M will have the desired multivariate normal distribution.



回答2:

Apache Commons has what you are looking for:

MultivariateNormalDistribution mnd = new MultivariateNormalDistribution(means, covariances);
double vals[] = mnd.sample();