commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject [math] MATH-1356
Date Tue, 19 Apr 2016 11:27:59 GMT
Repository: commons-math
Updated Branches:
  refs/heads/feature-MATH-1158 3066a8085 -> 0880a21c5


MATH-1356

Handle special cases for which the computation would otherwise produce NaN.
Thanks to Thomas Lacroix for the report.


Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/0880a21c
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/0880a21c
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/0880a21c

Branch: refs/heads/feature-MATH-1158
Commit: 0880a21c56cec1a2442b5123c3845bfc99e83a7f
Parents: 3066a80
Author: Gilles <erans@apache.org>
Authored: Tue Apr 19 00:27:32 2016 +0200
Committer: Gilles <erans@apache.org>
Committed: Tue Apr 19 00:27:32 2016 +0200

----------------------------------------------------------------------
 .../distribution/SaddlePointExpansion.java      |  6 +++++
 .../HypergeometricDistributionTest.java         | 26 ++++++++++++++++++++
 2 files changed, 32 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/0880a21c/src/main/java/org/apache/commons/math4/distribution/SaddlePointExpansion.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/distribution/SaddlePointExpansion.java
b/src/main/java/org/apache/commons/math4/distribution/SaddlePointExpansion.java
index 01b0f65..aea0e3c 100644
--- a/src/main/java/org/apache/commons/math4/distribution/SaddlePointExpansion.java
+++ b/src/main/java/org/apache/commons/math4/distribution/SaddlePointExpansion.java
@@ -159,6 +159,9 @@ final class SaddlePointExpansion {
             }
             ret = s1;
         } else {
+            if (x == 0) {
+                return mu;
+            }
             ret = x * FastMath.log(x / mu) + mu - x;
         }
         return ret;
@@ -180,6 +183,9 @@ final class SaddlePointExpansion {
             if (p < 0.1) {
                 ret = -getDeviancePart(n, n * q) - n * p;
             } else {
+                if (n == 0) {
+                    return 0;
+                }
                 ret = n * FastMath.log(q);
             }
         } else if (x == n) {

http://git-wip-us.apache.org/repos/asf/commons-math/blob/0880a21c/src/test/java/org/apache/commons/math4/distribution/HypergeometricDistributionTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/distribution/HypergeometricDistributionTest.java
b/src/test/java/org/apache/commons/math4/distribution/HypergeometricDistributionTest.java
index e12c5b4..7e7ce8c 100644
--- a/src/test/java/org/apache/commons/math4/distribution/HypergeometricDistributionTest.java
+++ b/src/test/java/org/apache/commons/math4/distribution/HypergeometricDistributionTest.java
@@ -312,6 +312,32 @@ public class HypergeometricDistributionTest extends IntegerDistributionAbstractT
     }
 
     @Test
+    public void testZeroTrial() {
+        final int n = 11; // population
+        final int m = 4;  // successes in population
+        final int s = 0;  // number of trials
+
+        final HypergeometricDistribution dist = new HypergeometricDistribution(n, m, 0);
+
+        for (int i = 1; i <= n; i++) {
+            final double p = dist.probability(i);
+            Assert.assertEquals("p=" + p, 0, p, 0d);
+        }
+    }
+
+    @Test
+    public void testMath1356() {
+        final int n = 11;  // population
+        final int m = 11;  // successes in population
+
+        for (int s = 0; s <= n; s++) {
+            final HypergeometricDistribution dist = new HypergeometricDistribution(n, m,
s);
+            final double p = dist.probability(s);
+            Assert.assertEquals("p=" + p, 1, p, 0d);
+        }
+    }
+
+    @Test
     public void testMath1021() {
         final int N = 43130568;
         final int m = 42976365;


Mime
View raw message