MultivariateNormalDistribution extends AbstractMultivariateRealDistribution { @Override public double[] sample() { final int dim = getDimension(); final double[] normalVals = new double[dim]; for (int i = 0; i < dim; i++) { normalVals[i] = random.nextGaussian(); } final double[] vals = samplingMatrix.operate(normalVals); for (int i = 0; i < dim; i++) { vals[i] += means[i]; } return vals; } MultivariateNormalDistribution(final double[] means,
                                          final double[][] covariances);  MultivariateNormalDistribution(RandomGenerator rng,
                                          final double[] means,
                                          final double[][] covariances); double[] getMeans(); RealMatrix getCovariances(); double density(final double[] vals); double[] getStandardDeviations(); @Override double[] sample();  }
@Test public void testSampling() { final double[] mu = { -1.5, 2 }; final double[][] sigma = { { 2, -1.1 }, { -1.1, 2 } }; final MultivariateNormalDistribution d = new MultivariateNormalDistribution(mu, sigma); d.reseedRandomGenerator(50); final int n = 500000; final double[][] samples = d.sample(n); final int dim = d.getDimension(); final double[] sampleMeans = new double[dim]; for (int i = 0; i < samples.length; i++) { for (int j = 0; j < dim; j++) { sampleMeans[j] += samples[i][j]; } } final double sampledValueTolerance = 1e-2; for (int j = 0; j < dim; j++) { sampleMeans[j] /= samples.length; Assert.assertEquals(mu[j], sampleMeans[j], sampledValueTolerance); } final double[][] sampleSigma = new Covariance(samples).getCovarianceMatrix().getData(); for (int i = 0; i < dim; i++) { for (int j = 0; j < dim; j++) { Assert.assertEquals(sigma[i][j], sampleSigma[i][j], sampledValueTolerance); } } }