MultivariateNormalDistribution extends AbstractMultivariateRealDistribution { public double density(final double[] vals) throws DimensionMismatchException { final int dim = getDimension(); if (vals.length != dim) { throw new DimensionMismatchException(vals.length, dim); } return FastMath.pow(2 * FastMath.PI, -0.5 * dim) * FastMath.pow(covarianceMatrixDeterminant, -0.5) * getExponentTerm(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 testDensities() { final double[] mu = { -1.5, 2 }; final double[][] sigma = { { 2, -1.1 }, { -1.1, 2 } }; final MultivariateNormalDistribution d = new MultivariateNormalDistribution(mu, sigma); final double[][] testValues = { { -1.5, 2 }, { 4, 4 }, { 1.5, -2 }, { 0, 0 } }; final double[] densities = new double[testValues.length]; for (int i = 0; i < densities.length; i++) { densities[i] = d.density(testValues[i]); } final double[] correctDensities = { 0.09528357207691344, 5.80932710124009e-09, 0.001387448895173267, 0.03309922090210541 }; for (int i = 0; i < testValues.length; i++) { Assert.assertEquals(correctDensities[i], densities[i], 1e-16); } }
@Test public void testUnivariateDistribution() { final double[] mu = { -1.5 }; final double[][] sigma = { { 1 } }; final MultivariateNormalDistribution multi = new MultivariateNormalDistribution(mu, sigma); final NormalDistribution uni = new NormalDistribution(mu[0], sigma[0][0]); final Random rng = new Random(); final int numCases = 100; final double tol = Math.ulp(1d); for (int i = 0; i < numCases; i++) { final double v = rng.nextDouble() * 10 - 5; Assert.assertEquals(uni.density(v), multi.density(new double[] { v }), tol); } }