From fc9d93db68a3c33e5ead8c7ecb187fdf0f06cc4f Mon Sep 17 00:00:00 2001 From: Gilles Date: Tue, 31 May 2016 02:38:16 +0200 Subject: [PATCH] Perform computation/allocation only if needed. Thanks to Dave Brosius. --- .../math4/stat/interval/ClopperPearsonInterval.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/apache/commons/math4/stat/interval/ClopperPearsonInterval.java b/src/main/java/org/apache/commons/math4/stat/interval/ClopperPearsonInterval.java index bf5d6ad6a..6f257e7e7 100644 --- a/src/main/java/org/apache/commons/math4/stat/interval/ClopperPearsonInterval.java +++ b/src/main/java/org/apache/commons/math4/stat/interval/ClopperPearsonInterval.java @@ -35,19 +35,18 @@ public class ClopperPearsonInterval implements BinomialConfidenceInterval { IntervalUtils.checkParameters(numberOfTrials, numberOfSuccesses, confidenceLevel); double lowerBound = 0; double upperBound = 0; - final double alpha = (1.0 - confidenceLevel) / 2.0; - final FDistribution distributionLowerBound = new FDistribution(2 * (numberOfTrials - numberOfSuccesses + 1), - 2 * numberOfSuccesses); if (numberOfSuccesses > 0) { + final double alpha = 0.5 * (1 - confidenceLevel); + + final FDistribution distributionLowerBound = new FDistribution(2 * (numberOfTrials - numberOfSuccesses + 1), + 2 * numberOfSuccesses); final double fValueLowerBound = distributionLowerBound.inverseCumulativeProbability(1 - alpha); lowerBound = numberOfSuccesses / (numberOfSuccesses + (numberOfTrials - numberOfSuccesses + 1) * fValueLowerBound); - } - final FDistribution distributionUpperBound = new FDistribution(2 * (numberOfSuccesses + 1), - 2 * (numberOfTrials - numberOfSuccesses)); - if (numberOfSuccesses > 0) { + final FDistribution distributionUpperBound = new FDistribution(2 * (numberOfSuccesses + 1), + 2 * (numberOfTrials - numberOfSuccesses)); final double fValueUpperBound = distributionUpperBound.inverseCumulativeProbability(1 - alpha); upperBound = (numberOfSuccesses + 1) * fValueUpperBound / (numberOfTrials - numberOfSuccesses + (numberOfSuccesses + 1) * fValueUpperBound);