NAG Library Routine Document

g01bjf (prob_binomial)


g01bjf returns the lower tail, upper tail and point probabilities associated with a binomial distribution.


Fortran Interface
Subroutine g01bjf ( n, p, k, plek, pgtk, peqk, ifail)
Integer, Intent (In):: n, k
Integer, Intent (Inout):: ifail
Real (Kind=nag_wp), Intent (In):: p
Real (Kind=nag_wp), Intent (Out):: plek, pgtk, peqk
C Header Interface
#include <nagmk26.h>
void  g01bjf_ (const Integer *n, const double *p, const Integer *k, double *plek, double *pgtk, double *peqk, Integer *ifail)


Let X denote a random variable having a binomial distribution with parameters n and p (n0 and 0<p<1). Then
ProbX=k= n k pk1-pn-k,  k=0,1,,n.  
The mean of the distribution is np and the variance is np1-p.
g01bjf computes for given n, p and k the probabilities:
plek=ProbXk pgtk=ProbX>k peqk=ProbX=k .  
The method is similar to the method for the Poisson distribution described in Knüsel (1986).


Knüsel L (1986) Computation of the chi-square and Poisson distribution SIAM J. Sci. Statist. Comput. 7 1022–1036


1:     n – IntegerInput
On entry: the parameter n of the binomial distribution.
Constraint: n0.
2:     p – Real (Kind=nag_wp)Input
On entry: the parameter p of the binomial distribution.
Constraint: 0.0<p<1.0.
3:     k – IntegerInput
On entry: the integer k which defines the required probabilities.
Constraint: 0kn.
4:     plek – Real (Kind=nag_wp)Output
On exit: the lower tail probability, ProbXk.
5:     pgtk – Real (Kind=nag_wp)Output
On exit: the upper tail probability, ProbX>k.
6:     peqk – Real (Kind=nag_wp)Output
On exit: the point probability, ProbX=k.
7:     ifail – IntegerInput/Output
On entry: ifail must be set to 0, -1 or 1. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1 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 argument, the recommended value is 0. When the value -1 or 1 is used it is essential to test the value of ifail on exit.
On exit: ifail=0 unless the routine detects an error or a warning has been flagged (see Section 6).

Error Indicators and Warnings

If on entry ifail=0 or -1, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
On entry, n=value.
Constraint: n0.
On entry, p=value.
Constraint: p<1.0.
On entry, p=value.
Constraint: p>0.0.
On entry, k=value.
Constraint: k0.
On entry, k=value and n=value.
Constraint: kn.
On entry, n is too large to be represented exactly as a double precision number.
On entry, the variance = np 1-p  exceeds 106.
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.


Results are correct to a relative accuracy of at least 10-6 on machines with a precision of 9 or more decimal digits, and to a relative accuracy of at least 10-3 on machines of lower precision (provided that the results do not underflow to zero).

Parallelism and Performance

g01bjf is not threaded in any implementation.

Further Comments

The time taken by g01bjf depends on the variance (=np1-p) and on k. For given variance, the time is greatest when knp (=the mean), and is then approximately proportional to the square-root of the variance.


This example reads values of n and p from a data file until end-of-file is reached, and prints the corresponding probabilities.

Program Text

Program Text (g01bjfe.f90)

Program Data

Program Data (g01bjfe.d)

Program Results

Program Results (g01bjfe.r)