# NAG CL Interfaceg01bjc (prob_​binomial)

Settings help

CL Name Style:

## 1Purpose

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

## 2Specification

 #include
 void g01bjc (Integer n, double p, Integer k, double *plek, double *pgtk, double *peqk, NagError *fail)
The function may be called by the names: g01bjc, nag_stat_prob_binomial or nag_binomial_dist.

## 3Description

Let $X$ denote a random variable having a binomial distribution with parameters $n$ and $p$ ($n\ge 0$ and $0). Then
 $Prob{X=k}=( n k ) pk(1-p)n-k, k=0,1,…,n.$
The mean of the distribution is $np$ and the variance is $np\left(1-p\right)$.
g01bjc computes for given $n$, $p$ and $k$ the probabilities:
 $plek=Prob{X≤k} pgtk=Prob{X>k} peqk=Prob{X=k} .$
The method is similar to the method for the Poisson distribution described in Knüsel (1986).

## 4References

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

## 5Arguments

1: $\mathbf{n}$Integer Input
On entry: the parameter $n$ of the binomial distribution.
Constraint: ${\mathbf{n}}\ge 0$.
2: $\mathbf{p}$double Input
On entry: the parameter $p$ of the binomial distribution.
Constraint: $0.0<{\mathbf{p}}<1.0$.
3: $\mathbf{k}$Integer Input
On entry: the integer $k$ which defines the required probabilities.
Constraint: $0\le {\mathbf{k}}\le {\mathbf{n}}$.
4: $\mathbf{plek}$double * Output
On exit: the lower tail probability, $\mathrm{Prob}\left\{X\le k\right\}$.
5: $\mathbf{pgtk}$double * Output
On exit: the upper tail probability, $\mathrm{Prob}\left\{X>k\right\}$.
6: $\mathbf{peqk}$double * Output
On exit: the point probability, $\mathrm{Prob}\left\{X=k\right\}$.
7: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

## 6Error Indicators and Warnings

NE_2_INT_ARG_GT
On entry, ${\mathbf{k}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{k}}\le {\mathbf{n}}$.
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_ARG_TOO_LARGE
On entry, n is too large to be represented exactly as a double precision number.
On entry, argument $⟨\mathit{\text{value}}⟩$ had an illegal value.
NE_INT_ARG_LT
On entry, ${\mathbf{k}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{k}}\ge 0$.
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 0$.
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_ARG_GE
On entry, ${\mathbf{p}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{p}}<1.0$.
NE_REAL_ARG_LE
On entry, ${\mathbf{p}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{p}}>0.0$.
NE_VARIANCE_TOO_LARGE
On entry, the variance $\left(=np\left(1-p\right)\right)$ exceeds ${10}^{6}$.

## 7Accuracy

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

## 8Parallelism and Performance

Background information to multithreading can be found in the Multithreading documentation.
g01bjc is not threaded in any implementation.

The time taken by g01bjc 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.

## 10Example

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

### 10.1Program Text

Program Text (g01bjce.c)

### 10.2Program Data

Program Data (g01bjce.d)

### 10.3Program Results

Program Results (g01bjce.r)