001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math3.stat.interval;
018
019import org.apache.commons.math3.distribution.FDistribution;
020
021/**
022 * Implements the Clopper-Pearson method for creating a binomial proportion confidence interval.
023 *
024 * @see <a
025 *      href="http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Clopper-Pearson_interval">
026 *      Clopper-Pearson interval (Wikipedia)</a>
027 * @since 3.3
028 */
029public class ClopperPearsonInterval implements BinomialConfidenceInterval {
030
031    /** {@inheritDoc} */
032    public ConfidenceInterval createInterval(int numberOfTrials, int numberOfSuccesses,
033                                             double confidenceLevel) {
034        IntervalUtils.checkParameters(numberOfTrials, numberOfSuccesses, confidenceLevel);
035        double lowerBound = 0;
036        double upperBound = 0;
037        final double alpha = (1.0 - confidenceLevel) / 2.0;
038
039        final FDistribution distributionLowerBound = new FDistribution(2 * (numberOfTrials - numberOfSuccesses + 1),
040                                                                       2 * numberOfSuccesses);
041        final double fValueLowerBound = distributionLowerBound.inverseCumulativeProbability(1 - alpha);
042        if (numberOfSuccesses > 0) {
043            lowerBound = numberOfSuccesses /
044                         (numberOfSuccesses + (numberOfTrials - numberOfSuccesses + 1) * fValueLowerBound);
045        }
046
047        final FDistribution distributionUpperBound = new FDistribution(2 * (numberOfSuccesses + 1),
048                                                                       2 * (numberOfTrials - numberOfSuccesses));
049        final double fValueUpperBound = distributionUpperBound.inverseCumulativeProbability(1 - alpha);
050        if (numberOfSuccesses > 0) {
051            upperBound = (numberOfSuccesses + 1) * fValueUpperBound /
052                         (numberOfTrials - numberOfSuccesses + (numberOfSuccesses + 1) * fValueUpperBound);
053        }
054
055        return new ConfidenceInterval(lowerBound, upperBound, confidenceLevel);
056    }
057
058}