hide long namesshow long names
hide short namesshow short names
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 parameter pp (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, nn, and the number of successes, kk, this function computes a 100(1α)%100(1-α)% confidence interval for pp, the probability parameter of a binomial distribution with probability function,
f(x) =
(n)
x
px(1p)nx,  x = 0,1,,n,
f(x)= n x px(1-p)n-x,  x=0,1,,n,
where αα is in the interval (0,1)(0,1).
Let the confidence interval be denoted by [pl,pupl,pu].
The point estimate for pp is = k / np^=k/n.
The lower and upper confidence limits plpl and pupu are estimated by the solutions to the equations;
n
(n)
x
plx(1pl)nx = α / 2,
x = k
x=kn n x plx (1-pl) n-x =α/2 ,
k
(n)
x
pux(1pu)nx = α / 2.
x = 0
x= 0k n x pux (1-pu) n-x =α /2 .
Three different methods are used depending on the number of trials, nn, and the number of successes, kk.
  1. If max (k,nk) < 106max(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,nk + 1,α / 2,
    pu = βk + 1,nk,1α / 2,
    pl = βk,n-k+1,α/2, pu = βk+1,n-k,1-α/2,
    where βa,b,δβa,b,δ is the deviate associated with the lower tail probability, δδ, of the beta distribution with parameters aa and bb. These beta deviates are computed using nag_stat_inv_cdf_beta (g01fe).
  2. If max (k,nk)106max(k,n-k)106 and min (k,nk) 1000 min(k,n-k) 1000 .
    The binomial variate with parameters nn and pp is approximated by a Poisson variate with mean npnp, see page 38 of Hastings and Peacock (1975).
    The relationship between the Poisson and χ2χ2-distributions (see page 112 of Hastings and Peacock (1975)) is used to derive the following equations;
    pl = 1/(2n)χ2k,α / 22,
    pu = 1/(2n)χ2k + 2,1α / 22,
    pl = 12n χ2k,α/22, pu = 12n χ2k+2,1-α/22,
    where χδ,ν2χδ,ν2 is the deviate associated with the lower tail probability, δδ, of the χ2χ2-distribution with νν degrees of freedom.
    In turn the relationship between the χ2χ2-distribution and the gamma distribution (see page 70 of Hastings and Peacock (1975)) yields the following equivalent equations;
    pl = 1/(2n)γk,2;α / 2,
    pu = 1/(2n)γk + 1,2;1α / 2,
    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 nag_stat_inv_cdf_gamma (g01ff).
  3. If max (k,nk) > 106max(k,n-k)>106 and min (k,nk) > 1000 min(k,n-k) > 1000 .
    The binomial variate with parameters nn and pp is approximated by a Normal variate with mean npnp and variance np(1p)np(1-p), see page 38 of Hastings and Peacock (1975).
    The approximate lower and upper confidence limits plpl and pupu are the solutions to the equations;
    (knpl)/(sqrt(npl(1pl))) = z1α / 2,
    (knpu)/(sqrt(npu(1pu))) = zα / 2,
    k-npl npl(1-pl) = z1-α/2, k-npu npu(1-pu) = zα/2,
    where zδzδ is the deviate associated with the lower tail probability, δδ, 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:     n – int64int32nag_int scalar
nn, the number of trials.
Constraint: n1n1.
2:     k – int64int32nag_int scalar
kk, the number of successes.
Constraint: 0kn0kn.
3:     clevel – double scalar
The confidence level, (1α)(1-α), for two-sided interval estimate. For example clevel = 0.95clevel=0.95 will give a 95%95% confidence interval.
Constraint: 0.0 < clevel < 1.00.0<clevel<1.0.

Optional Input Parameters

None.

Input Parameters Omitted from the MATLAB Interface

None.

Output Parameters

1:     pl – double scalar
The lower limit, plpl, of the confidence interval.
2:     pu – double scalar
The upper limit, pupu, of the confidence interval.
3:     ifail – int64int32nag_int scalar
ifail = 0ifail=0 unless the function detects an error (see [Error Indicators and Warnings]).

Error Indicators and Warnings

Errors or warnings detected by the function:
  ifail = 1ifail=1
On entry,n < 1n<1,
ork < 0k<0,
orn < kn<k,
orclevel0.0clevel0.0,
orclevel1.0clevel1.0.
  ifail = 2ifail=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.

Accuracy

For most cases using the beta deviates the results should have a relative accuracy of max (0.5e−12,50.0 × ε)max(0.5e−12,50.0×ε) where εε is the machine precision (see nag_machine_precision (x02aj)). Thus on machines with sufficiently high precision the results should be accurate to 1212 significant figures. Some accuracy may be lost when α / 2α/2 or 1α / 21-α/2 is very close to 0.00.0, which will occur if clevel is very close to 1.01.0. This should not affect the usual confidence levels used.
The approximations used when nn is large are accurate to at least 33 significant digits but usually to more.

Further Comments

None.

Example

function nag_univar_ci_binomial_example
n = int64(1067);
k = int64(117);
clevel = 0.95;
[pl, pu, ifail] = nag_univar_ci_binomial(n, k, clevel)
 

pl =

    0.0915


pu =

    0.1300


ifail =

                    0


function g07aa_example
n = int64(1067);
k = int64(117);
clevel = 0.95;
[pl, pu, ifail] = g07aa(n, k, clevel)
 

pl =

    0.0915


pu =

    0.1300


ifail =

                    0



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–2013