Avoid repeated computations.

This commit is contained in:
Gilles 2017-06-22 03:14:35 +02:00
parent 34adc60660
commit b05bd69cdd
1 changed files with 7 additions and 7 deletions

View File

@ -33,18 +33,18 @@ public class WilsonScoreInterval implements BinomialConfidenceInterval {
@Override
public ConfidenceInterval createInterval(int numberOfTrials, int numberOfSuccesses, double confidenceLevel) {
IntervalUtils.checkParameters(numberOfTrials, numberOfSuccesses, confidenceLevel);
final double alpha = (1.0 - confidenceLevel) / 2;
final double alpha = (1 - confidenceLevel) / 2;
final NormalDistribution normalDistribution = new NormalDistribution();
final double z = normalDistribution.inverseCumulativeProbability(1 - alpha);
final double zSquared = z * z;
final double zSquaredOverNumTrials = zSquared / numberOfTrials;
final double mean = (double) numberOfSuccesses / (double) numberOfTrials;
final double oneOverNumTrials = 1d / numberOfTrials;
final double zSquaredOverNumTrials = zSquared * oneOverNumTrials;
final double mean = oneOverNumTrials * numberOfSuccesses;
final double factor = 1.0 / (1 + zSquaredOverNumTrials);
final double factor = 1 / (1 + zSquaredOverNumTrials);
final double modifiedSuccessRatio = mean + zSquaredOverNumTrials / 2;
final double difference = z *
FastMath.sqrt(1.0 / numberOfTrials * mean * (1 - mean) +
(zSquaredOverNumTrials / (4 * numberOfTrials)));
final double difference = z * FastMath.sqrt(oneOverNumTrials * mean * (1 - mean) +
(oneOverNumTrials * zSquaredOverNumTrials / 4));
final double lowerBound = factor * (modifiedSuccessRatio - difference);
final double upperBound = factor * (modifiedSuccessRatio + difference);