diff --git a/pom.xml b/pom.xml index b26a13d34..8969cd5a6 100644 --- a/pom.xml +++ b/pom.xml @@ -150,6 +150,9 @@ Michael Bjorkegren + + Brian Bloniarz + John Bollinger diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 1bfbedb30..20ab55107 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -51,6 +51,9 @@ If the output is not quite correct, check for invisible trailing spaces! + + Fixed overflow in "HypergeometricDistribution". + Fixed "nextPermutation" method (in "o.a.c.m.random.RandomDataGenerator"). This bug does not affect applications using a previous version of diff --git a/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java b/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java index 27691272f..81e180cdc 100644 --- a/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java +++ b/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java @@ -265,7 +265,7 @@ public class HypergeometricDistribution extends AbstractIntegerDistribution { * size {@code n}, the mean is {@code n * m / N}. */ public double getNumericalMean() { - return (double) (getSampleSize() * getNumberOfSuccesses()) / (double) getPopulationSize(); + return getSampleSize() * (getNumberOfSuccesses() / (double) getPopulationSize()); } /** diff --git a/src/test/java/org/apache/commons/math3/distribution/HypergeometricDistributionTest.java b/src/test/java/org/apache/commons/math3/distribution/HypergeometricDistributionTest.java index 5598cd064..0893c3557 100644 --- a/src/test/java/org/apache/commons/math3/distribution/HypergeometricDistributionTest.java +++ b/src/test/java/org/apache/commons/math3/distribution/HypergeometricDistributionTest.java @@ -284,4 +284,18 @@ public class HypergeometricDistributionTest extends IntegerDistributionAbstractT double upper = 1.0 - dist.cumulativeProbability(k) + dist.probability(k); Assert.assertTrue(Precision.compareTo(1.0, upper, 1) == 0); } + + @Test + public void testMath1021() { + final int N = 43130568; + final int m = 42976365; + final int n = 50; + final HypergeometricDistribution dist = new HypergeometricDistribution(N, m, n); + + for (int i = 0; i < 100; i++) { + final int sample = dist.sample(); + Assert.assertTrue("sample=" + sample, 0 <= sample); + Assert.assertTrue("sample=" + sample, sample <= n); + } + } }