Repository: commons-math
Updated Branches:
  refs/heads/master 777af155a -> 612a04d6b


MATH-1421: Code refactoring.

Replaced unnecessary usage of "pow" function.
Reordered some operations.

It fixes the reported case where the lower bound was negative (see unit
test), but it might not be sufficient to avoid the problem in all cases.


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

Branch: refs/heads/master
Commit: 34adc606601cb578486d4a019b4655c5aff607b5
Parents: 777af15
Author: Gilles <er...@apache.org>
Authored: Thu Jun 22 03:03:15 2017 +0200
Committer: Gilles <er...@apache.org>
Committed: Thu Jun 22 03:03:15 2017 +0200

----------------------------------------------------------------------
 .../commons/math4/stat/interval/WilsonScoreInterval.java    | 9 +++++----
 .../math4/stat/interval/WilsonScoreIntervalTest.java        | 5 +++++
 2 files changed, 10 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/34adc606/src/main/java/org/apache/commons/math4/stat/interval/WilsonScoreInterval.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/math4/stat/interval/WilsonScoreInterval.java 
b/src/main/java/org/apache/commons/math4/stat/interval/WilsonScoreInterval.java
index bf683b1..f36bd3b 100644
--- 
a/src/main/java/org/apache/commons/math4/stat/interval/WilsonScoreInterval.java
+++ 
b/src/main/java/org/apache/commons/math4/stat/interval/WilsonScoreInterval.java
@@ -36,14 +36,15 @@ public class WilsonScoreInterval implements 
BinomialConfidenceInterval {
         final double alpha = (1.0 - confidenceLevel) / 2;
         final NormalDistribution normalDistribution = new NormalDistribution();
         final double z = normalDistribution.inverseCumulativeProbability(1 - 
alpha);
-        final double zSquared = FastMath.pow(z, 2);
+        final double zSquared = z * z;
+        final double zSquaredOverNumTrials = zSquared / numberOfTrials;
         final double mean = (double) numberOfSuccesses / (double) 
numberOfTrials;
 
-        final double factor = 1.0 / (1 + (1.0 / numberOfTrials) * zSquared);
-        final double modifiedSuccessRatio = mean + (1.0 / (2 * 
numberOfTrials)) * zSquared;
+        final double factor = 1.0 / (1 + zSquaredOverNumTrials);
+        final double modifiedSuccessRatio = mean + zSquaredOverNumTrials / 2;
         final double difference = z *
                                   FastMath.sqrt(1.0 / numberOfTrials * mean * 
(1 - mean) +
-                                                (1.0 / (4 * 
FastMath.pow(numberOfTrials, 2)) * zSquared));
+                                                (zSquaredOverNumTrials / (4 * 
numberOfTrials)));
 
         final double lowerBound = factor * (modifiedSuccessRatio - difference);
         final double upperBound = factor * (modifiedSuccessRatio + difference);

http://git-wip-us.apache.org/repos/asf/commons-math/blob/34adc606/src/test/java/org/apache/commons/math4/stat/interval/WilsonScoreIntervalTest.java
----------------------------------------------------------------------
diff --git 
a/src/test/java/org/apache/commons/math4/stat/interval/WilsonScoreIntervalTest.java
 
b/src/test/java/org/apache/commons/math4/stat/interval/WilsonScoreIntervalTest.java
index 2355cd2..22eef09 100644
--- 
a/src/test/java/org/apache/commons/math4/stat/interval/WilsonScoreIntervalTest.java
+++ 
b/src/test/java/org/apache/commons/math4/stat/interval/WilsonScoreIntervalTest.java
@@ -40,4 +40,9 @@ public class WilsonScoreIntervalTest extends 
BinomialConfidenceIntervalAbstractT
         Assert.assertEquals(0.1242664, confidenceInterval.getUpperBound(), 
1E-5);
     }
 
+    @Test
+    public void testMath1421() {
+        double lo = new WilsonScoreInterval().createInterval(19436, 0, 
0.95).getLowerBound();
+        Assert.assertTrue(lo + " < 0", lo >= 0);
+    }
 }

Reply via email to