summaryrefslogtreecommitdiff
path: root/src/main/java/org/apache/commons/math3/stat/interval
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/apache/commons/math3/stat/interval')
-rw-r--r--src/main/java/org/apache/commons/math3/stat/interval/AgrestiCoullInterval.java48
-rw-r--r--src/main/java/org/apache/commons/math3/stat/interval/BinomialConfidenceInterval.java62
-rw-r--r--src/main/java/org/apache/commons/math3/stat/interval/ClopperPearsonInterval.java58
-rw-r--r--src/main/java/org/apache/commons/math3/stat/interval/ConfidenceInterval.java109
-rw-r--r--src/main/java/org/apache/commons/math3/stat/interval/IntervalUtils.java174
-rw-r--r--src/main/java/org/apache/commons/math3/stat/interval/NormalApproximationInterval.java44
-rw-r--r--src/main/java/org/apache/commons/math3/stat/interval/WilsonScoreInterval.java52
-rw-r--r--src/main/java/org/apache/commons/math3/stat/interval/package-info.java22
8 files changed, 569 insertions, 0 deletions
diff --git a/src/main/java/org/apache/commons/math3/stat/interval/AgrestiCoullInterval.java b/src/main/java/org/apache/commons/math3/stat/interval/AgrestiCoullInterval.java
new file mode 100644
index 0000000..b71c718
--- /dev/null
+++ b/src/main/java/org/apache/commons/math3/stat/interval/AgrestiCoullInterval.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.stat.interval;
+
+import org.apache.commons.math3.distribution.NormalDistribution;
+import org.apache.commons.math3.util.FastMath;
+
+/**
+ * Implements the Agresti-Coull method for creating a binomial proportion confidence interval.
+ *
+ * @see <a
+ * href="http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Agresti-Coull_Interval">
+ * Agresti-Coull interval (Wikipedia)</a>
+ * @since 3.3
+ */
+public class AgrestiCoullInterval implements BinomialConfidenceInterval {
+
+ /** {@inheritDoc} */
+ public ConfidenceInterval createInterval(int numberOfTrials, int numberOfSuccesses, double confidenceLevel) {
+ IntervalUtils.checkParameters(numberOfTrials, numberOfSuccesses, confidenceLevel);
+ 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 modifiedNumberOfTrials = numberOfTrials + zSquared;
+ final double modifiedSuccessesRatio = (1.0 / modifiedNumberOfTrials) * (numberOfSuccesses + 0.5 * zSquared);
+ final double difference = z *
+ FastMath.sqrt(1.0 / modifiedNumberOfTrials * modifiedSuccessesRatio *
+ (1 - modifiedSuccessesRatio));
+ return new ConfidenceInterval(modifiedSuccessesRatio - difference, modifiedSuccessesRatio + difference,
+ confidenceLevel);
+ }
+
+}
diff --git a/src/main/java/org/apache/commons/math3/stat/interval/BinomialConfidenceInterval.java b/src/main/java/org/apache/commons/math3/stat/interval/BinomialConfidenceInterval.java
new file mode 100644
index 0000000..532679a
--- /dev/null
+++ b/src/main/java/org/apache/commons/math3/stat/interval/BinomialConfidenceInterval.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.stat.interval;
+
+import org.apache.commons.math3.exception.NumberIsTooLargeException;
+import org.apache.commons.math3.exception.OutOfRangeException;
+import org.apache.commons.math3.exception.NotPositiveException;
+import org.apache.commons.math3.exception.NotStrictlyPositiveException;
+
+/**
+ * Interface to generate confidence intervals for a binomial proportion.
+ *
+ * @see <a
+ * href="http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval">Binomial
+ * proportion confidence interval (Wikipedia)</a>
+ * @since 3.3
+ */
+public interface BinomialConfidenceInterval {
+
+ /**
+ * Create a confidence interval for the true probability of success
+ * of an unknown binomial distribution with the given observed number
+ * of trials, successes and confidence level.
+ * <p>
+ * Preconditions:
+ * <ul>
+ * <li>{@code numberOfTrials} must be positive</li>
+ * <li>{@code numberOfSuccesses} may not exceed {@code numberOfTrials}</li>
+ * <li>{@code confidenceLevel} must be strictly between 0 and 1 (exclusive)</li>
+ * </ul>
+ * </p>
+ *
+ * @param numberOfTrials number of trials
+ * @param numberOfSuccesses number of successes
+ * @param confidenceLevel desired probability that the true probability of
+ * success falls within the returned interval
+ * @return Confidence interval containing the probability of success with
+ * probability {@code confidenceLevel}
+ * @throws NotStrictlyPositiveException if {@code numberOfTrials <= 0}.
+ * @throws NotPositiveException if {@code numberOfSuccesses < 0}.
+ * @throws NumberIsTooLargeException if {@code numberOfSuccesses > numberOfTrials}.
+ * @throws OutOfRangeException if {@code confidenceLevel} is not in the interval {@code (0, 1)}.
+ */
+ ConfidenceInterval createInterval(int numberOfTrials, int numberOfSuccesses, double confidenceLevel)
+ throws NotStrictlyPositiveException, NotPositiveException,
+ NumberIsTooLargeException, OutOfRangeException;
+
+}
diff --git a/src/main/java/org/apache/commons/math3/stat/interval/ClopperPearsonInterval.java b/src/main/java/org/apache/commons/math3/stat/interval/ClopperPearsonInterval.java
new file mode 100644
index 0000000..34a0d57
--- /dev/null
+++ b/src/main/java/org/apache/commons/math3/stat/interval/ClopperPearsonInterval.java
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.stat.interval;
+
+import org.apache.commons.math3.distribution.FDistribution;
+
+/**
+ * Implements the Clopper-Pearson method for creating a binomial proportion confidence interval.
+ *
+ * @see <a
+ * href="http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Clopper-Pearson_interval">
+ * Clopper-Pearson interval (Wikipedia)</a>
+ * @since 3.3
+ */
+public class ClopperPearsonInterval implements BinomialConfidenceInterval {
+
+ /** {@inheritDoc} */
+ public ConfidenceInterval createInterval(int numberOfTrials, int numberOfSuccesses,
+ double confidenceLevel) {
+ 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);
+ final double fValueLowerBound = distributionLowerBound.inverseCumulativeProbability(1 - alpha);
+ if (numberOfSuccesses > 0) {
+ lowerBound = numberOfSuccesses /
+ (numberOfSuccesses + (numberOfTrials - numberOfSuccesses + 1) * fValueLowerBound);
+ }
+
+ final FDistribution distributionUpperBound = new FDistribution(2 * (numberOfSuccesses + 1),
+ 2 * (numberOfTrials - numberOfSuccesses));
+ final double fValueUpperBound = distributionUpperBound.inverseCumulativeProbability(1 - alpha);
+ if (numberOfSuccesses > 0) {
+ upperBound = (numberOfSuccesses + 1) * fValueUpperBound /
+ (numberOfTrials - numberOfSuccesses + (numberOfSuccesses + 1) * fValueUpperBound);
+ }
+
+ return new ConfidenceInterval(lowerBound, upperBound, confidenceLevel);
+ }
+
+}
diff --git a/src/main/java/org/apache/commons/math3/stat/interval/ConfidenceInterval.java b/src/main/java/org/apache/commons/math3/stat/interval/ConfidenceInterval.java
new file mode 100644
index 0000000..0147c8c
--- /dev/null
+++ b/src/main/java/org/apache/commons/math3/stat/interval/ConfidenceInterval.java
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.stat.interval;
+
+import org.apache.commons.math3.exception.MathIllegalArgumentException;
+import org.apache.commons.math3.exception.util.LocalizedFormats;
+
+/**
+ * Represents an interval estimate of a population parameter.
+ *
+ * @since 3.3
+ */
+public class ConfidenceInterval {
+
+ /** Lower endpoint of the interval */
+ private double lowerBound;
+
+ /** Upper endpoint of the interval */
+ private double upperBound;
+
+ /**
+ * The asserted probability that the interval contains the population
+ * parameter
+ */
+ private double confidenceLevel;
+
+ /**
+ * Create a confidence interval with the given bounds and confidence level.
+ * <p>
+ * Preconditions:
+ * <ul>
+ * <li>{@code lower} must be strictly less than {@code upper}</li>
+ * <li>{@code confidenceLevel} must be strictly between 0 and 1 (exclusive)</li>
+ * </ul>
+ * </p>
+ *
+ * @param lowerBound lower endpoint of the interval
+ * @param upperBound upper endpoint of the interval
+ * @param confidenceLevel coverage probability
+ * @throws MathIllegalArgumentException if the preconditions are not met
+ */
+ public ConfidenceInterval(double lowerBound, double upperBound, double confidenceLevel) {
+ checkParameters(lowerBound, upperBound, confidenceLevel);
+ this.lowerBound = lowerBound;
+ this.upperBound = upperBound;
+ this.confidenceLevel = confidenceLevel;
+ }
+
+ /**
+ * @return the lower endpoint of the interval
+ */
+ public double getLowerBound() {
+ return lowerBound;
+ }
+
+ /**
+ * @return the upper endpoint of the interval
+ */
+ public double getUpperBound() {
+ return upperBound;
+ }
+
+ /**
+ * @return the asserted probability that the interval contains the
+ * population parameter
+ */
+ public double getConfidenceLevel() {
+ return confidenceLevel;
+ }
+
+ /**
+ * @return String representation of the confidence interval
+ */
+ @Override
+ public String toString() {
+ return "[" + lowerBound + ";" + upperBound + "] (confidence level:" + confidenceLevel + ")";
+ }
+
+ /**
+ * Verifies that (lower, upper) is a valid non-empty interval and confidence
+ * is strictly between 0 and 1.
+ *
+ * @param lower lower endpoint
+ * @param upper upper endpoint
+ * @param confidence confidence level
+ */
+ private void checkParameters(double lower, double upper, double confidence) {
+ if (lower >= upper) {
+ throw new MathIllegalArgumentException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper);
+ }
+ if (confidence <= 0 || confidence >= 1) {
+ throw new MathIllegalArgumentException(LocalizedFormats.OUT_OF_BOUNDS_CONFIDENCE_LEVEL, confidence, 0, 1);
+ }
+ }
+}
diff --git a/src/main/java/org/apache/commons/math3/stat/interval/IntervalUtils.java b/src/main/java/org/apache/commons/math3/stat/interval/IntervalUtils.java
new file mode 100644
index 0000000..0613c99
--- /dev/null
+++ b/src/main/java/org/apache/commons/math3/stat/interval/IntervalUtils.java
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.stat.interval;
+
+import org.apache.commons.math3.exception.NotPositiveException;
+import org.apache.commons.math3.exception.NotStrictlyPositiveException;
+import org.apache.commons.math3.exception.NumberIsTooLargeException;
+import org.apache.commons.math3.exception.OutOfRangeException;
+import org.apache.commons.math3.exception.util.LocalizedFormats;
+
+/**
+ * Factory methods to generate confidence intervals for a binomial proportion.
+ * The supported methods are:
+ * <ul>
+ * <li>Agresti-Coull interval</li>
+ * <li>Clopper-Pearson method (exact method)</li>
+ * <li>Normal approximation (based on central limit theorem)</li>
+ * <li>Wilson score interval</li>
+ * </ul>
+ *
+ * @since 3.3
+ */
+public final class IntervalUtils {
+
+ /** Singleton Agresti-Coull instance. */
+ private static final BinomialConfidenceInterval AGRESTI_COULL = new AgrestiCoullInterval();
+
+ /** Singleton Clopper-Pearson instance. */
+ private static final BinomialConfidenceInterval CLOPPER_PEARSON = new ClopperPearsonInterval();
+
+ /** Singleton NormalApproximation instance. */
+ private static final BinomialConfidenceInterval NORMAL_APPROXIMATION = new NormalApproximationInterval();
+
+ /** Singleton Wilson score instance. */
+ private static final BinomialConfidenceInterval WILSON_SCORE = new WilsonScoreInterval();
+
+ /**
+ * Prevent instantiation.
+ */
+ private IntervalUtils() {
+ }
+
+ /**
+ * Create an Agresti-Coull binomial confidence interval for the true
+ * probability of success of an unknown binomial distribution with the given
+ * observed number of trials, successes and confidence level.
+ *
+ * @param numberOfTrials number of trials
+ * @param numberOfSuccesses number of successes
+ * @param confidenceLevel desired probability that the true probability of
+ * success falls within the returned interval
+ * @return Confidence interval containing the probability of success with
+ * probability {@code confidenceLevel}
+ * @throws NotStrictlyPositiveException if {@code numberOfTrials <= 0}.
+ * @throws NotPositiveException if {@code numberOfSuccesses < 0}.
+ * @throws NumberIsTooLargeException if {@code numberOfSuccesses > numberOfTrials}.
+ * @throws OutOfRangeException if {@code confidenceLevel} is not in the interval {@code (0, 1)}.
+ */
+ public static ConfidenceInterval getAgrestiCoullInterval(int numberOfTrials, int numberOfSuccesses,
+ double confidenceLevel) {
+ return AGRESTI_COULL.createInterval(numberOfTrials, numberOfSuccesses, confidenceLevel);
+ }
+
+ /**
+ * Create a Clopper-Pearson binomial confidence interval for the true
+ * probability of success of an unknown binomial distribution with the given
+ * observed number of trials, successes and confidence level.
+ * <p>
+ * Preconditions:
+ * <ul>
+ * <li>{@code numberOfTrials} must be positive</li>
+ * <li>{@code numberOfSuccesses} may not exceed {@code numberOfTrials}</li>
+ * <li>{@code confidenceLevel} must be strictly between 0 and 1 (exclusive)</li>
+ * </ul>
+ * </p>
+ *
+ * @param numberOfTrials number of trials
+ * @param numberOfSuccesses number of successes
+ * @param confidenceLevel desired probability that the true probability of
+ * success falls within the returned interval
+ * @return Confidence interval containing the probability of success with
+ * probability {@code confidenceLevel}
+ * @throws NotStrictlyPositiveException if {@code numberOfTrials <= 0}.
+ * @throws NotPositiveException if {@code numberOfSuccesses < 0}.
+ * @throws NumberIsTooLargeException if {@code numberOfSuccesses > numberOfTrials}.
+ * @throws OutOfRangeException if {@code confidenceLevel} is not in the interval {@code (0, 1)}.
+ */
+ public static ConfidenceInterval getClopperPearsonInterval(int numberOfTrials, int numberOfSuccesses,
+ double confidenceLevel) {
+ return CLOPPER_PEARSON.createInterval(numberOfTrials, numberOfSuccesses, confidenceLevel);
+ }
+
+ /**
+ * Create a binomial confidence interval for the true probability of success
+ * of an unknown binomial distribution with the given observed number of
+ * trials, successes and confidence level using the Normal approximation to
+ * the binomial distribution.
+ *
+ * @param numberOfTrials number of trials
+ * @param numberOfSuccesses number of successes
+ * @param confidenceLevel desired probability that the true probability of
+ * success falls within the interval
+ * @return Confidence interval containing the probability of success with
+ * probability {@code confidenceLevel}
+ */
+ public static ConfidenceInterval getNormalApproximationInterval(int numberOfTrials, int numberOfSuccesses,
+ double confidenceLevel) {
+ return NORMAL_APPROXIMATION.createInterval(numberOfTrials, numberOfSuccesses, confidenceLevel);
+ }
+
+ /**
+ * Create a Wilson score binomial confidence interval for the true
+ * probability of success of an unknown binomial distribution with the given
+ * observed number of trials, successes and confidence level.
+ *
+ * @param numberOfTrials number of trials
+ * @param numberOfSuccesses number of successes
+ * @param confidenceLevel desired probability that the true probability of
+ * success falls within the returned interval
+ * @return Confidence interval containing the probability of success with
+ * probability {@code confidenceLevel}
+ * @throws NotStrictlyPositiveException if {@code numberOfTrials <= 0}.
+ * @throws NotPositiveException if {@code numberOfSuccesses < 0}.
+ * @throws NumberIsTooLargeException if {@code numberOfSuccesses > numberOfTrials}.
+ * @throws OutOfRangeException if {@code confidenceLevel} is not in the interval {@code (0, 1)}.
+ */
+ public static ConfidenceInterval getWilsonScoreInterval(int numberOfTrials, int numberOfSuccesses,
+ double confidenceLevel) {
+ return WILSON_SCORE.createInterval(numberOfTrials, numberOfSuccesses, confidenceLevel);
+ }
+
+ /**
+ * Verifies that parameters satisfy preconditions.
+ *
+ * @param numberOfTrials number of trials (must be positive)
+ * @param numberOfSuccesses number of successes (must not exceed numberOfTrials)
+ * @param confidenceLevel confidence level (must be strictly between 0 and 1)
+ * @throws NotStrictlyPositiveException if {@code numberOfTrials <= 0}.
+ * @throws NotPositiveException if {@code numberOfSuccesses < 0}.
+ * @throws NumberIsTooLargeException if {@code numberOfSuccesses > numberOfTrials}.
+ * @throws OutOfRangeException if {@code confidenceLevel} is not in the interval {@code (0, 1)}.
+ */
+ static void checkParameters(int numberOfTrials, int numberOfSuccesses, double confidenceLevel) {
+ if (numberOfTrials <= 0) {
+ throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_TRIALS, numberOfTrials);
+ }
+ if (numberOfSuccesses < 0) {
+ throw new NotPositiveException(LocalizedFormats.NEGATIVE_NUMBER_OF_SUCCESSES, numberOfSuccesses);
+ }
+ if (numberOfSuccesses > numberOfTrials) {
+ throw new NumberIsTooLargeException(LocalizedFormats.NUMBER_OF_SUCCESS_LARGER_THAN_POPULATION_SIZE,
+ numberOfSuccesses, numberOfTrials, true);
+ }
+ if (confidenceLevel <= 0 || confidenceLevel >= 1) {
+ throw new OutOfRangeException(LocalizedFormats.OUT_OF_BOUNDS_CONFIDENCE_LEVEL,
+ confidenceLevel, 0, 1);
+ }
+ }
+
+}
diff --git a/src/main/java/org/apache/commons/math3/stat/interval/NormalApproximationInterval.java b/src/main/java/org/apache/commons/math3/stat/interval/NormalApproximationInterval.java
new file mode 100644
index 0000000..25a213a
--- /dev/null
+++ b/src/main/java/org/apache/commons/math3/stat/interval/NormalApproximationInterval.java
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.stat.interval;
+
+import org.apache.commons.math3.distribution.NormalDistribution;
+import org.apache.commons.math3.util.FastMath;
+
+/**
+ * Implements the normal approximation method for creating a binomial proportion confidence interval.
+ *
+ * @see <a
+ * href="http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Normal_approximation_interval">
+ * Normal approximation interval (Wikipedia)</a>
+ * @since 3.3
+ */
+public class NormalApproximationInterval implements BinomialConfidenceInterval {
+
+ /** {@inheritDoc} */
+ public ConfidenceInterval createInterval(int numberOfTrials, int numberOfSuccesses,
+ double confidenceLevel) {
+ IntervalUtils.checkParameters(numberOfTrials, numberOfSuccesses, confidenceLevel);
+ final double mean = (double) numberOfSuccesses / (double) numberOfTrials;
+ final double alpha = (1.0 - confidenceLevel) / 2;
+ final NormalDistribution normalDistribution = new NormalDistribution();
+ final double difference = normalDistribution.inverseCumulativeProbability(1 - alpha) *
+ FastMath.sqrt(1.0 / numberOfTrials * mean * (1 - mean));
+ return new ConfidenceInterval(mean - difference, mean + difference, confidenceLevel);
+ }
+
+}
diff --git a/src/main/java/org/apache/commons/math3/stat/interval/WilsonScoreInterval.java b/src/main/java/org/apache/commons/math3/stat/interval/WilsonScoreInterval.java
new file mode 100644
index 0000000..9932835
--- /dev/null
+++ b/src/main/java/org/apache/commons/math3/stat/interval/WilsonScoreInterval.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.stat.interval;
+
+import org.apache.commons.math3.distribution.NormalDistribution;
+import org.apache.commons.math3.util.FastMath;
+
+/**
+ * Implements the Wilson score method for creating a binomial proportion confidence interval.
+ *
+ * @see <a
+ * href="http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Wilson_score_interval">
+ * Wilson score interval (Wikipedia)</a>
+ * @since 3.3
+ */
+public class WilsonScoreInterval implements BinomialConfidenceInterval {
+
+ /** {@inheritDoc} */
+ public ConfidenceInterval createInterval(int numberOfTrials, int numberOfSuccesses, double confidenceLevel) {
+ IntervalUtils.checkParameters(numberOfTrials, numberOfSuccesses, confidenceLevel);
+ 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 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 difference = z *
+ FastMath.sqrt(1.0 / numberOfTrials * mean * (1 - mean) +
+ (1.0 / (4 * FastMath.pow(numberOfTrials, 2)) * zSquared));
+
+ final double lowerBound = factor * (modifiedSuccessRatio - difference);
+ final double upperBound = factor * (modifiedSuccessRatio + difference);
+ return new ConfidenceInterval(lowerBound, upperBound, confidenceLevel);
+ }
+
+}
diff --git a/src/main/java/org/apache/commons/math3/stat/interval/package-info.java b/src/main/java/org/apache/commons/math3/stat/interval/package-info.java
new file mode 100644
index 0000000..34f43d9
--- /dev/null
+++ b/src/main/java/org/apache/commons/math3/stat/interval/package-info.java
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+ *
+ * Classes providing binomial proportion confidence interval construction.
+ *
+ */
+package org.apache.commons.math3.stat.interval;