Repository: commons-math Updated Branches: refs/heads/master 51a9539c6 -> 4aa4c6d31
Made getKernel return a constant distribution for zero variance bins. JIRA: MATH-1203. Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/4aa4c6d3 Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/4aa4c6d3 Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/4aa4c6d3 Branch: refs/heads/master Commit: 4aa4c6d31f98d8cd80ccbcf99ded77bfc6de25c5 Parents: 51a9539 Author: Phil Steitz <[email protected]> Authored: Sun Mar 8 14:10:25 2015 -0700 Committer: Phil Steitz <[email protected]> Committed: Sun Mar 8 14:10:25 2015 -0700 ---------------------------------------------------------------------- src/changes/changes.xml | 3 +++ .../commons/math4/random/EmpiricalDistribution.java | 2 +- .../math4/random/EmpiricalDistributionTest.java | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-math/blob/4aa4c6d3/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 4e5eb4d..08ba014 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -54,6 +54,9 @@ If the output is not quite correct, check for invisible trailing spaces! </release> <release version="4.0" date="XXXX-XX-XX" description=""> + <action dev="psteitz" type="fix" issue="MATH-1203"> + EmpiricalDistribution getKernel fails for buckets with only multiple instances of the same value. + </action> <action dev="tn" type="update" issue="MATH-869"> "SpearmansCorrelation" will now throw an "MathIllegalArgumentException" if provided with a "NaturalRanking" instance that uses "REMOVED" as "NaNStrategy". http://git-wip-us.apache.org/repos/asf/commons-math/blob/4aa4c6d3/src/main/java/org/apache/commons/math4/random/EmpiricalDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/random/EmpiricalDistribution.java b/src/main/java/org/apache/commons/math4/random/EmpiricalDistribution.java index 9458289..3b3a864 100644 --- a/src/main/java/org/apache/commons/math4/random/EmpiricalDistribution.java +++ b/src/main/java/org/apache/commons/math4/random/EmpiricalDistribution.java @@ -799,7 +799,7 @@ public class EmpiricalDistribution extends AbstractRealDistribution { * @return within-bin kernel parameterized by bStats */ protected RealDistribution getKernel(SummaryStatistics bStats) { - if (bStats.getN() == 1) { + if (bStats.getN() == 1 || bStats.getVariance() == 0) { return new ConstantRealDistribution(bStats.getMean()); } else { return new NormalDistribution(randomData.getRandomGenerator(), http://git-wip-us.apache.org/repos/asf/commons-math/blob/4aa4c6d3/src/test/java/org/apache/commons/math4/random/EmpiricalDistributionTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/random/EmpiricalDistributionTest.java b/src/test/java/org/apache/commons/math4/random/EmpiricalDistributionTest.java index fd4bd5a..9290c07 100644 --- a/src/test/java/org/apache/commons/math4/random/EmpiricalDistributionTest.java +++ b/src/test/java/org/apache/commons/math4/random/EmpiricalDistributionTest.java @@ -431,6 +431,21 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes } /** + * MATH-1203 + */ + @Test + public void testNoBinVariance() { + final double[] data = {0, 0, 1, 1}; + EmpiricalDistribution dist = new EmpiricalDistribution(2); + dist.load(data); + dist.reseedRandomGenerator(1000); + for (int i = 0; i < 1000; i++) { + final double dev = dist.sample(); + Assert.assertTrue(dev == 0 || dev == 1); + } + } + + /** * Find the bin that x belongs (relative to {@link #makeDistribution()}). */ private int findBin(double x) {
