You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2022/06/27 13:24:56 UTC

[commons-math] 02/05: MATH-1644: Prevent computed probability from exceeding 1.

This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch feature__MATH-1563__genetic_algorithm
in repository https://gitbox.apache.org/repos/asf/commons-math.git

commit 3c30a6d520a13e1f2548ee7414356c10ec6c92f4
Author: Gilles Sadowski <gi...@gmail.com>
AuthorDate: Sat Apr 9 14:07:03 2022 +0200

    MATH-1644: Prevent computed probability from exceeding 1.
---
 .../apache/commons/math4/legacy/stat/inference/BinomialTest.java   | 2 +-
 .../commons/math4/legacy/stat/inference/BinomialTestTest.java      | 7 +++++++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/inference/BinomialTest.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/inference/BinomialTest.java
index 80650c9b4..a7deb3187 100644
--- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/inference/BinomialTest.java
+++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/inference/BinomialTest.java
@@ -138,7 +138,7 @@ public class BinomialTest {
                     if (criticalValueLow == criticalValueHigh) {
                         pTotal += pLow;
                     } else {
-                        pTotal += 2 * pLow;
+                        pTotal += 2 * Math.nextDown(pLow);
                     }
                     criticalValueLow++;
                     criticalValueHigh--;
diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/inference/BinomialTestTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/inference/BinomialTestTest.java
index bf83dfa17..f480e0b66 100644
--- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/inference/BinomialTestTest.java
+++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/inference/BinomialTestTest.java
@@ -126,4 +126,11 @@ public class BinomialTestTest {
         Assert.assertFalse(testStatistic.binomialTest(trials, successes, probability, AlternativeHypothesis.GREATER_THAN, alpha01));
         Assert.assertFalse(testStatistic.binomialTest(trials, successes, probability, AlternativeHypothesis.LESS_THAN, alpha05));
     }
+
+    @Test
+    public void testMath1644() {
+        final BinomialTest bt = new BinomialTest();
+        final double pval = bt.binomialTest(10, 5, 0.5, AlternativeHypothesis.TWO_SIDED);
+        Assert.assertTrue("pval=" + pval, pval <= 1);
+    }
 }