Fixed truncated value. Thanks to Piotr Wydrych.
Added unit test: comparing density values with univariate
normal distribution.


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1433367 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2013-01-15 12:15:50 +00:00
parent fea4dd914b
commit cedf0d27f9
3 changed files with 25 additions and 1 deletions

View File

@ -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.
">
<action dev="erans" type="fix" issue="MATH-929" due-to="Piotr Wydrych">
Fixed truncated value in "MultivariateNormalDistribution".
</action>
<action dev="erans" type="fix" issue="MATH-927" due-to="Dennis Hendriks">
Made "BitStreamGenerator" implement the "Serializable" interface.
</action>

View File

@ -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);
}

View File

@ -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);
}
}
}