G01 Chapter Contents
G01 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG01BJF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

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

## 2  Specification

 SUBROUTINE G01BJF ( N, P, K, PLEK, PGTK, PEQK, IFAIL)
 INTEGER N, K, IFAIL REAL (KIND=nag_wp) P, PLEK, PGTK, PEQK

## 3  Description

Let $X$ denote a random variable having a binomial distribution with parameters $n$ and $p$ ($n\ge 0$ and $0). Then
 $ProbX=k= n k pk1-pn-k, k=0,1,…,n.$
The mean of the distribution is $np$ and the variance is $np\left(1-p\right)$.
G01BJF computes for given $n$, $p$ and $k$ the probabilities:
 $PLEK=ProbX≤k PGTK=ProbX>k PEQK=ProbX=k .$
The method is similar to the method for the Poisson distribution described in Knüsel (1986).

## 4  References

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

## 5  Parameters

1:     N – INTEGERInput
On entry: the parameter $n$ of the binomial distribution.
Constraint: ${\mathbf{N}}\ge 0$.
2:     P – REAL (KIND=nag_wp)Input
On entry: the parameter $p$ of the binomial distribution.
Constraint: $0.0<{\mathbf{P}}<1.0$.
3:     K – INTEGERInput
On entry: the integer $k$ which defines the required probabilities.
Constraint: $0\le {\mathbf{K}}\le {\mathbf{N}}$.
4:     PLEK – REAL (KIND=nag_wp)Output
On exit: the lower tail probability, $\mathrm{Prob}\left\{X\le k\right\}$.
5:     PGTK – REAL (KIND=nag_wp)Output
On exit: the upper tail probability, $\mathrm{Prob}\left\{X>k\right\}$.
6:     PEQK – REAL (KIND=nag_wp)Output
On exit: the point probability, $\mathrm{Prob}\left\{X=k\right\}$.
7:     IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ 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 parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ 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).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{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{N}}<0$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{P}}\le 0.0$, or ${\mathbf{P}}\ge 1.0$.
${\mathbf{IFAIL}}=3$
 On entry, ${\mathbf{K}}<0$, or ${\mathbf{K}}>{\mathbf{N}}$.
${\mathbf{IFAIL}}=4$
 On entry, N is too large to be represented exactly as a real number.
${\mathbf{IFAIL}}=5$
 On entry, the variance ($\text{}=np\left(1-p\right)$) exceeds ${10}^{6}$.

## 7  Accuracy

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

The time taken by G01BJF depends on the variance ($\text{}=np\left(1-p\right)$) and on $k$. For given variance, the time is greatest when $k\approx np$ ($\text{}=\text{the mean}$), and is then approximately proportional to the square-root of the variance.

## 9  Example

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

### 9.1  Program Text

Program Text (g01bjfe.f90)

### 9.2  Program Data

Program Data (g01bjfe.d)

### 9.3  Program Results

Program Results (g01bjfe.r)