Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_univar_ci_binomial (g07aa)

Purpose

nag_univar_ci_binomial (g07aa) computes a confidence interval for the argument $p$ (the probability of a success) of a binomial distribution.

Syntax

[pl, pu, ifail] = g07aa(n, k, clevel)
[pl, pu, ifail] = nag_univar_ci_binomial(n, k, clevel)

Description

Given the number of trials, $n$, and the number of successes, $k$, this function computes a $100\left(1-\alpha \right)%$ confidence interval for $p$, the probability argument of a binomial distribution with probability function,
 $fx= n x px1-pn-x, x=0,1,…,n,$
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 $\stackrel{^}{p}=k/n$.
The lower and upper confidence limits ${p}_{l}$ and ${p}_{u}$ are estimated by the solutions to the equations;
 $∑x=kn n x plx 1-pl n-x =α/2 ,$
 $∑x= 0k n x pux 1-pu n-x =α /2 .$
Three different methods are used depending on the number of trials, $n$, and the number of successes, $k$.
1. If $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(k,n-k\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,
 $pl = βk,n-k+1,α/2, pu = βk+1,n-k,1-α/2,$
where ${\beta }_{a,b,\delta }$ is the deviate associated with the lower tail probability, $\delta$, of the beta distribution with arguments $a$ and $b$. These beta deviates are computed using nag_stat_inv_cdf_beta (g01fe).
2. If $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(k,n-k\right)\ge {10}^{6}$ and $\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(k,n-k\right)\le 1000$.
The binomial variate with arguments $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;
 $pl = 12n χ2k,α/22, pu = 12n χ2k+2,1-α/22,$
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;
 $pl = 12n γk,2;α/2, pu = 12n γk+1,2;1-α/2,$
where ${\gamma }_{\alpha ,\beta \text{;}\delta }$ is the deviate associated with the lower tail probability, $\delta$, of the gamma distribution with shape argument $\alpha$ and scale argument $\beta$. These deviates are computed using nag_stat_inv_cdf_gamma (g01ff).
3. If $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(k,n-k\right)>{10}^{6}$ and $\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(k,n-k\right)>1000$.
The binomial variate with arguments $n$ and $p$ is approximated by a Normal variate with mean $np$ and variance $np\left(1-p\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;
 $k-npl npl1-pl = z1-α/2, k-npu npu1-pu = zα/2,$
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 (nag_zeros_quadratic_real (c02aj)).

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

Parameters

Compulsory Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
$n$, the number of trials.
Constraint: ${\mathbf{n}}\ge 1$.
2:     $\mathrm{k}$int64int32nag_int scalar
$k$, the number of successes.
Constraint: $0\le {\mathbf{k}}\le {\mathbf{n}}$.
3:     $\mathrm{clevel}$ – double scalar
The confidence level, $\left(1-\alpha \right)$, for two-sided interval estimate. For example ${\mathbf{clevel}}=0.95$ will give a $95%$ confidence interval.
Constraint: $0.0<{\mathbf{clevel}}<1.0$.

None.

Output Parameters

1:     $\mathrm{pl}$ – double scalar
The lower limit, ${p}_{l}$, of the confidence interval.
2:     $\mathrm{pu}$ – double scalar
The upper limit, ${p}_{u}$, of the confidence interval.
3:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

Error Indicators and Warnings

Errors or warnings detected by the function:
${\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.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please contact NAG.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

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−12},50.0×\epsilon \right)$ where $\epsilon$ is the machine precision (see nag_machine_precision (x02aj)). 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.

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, 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.
```function g07aa_example

fprintf('g07aa example results\n\n');

n = int64([1067, 402]);
k = int64([117,  54]);
clevel = 0.95;

disp(' Probability    Confidence Interval ');
for j = 1:numel(n)
[pl, pu, ifail] = g07aa(n(j), k(j), clevel);
p_hat = double(k(j))/double(n(j));
fprintf('%10.4f      (%7.4f, %7.4f)\n', p_hat, pl, pu);
end

```
```g07aa example results

Probability    Confidence Interval
0.1097      ( 0.0915,  0.1300)
0.1343      ( 0.1025,  0.1716)
```

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015