This is an automated email from the ASF dual-hosted git repository. erans pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-math.git
commit 3a5cf279bbeed85a169ba4480b6dea10cb7a7beb Author: Gilles Sadowski <gillese...@gmail.com> AuthorDate: Mon Sep 27 01:02:54 2021 +0200 MATH-1462: Extend workaround for bins that contain up to 3 values. --- .../math4/legacy/distribution/EmpiricalDistribution.java | 5 +++-- .../legacy/distribution/EmpiricalDistributionTest.java | 13 ++++++------- src/changes/changes.xml | 3 +++ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/EmpiricalDistribution.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/EmpiricalDistribution.java index 7929378..cca2c19 100644 --- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/EmpiricalDistribution.java +++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/EmpiricalDistribution.java @@ -409,7 +409,8 @@ public final class EmpiricalDistribution extends AbstractRealDistribution ++i; } - final ContinuousDistribution kernel = getKernel(binStats.get(i)); + final SummaryStatistics stats = binStats.get(i); + final ContinuousDistribution kernel = getKernel(stats); final double kB = kB(i); final double[] binBounds = getUpperBounds(); final double lower = i == 0 ? min : binBounds[i - 1]; @@ -546,7 +547,7 @@ public final class EmpiricalDistribution extends AbstractRealDistribution */ private static Function<SummaryStatistics, ContinuousDistribution> defaultKernel() { return stats -> { - if (stats.getN() <= 1 || + if (stats.getN() <= 3 || stats.getVariance() == 0) { return new ConstantContinuousDistribution(stats.getMean()); } else { diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/distribution/EmpiricalDistributionTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/distribution/EmpiricalDistributionTest.java index 4f26e4e..addd94a 100644 --- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/distribution/EmpiricalDistributionTest.java +++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/distribution/EmpiricalDistributionTest.java @@ -489,13 +489,12 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes 6212, 5961, 711 }; - final EmpiricalDistribution ed = EmpiricalDistribution.from(1000, data); + final double p = 0.32; + for (int i = 745; i <= 1100; i++) { + final EmpiricalDistribution ed = EmpiricalDistribution.from(i, data); + final double v = ed.inverseCumulativeProbability(p); - double v; - double p; - - p = 0.32; - v = ed.inverseCumulativeProbability(p); - Assert.assertTrue("p=" + p + " => v=" + v, Double.isFinite(v)); + Assert.assertTrue("p=" + p + " => v=" + v, Double.isFinite(v)); + } } } diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 595ebe8..5c276ce 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -97,6 +97,9 @@ Caveat: nightmare was one of the main reasons for creating more focused components.] "> + <action dev="erans" type="fix" issue="MATH-1462"> + "EmpiricalDistribution": Use constant kernel for bins that contain up to 3 values. + </action> <action dev="aherbert" due-to="Arturo Bernal" type="update"> Simplify assertions with simpler equivalent. </action>