commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject [math] Made getKernel return a constant distribution for zero variance bins. JIRA: MATH-1203.
Date Sun, 08 Mar 2015 22:11:12 GMT
Repository: commons-math
Updated Branches:
  refs/heads/MATH_3_X 9aa638267 -> 59912a072


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/59912a07
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/59912a07
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/59912a07

Branch: refs/heads/MATH_3_X
Commit: 59912a0724200beef63df2d74497165c3d1081bf
Parents: 9aa6382
Author: Phil Steitz <phil.steitz@gmail.com>
Authored: Sun Mar 8 15:11:03 2015 -0700
Committer: Phil Steitz <phil.steitz@gmail.com>
Committed: Sun Mar 8 15:11:03 2015 -0700

----------------------------------------------------------------------
 src/changes/changes.xml                              |  3 +++
 .../commons/math3/random/EmpiricalDistribution.java  |  2 +-
 .../math3/random/EmpiricalDistributionTest.java      | 15 +++++++++++++++
 3 files changed, 19 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/59912a07/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index b512dc8..03d6fa9 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="3.5" date="2015-01-11" 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="evanward" type="fix" issue="MATH-1204">
         "UnivariateSolverUtils#bracket(...)" sometimes failed to bracket
         if a reached the lower bound.

http://git-wip-us.apache.org/repos/asf/commons-math/blob/59912a07/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java
----------------------------------------------------------------------
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 3dd2d7f..fc27300 100644
--- a/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java
+++ b/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java
@@ -826,7 +826,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/59912a07/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java
----------------------------------------------------------------------
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 42e9a7f..580be9e 100644
--- a/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java
+++ b/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java
@@ -433,6 +433,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) {


Mime
View raw message