The routine may be called by the names g07aaf or nagf_univar_ci_binomial.
3Description
Given the number of trials, $n$, and the number of successes, $k$, this routine computes a $100(1-\alpha )\%$ confidence interval for $p$, the probability parameter of a binomial distribution with probability function,
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.
2.If $\mathrm{max}\phantom{\rule{0.125em}{0ex}}(k,n-k)\ge {10}^{6}$ and $\mathrm{min}\phantom{\rule{0.125em}{0ex}}(k,n-k)\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.
3.If $\mathrm{max}\phantom{\rule{0.125em}{0ex}}(k,n-k)>{10}^{6}$ and $\mathrm{min}\phantom{\rule{0.125em}{0ex}}(k,n-k)>1000$.
The binomial variate with parameters $n$ and $p$ is approximated by a Normal variate with mean $np$ and variance $np(1-p)$, 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).
4References
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
5Arguments
1: $\mathbf{n}$ – IntegerInput
On entry: $n$, the number of trials.
Constraint:
${\mathbf{n}}\ge 1$.
2: $\mathbf{k}$ – IntegerInput
On entry: $k$, the number of successes.
Constraint:
$0\le {\mathbf{k}}\le {\mathbf{n}}$.
3: $\mathbf{clevel}$ – Real (Kind=nag_wp)Input
On entry: the confidence level, $(1-\alpha )$, for two-sided interval estimate. For example ${\mathbf{clevel}}=0.95$ will give a $95\%$ confidence interval.
Constraint:
$0.0<{\mathbf{clevel}}<1.0$.
4: $\mathbf{pl}$ – Real (Kind=nag_wp)Output
On exit: the lower limit, ${p}_{l}$, of the confidence interval.
5: $\mathbf{pu}$ – Real (Kind=nag_wp)Output
On exit: the upper limit, ${p}_{u}$, of the confidence interval.
6: $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $\mathrm{-1}$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $\mathrm{-1}$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $\mathrm{-1}$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ 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).
6Error Indicators and Warnings
If on entry ${\mathbf{ifail}}=0$ or $\mathrm{-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{clevel}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: $0.0<{\mathbf{clevel}}<1.0$.
On entry, ${\mathbf{k}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{k}}\ge 0$.
On entry, ${\mathbf{n}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{n}}\ge 1$.
On entry, ${\mathbf{n}}=\u27e8\mathit{\text{value}}\u27e9$ and ${\mathbf{k}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{n}}\ge {\mathbf{k}}$.
${\mathbf{ifail}}=2$
When using the relationship with the gamma distribution the series to calculate the gamma probabilities has failed to converge. Both pl and pu are set to zero. This is an unlikely error exit.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please
contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.
7Accuracy
For most cases using the beta deviates the results should have a relative accuracy of $\mathrm{max}\phantom{\rule{0.125em}{0ex}}(\text{0.5E\u221212},50.0\times \epsilon )$ 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.
8Parallelism and Performance
Background information to multithreading can be found in the Multithreading documentation.
g07aaf is not threaded in any implementation.
9Further Comments
None.
10Example
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, non-smokers 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.