Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_stat_prob_binomial (g01bj)

## Purpose

nag_stat_prob_binomial (g01bj) returns the lower tail, upper tail and point probabilities associated with a binomial distribution.

## Syntax

[plek, pgtk, peqk, ifail] = g01bj(n, p, k)
[plek, pgtk, peqk, ifail] = nag_stat_prob_binomial(n, p, k)

## 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)$.
nag_stat_prob_binomial (g01bj) 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).

## References

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

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
The parameter $n$ of the binomial distribution.
Constraint: ${\mathbf{n}}\ge 0$.
2:     $\mathrm{p}$ – double scalar
The parameter $p$ of the binomial distribution.
Constraint: $0.0<{\mathbf{p}}<1.0$.
3:     $\mathrm{k}$int64int32nag_int scalar
The integer $k$ which defines the required probabilities.
Constraint: $0\le {\mathbf{k}}\le {\mathbf{n}}$.

None.

### Output Parameters

1:     $\mathrm{plek}$ – double scalar
The lower tail probability, $\mathrm{Prob}\left\{X\le k\right\}$.
2:     $\mathrm{pgtk}$ – double scalar
The upper tail probability, $\mathrm{Prob}\left\{X>k\right\}$.
3:     $\mathrm{peqk}$ – double scalar
The point probability, $\mathrm{Prob}\left\{X=k\right\}$.
4:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:
${\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 double number.
${\mathbf{ifail}}=5$
 On entry, the variance ($\text{}=np\left(1-p\right)$) exceeds ${10}^{6}$.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## 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 nag_stat_prob_binomial (g01bj) 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.

## Example

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

fprintf('g01bj example results\n\n');

n = int64([4    19     100    2000]);
k = int64([2    13     67     700]);
p =         [0.5  0.44   0.75   0.33];

fprintf('   n     p      k     plek      pgtk      peqk\n');
for i=1:4
[plek, pgtk, peqk, ifail] = ...
g01bj(n(i), p(i), k(i));
fprintf('%5d%7.3f%5d%10.5f%10.5f%10.5f\n', n(i), p(i), k(i), ...
plek, pgtk, peqk);
end

```
```g01bj example results

n     p      k     plek      pgtk      peqk
4  0.500    2   0.68750   0.31250   0.37500
19  0.440   13   0.99138   0.00862   0.01939
100  0.750   67   0.04460   0.95540   0.01700
2000  0.330  700   0.97251   0.02749   0.00312
```