4

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?

Mike G
  • 4,232
  • 9
  • 40
  • 66
Jonas Coussement
  • 382
  • 2
  • 15
  • In case you are looking for a library, probably apache commons math has something for cases like this. (Though in that case probably http://softwarerecs.stackexchange.com/ a better site to ask.) – Gábor Bakos Jan 07 '15 at 16:19

2 Answers2

4

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.

pjs
  • 18,696
  • 4
  • 27
  • 56
2

Apache Commons has what you are looking for:

MultivariateNormalDistribution mnd = new MultivariateNormalDistribution(means, covariances);
double vals[] = mnd.sample();
Dima
  • 39,570
  • 6
  • 44
  • 70