NAG CL Interface
g07aac (ci_​binomial)

Settings help

CL Name Style:


1 Purpose

g07aac computes a confidence interval for the parameter p (the probability of a success) of a binomial distribution.

2 Specification

#include <nag.h>
void  g07aac (Integer n, Integer k, double clevel, double *pl, double *pu, NagError *fail)
The function may be called by the names: g07aac, nag_univar_ci_binomial or nag_binomial_ci.

3 Description

Given the number of trials, n, and the number of successes, k, this function computes a 100(1-α)% confidence interval for p, the probability parameter of a binomial distribution with probability function,
f(x)=( n x ) px(1-p)n-x,  x=0,1,,n,  
where α is in the interval (0,1).
Let the confidence interval be denoted by [pl,pu].
The point estimate for p is p^=k/n.
The lower and upper confidence limits pl and pu 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. 1.If max(k,n-k)<106.
    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 βa,b,δ is the deviate associated with the lower tail probability, δ, of the beta distribution with parameters a and b. These beta deviates are computed using g01fec.
  2. 2.If max(k,n-k)106 and min(k,n-k) 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 χ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 χδ,ν2 is the deviate associated with the lower tail probability, δ, of the χ2-distribution with ν degrees of freedom.
    In turn the relationship between the χ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 γα,β;δ is the deviate associated with the lower tail probability, δ, of the gamma distribution with shape parameter α and scale parameter β. These deviates are computed using g01ffc.
  3. 3.If max(k,n-k)>106 and min(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 pl and pu are the solutions to the equations;
    k-npl npl(1-pl) = z1-α/2, k-npu npu(1-pu) = zα/2,  
    where zδ is the deviate associated with the lower tail probability, δ, of the standard Normal distribution. These equations are solved using a quadratic equation solver .

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 Arguments

1: n Integer Input
On entry: n, the number of trials.
Constraint: n1.
2: k Integer Input
On entry: k, the number of successes.
Constraint: 0kn.
3: clevel double Input
On entry: the confidence level, (1-α), for two-sided interval estimate. For example clevel=0.95 will give a 95% confidence interval.
Constraint: 0.0<clevel<1.0.
4: pl double * Output
On exit: the lower limit, pl, of the confidence interval.
5: pu double * Output
On exit: the upper limit, pu, of the confidence interval.
6: fail NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

6 Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
NE_BAD_PARAM
On entry, argument value had an illegal value.
NE_CONVERGENCE
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.
NE_INT
On entry, k=value.
Constraint: k0.
On entry, n=value.
Constraint: n1.
NE_INT_2
On entry, n=value and k=value.
Constraint: nk.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_REAL
On entry, clevel=value.
Constraint: 0.0<clevel<1.0.

7 Accuracy

For most cases using the beta deviates the results should have a relative accuracy of max(0.5e−12,50.0×ε) where ε is the machine precision (see X02AJC). Thus on machines with sufficiently high precision the results should be accurate to 12 significant figures. Some accuracy may be lost when α/2 or 1-α/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.

8 Parallelism and Performance

g07aac is not threaded in any implementation.

9 Further Comments

None.

10 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.

10.1 Program Text

Program Text (g07aace.c)

10.2 Program Data

Program Data (g07aace.d)

10.3 Program Results

Program Results (g07aace.r)