NAG Library Routine Document
G07AAF
1 Purpose
G07AAF computes a confidence interval for the parameter $p$ (the probability of a success) of a binomial distribution.
2 Specification
INTEGER 
N, K, IFAIL 
REAL (KIND=nag_wp) 
CLEVEL, PL, PU 

3 Description
Given the number of trials,
$n$, and the number of successes,
$k$, this routine computes a
$100\left(1\alpha \right)\%$ confidence interval for
$p$, the probability parameter of a binomial distribution with probability function,
where
$\alpha $ is in the interval
$\left(0,1\right)$.
Let the confidence interval be denoted by [${p}_{l},{p}_{u}$].
The point estimate for $p$ is $\hat{p}=k/n$.
The lower and upper confidence limits
${p}_{l}$ and
${p}_{u}$ are estimated by the solutions to the equations;
Three different methods are used depending on the number of trials,
$n$, and the number of successes,
$k$.
 If $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(k,nk\right)<{10}^{6}$.
The relationship between the beta and binomial distributions (see page 38 of
Hastings and Peacock (1975)) is used to derive the equivalent equations,
where
${\beta}_{a,b,\delta}$ is the deviate associated with the lower tail probability,
$\delta $, of the beta distribution with parameters
$a$ and
$b$. These beta deviates are computed using
G01FEF.
 If $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(k,nk\right)\ge {10}^{6}$ and $\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(k,nk\right)\le 1000$.
The binomial variate with parameters
$n$ and
$p$ is approximated by a Poisson variate with mean
$np$, see page 38 of
Hastings and Peacock (1975).
The relationship between the Poisson and
${\chi}^{2}$distributions (see page 112 of
Hastings and Peacock (1975)) is used to derive the following equations;
where
${\chi}_{\delta ,\nu}^{2}$ is the deviate associated with the lower tail probability,
$\delta $, of the
${\chi}^{2}$distribution with
$\nu $ degrees of freedom.
In turn the relationship between the
${\chi}^{2}$distribution and the gamma distribution (see page 70 of
Hastings and Peacock (1975)) yields the following equivalent equations;
where
${\gamma}_{\alpha ,\beta \text{;}\delta}$ is the deviate associated with the lower tail probability,
$\delta $, of the gamma distribution with shape parameter
$\alpha $ and scale parameter
$\beta $. These deviates are computed using
G01FFF.
 If $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(k,nk\right)>{10}^{6}$ and $\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(k,nk\right)>1000$.
The binomial variate with parameters
$n$ and
$p$ is approximated by a Normal variate with mean
$np$ and variance
$np\left(1p\right)$, see page 38 of
Hastings and Peacock (1975).
The approximate lower and upper confidence limits
${p}_{l}$ and
${p}_{u}$ are the solutions to the equations;
where
${z}_{\delta}$ is the deviate associated with the lower tail probability,
$\delta $, of the standard Normal distribution. These equations are solved using a quadratic equation solver
(
C02AJF).
4 References
Hastings N A J and Peacock J B (1975) Statistical Distributions Butterworth
Snedecor G W and Cochran W G (1967) Statistical Methods Iowa State University Press
5 Parameters
 1: N – INTEGERInput
On entry: $n$, the number of trials.
Constraint:
${\mathbf{N}}\ge 1$.
 2: K – INTEGERInput
On entry: $k$, the number of successes.
Constraint:
$0\le {\mathbf{K}}\le {\mathbf{N}}$.
 3: CLEVEL – REAL (KIND=nag_wp)Input
On entry: the confidence level, $\left(1\alpha \right)$, for twosided interval estimate. For example ${\mathbf{CLEVEL}}=0.95$ will give a $95\%$ confidence interval.
Constraint:
$0.0<{\mathbf{CLEVEL}}<1.0$.
 4: PL – REAL (KIND=nag_wp)Output
On exit: the lower limit, ${p}_{l}$, of the confidence interval.
 5: PU – REAL (KIND=nag_wp)Output
On exit: the upper limit, ${p}_{u}$, of the confidence interval.
 6: IFAIL – INTEGERInput/Output

On entry:
IFAIL must be set to
$0$,
$1\text{ or}1$. If you are unfamiliar with this parameter you should refer to
Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value
$1\text{ or}1$ is recommended. If the output of error messages is undesirable, then the value
$1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is
$0$.
When the value $\mathbf{1}\text{ or}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit:
${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see
Section 6).
6 Error Indicators and Warnings
If on entry
${\mathbf{IFAIL}}={\mathbf{0}}$ or
${{\mathbf{1}}}$, explanatory error messages are output on the current error message unit (as defined by
X04AAF).
Errors or warnings detected by the routine:
 ${\mathbf{IFAIL}}=1$
On entry,  ${\mathbf{N}}<1$, 
or  ${\mathbf{K}}<0$, 
or  ${\mathbf{N}}<{\mathbf{K}}$, 
or  ${\mathbf{CLEVEL}}\le 0.0$, 
or  ${\mathbf{CLEVEL}}\ge 1.0$. 
 ${\mathbf{IFAIL}}=2$
When using the relationship with the gamma distribution to calculate one of the confidence limits, the series to calculate the gamma probabilities has failed to converge. Both
PL and
PU are set to zero. This is a very unlikely error exit and if it occurs please contact
NAG.
7 Accuracy
For most cases using the beta deviates the results should have a relative accuracy of
$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(\text{0.5E\u221212},50.0\times \epsilon \right)$ where
$\epsilon $ is the
machine precision (see
X02AJF). Thus on machines with sufficiently high precision the results should be accurate to
$12$ significant figures. Some accuracy may be lost when
$\alpha /2$ or
$1\alpha /2$ is very close to
$0.0$, which will occur if
CLEVEL is very close to
$1.0$. This should not affect the usual confidence levels used.
The approximations used when $n$ is large are accurate to at least $3$ significant digits but usually to more.
None.
9 Example
The following example program reads in the number of deaths recorded among male recipients of war pensions in a six year period following an initial questionnaire in 1956. We consider two classes, nonsmokers and those who reported that they smoked pipes only. The total number of males in each class is also read in. The data is taken from page 216 of
Snedecor and Cochran (1967). An estimate of the probability of a death in the six year period in each class is computed together with 95% confidence intervals for these estimates.
9.1 Program Text
Program Text (g07aafe.f90)
9.2 Program Data
Program Data (g07aafe.d)
9.3 Program Results
Program Results (g07aafe.r)