diff --git a/src/changes/changes.xml b/src/changes/changes.xml index a61254cfa..fb58f546a 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -55,6 +55,9 @@ This is a minor release: It combines bug fixes and new features. Changes to existing features were made in a backwards-compatible way such as to allow drop-in replacement of the v3.1[.1] JAR file. "> + + Fixed truncated value in "MultivariateNormalDistribution". + Made "BitStreamGenerator" implement the "Serializable" interface. diff --git a/src/main/java/org/apache/commons/math3/distribution/MultivariateNormalDistribution.java b/src/main/java/org/apache/commons/math3/distribution/MultivariateNormalDistribution.java index 15706811b..fd18c281b 100644 --- a/src/main/java/org/apache/commons/math3/distribution/MultivariateNormalDistribution.java +++ b/src/main/java/org/apache/commons/math3/distribution/MultivariateNormalDistribution.java @@ -180,7 +180,7 @@ public class MultivariateNormalDistribution throw new DimensionMismatchException(vals.length, dim); } - return FastMath.pow(2 * FastMath.PI, -dim / 2) * + return FastMath.pow(2 * FastMath.PI, -0.5 * dim) * FastMath.pow(covarianceMatrixDeterminant, -0.5) * getExponentTerm(vals); } diff --git a/src/test/java/org/apache/commons/math3/distribution/MultivariateNormalDistributionTest.java b/src/test/java/org/apache/commons/math3/distribution/MultivariateNormalDistributionTest.java index 756c009c6..96666187a 100644 --- a/src/test/java/org/apache/commons/math3/distribution/MultivariateNormalDistributionTest.java +++ b/src/test/java/org/apache/commons/math3/distribution/MultivariateNormalDistributionTest.java @@ -20,6 +20,7 @@ package org.apache.commons.math3.distribution; import org.apache.commons.math3.stat.correlation.Covariance; import org.apache.commons.math3.linear.RealMatrix; +import java.util.Random; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -130,4 +131,24 @@ public class MultivariateNormalDistributionTest { Assert.assertEquals(correctDensities[i], densities[i], 1e-16); } } + + /** + * Test the accuracy of the distribution when calculating densities. + */ + @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); + } + } }