diff --git a/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java b/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java index c2323fc48..f5c920b61 100644 --- a/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java +++ b/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java @@ -637,7 +637,11 @@ public class EmpiricalDistribution extends AbstractRealDistribution { final double pB = pB(binIndex); final RealDistribution kernel = k(x); if (kernel instanceof ConstantRealDistribution) { - return pBminus + pB; + if (x < kernel.getNumericalMean()) { + return pBminus; + } else { + return pBminus + pB; + } } final double[] binBounds = getUpperBounds(); final double kB = kB(binIndex); diff --git a/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java b/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java index abc96f263..23dd34618 100644 --- a/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java +++ b/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java @@ -448,7 +448,7 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes Assert.assertEquals(0.5, dist.cumulativeProbability(0), Double.MIN_VALUE); Assert.assertEquals(1.0, dist.cumulativeProbability(1), Double.MIN_VALUE); Assert.assertEquals(0.5, dist.cumulativeProbability(0.5), Double.MIN_VALUE); - Assert.assertEquals(1.0, dist.cumulativeProbability(0.7), Double.MIN_VALUE); + Assert.assertEquals(0.5, dist.cumulativeProbability(0.7), Double.MIN_VALUE); } /** @@ -488,11 +488,11 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes Assert.assertTrue(Arrays.binarySearch(values, dist.sample()) >= 0); } final double tol = 10E-12; - Assert.assertEquals(0.2, dist.cumulativeProbability(1), tol); + Assert.assertEquals(0.0, dist.cumulativeProbability(1), tol); Assert.assertEquals(0.2, dist.cumulativeProbability(2), tol); - Assert.assertEquals(0.8, dist.cumulativeProbability(10), tol); + Assert.assertEquals(0.6, dist.cumulativeProbability(10), tol); Assert.assertEquals(0.8, dist.cumulativeProbability(12), tol); - Assert.assertEquals(1.0, dist.cumulativeProbability(13), tol); + Assert.assertEquals(0.8, dist.cumulativeProbability(13), tol); Assert.assertEquals(1.0, dist.cumulativeProbability(15), tol); Assert.assertEquals(2.0, dist.inverseCumulativeProbability(0.1), tol);