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_vector (g01sj)

## Purpose

nag_stat_prob_binomial_vector (g01sj) returns a number of the lower tail, upper tail and point probabilities for the binomial distribution.

## Syntax

[plek, pgtk, peqk, ivalid, ifail] = g01sj(n, p, k, 'ln', ln, 'lp', lp, 'lk', lk)
[plek, pgtk, peqk, ivalid, ifail] = nag_stat_prob_binomial_vector(n, p, k, 'ln', ln, 'lp', lp, 'lk', lk)

## Description

Let X = {Xi : i = 1 , 2 ,, m } $X=\left\{{X}_{i}:i=1,2,\dots ,m\right\}$ denote a vector of random variables each having a binomial distribution with parameters ni${n}_{i}$ and pi${p}_{i}$ (ni0${n}_{i}\ge 0$ and 0 < pi < 1$0<{p}_{i}<1$). Then
Prob{Xi = ki} =
 ( ni ) ki
piki(1pi)niki,  ki = 0,1,,ni.
$Prob{Xi=ki}= ni ki piki(1-pi)ni-ki, ki=0,1,…,ni.$
The mean of the each distribution is given by nipi${n}_{i}{p}_{i}$ and the variance by nipi(1pi)${n}_{i}{p}_{i}\left(1-{p}_{i}\right)$.
nag_stat_prob_binomial_vector (g01sj) computes, for given ni${n}_{i}$, pi${p}_{i}$ and ki${k}_{i}$, the probabilities: Prob{Xiki}$\mathrm{Prob}\left\{{X}_{i}\le {k}_{i}\right\}$, Prob{Xi > ki}$\mathrm{Prob}\left\{{X}_{i}>{k}_{i}\right\}$ and Prob{Xi = ki}$\mathrm{Prob}\left\{{X}_{i}={k}_{i}\right\}$ using an algorithm similar to that described in Knüsel (1986) for the Poisson distribution.
The input arrays to this function are designed to allow maximum flexibility in the supply of vector parameters by re-using elements of any arrays that are shorter than the total number of evaluations required. See Section [Vectorized s] in the G01 Chapter Introduction for further information.

## 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:     n(ln) – int64int32nag_int array
ln, the dimension of the array, must satisfy the constraint ln > 0${\mathbf{ln}}>0$.
ni${n}_{i}$, the first parameter of the binomial distribution with ni = n(j)${n}_{i}={\mathbf{n}}\left(j\right)$, j = ((i1)  mod  ln) + 1, for i = 1,2,,max (ln,lp,lk)$i=1,2,\dots ,\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{ln}},{\mathbf{lp}},{\mathbf{lk}}\right)$.
Constraint: n(j)0${\mathbf{n}}\left(\mathit{j}\right)\ge 0$, for j = 1,2,,ln$\mathit{j}=1,2,\dots ,{\mathbf{ln}}$.
2:     p(lp) – double array
lp, the dimension of the array, must satisfy the constraint lp > 0${\mathbf{lp}}>0$.
pi${p}_{i}$, the second parameter of the binomial distribution with pi = p(j)${p}_{i}={\mathbf{p}}\left(j\right)$, j = ((i1)  mod  lp) + 1.
Constraint: 0.0 < p(j) < 1.0$0.0<{\mathbf{p}}\left(\mathit{j}\right)<1.0$, for j = 1,2,,lp$\mathit{j}=1,2,\dots ,{\mathbf{lp}}$.
3:     k(lk) – int64int32nag_int array
lk, the dimension of the array, must satisfy the constraint lk > 0${\mathbf{lk}}>0$.
ki${k}_{i}$, the integer which defines the required probabilities with ki = k(j)${k}_{i}={\mathbf{k}}\left(j\right)$, j = ((i1)  mod  lk) + 1.
Constraint: 0 ki ni $0\le {k}_{i}\le {n}_{i}$.

### Optional Input Parameters

1:     ln – int64int32nag_int scalar
Default: The dimension of the array n.
The length of the array n
Constraint: ln > 0${\mathbf{ln}}>0$.
2:     lp – int64int32nag_int scalar
Default: The dimension of the array p.
The length of the array p
Constraint: lp > 0${\mathbf{lp}}>0$.
3:     lk – int64int32nag_int scalar
Default: The dimension of the array k.
The length of the array k
Constraint: lk > 0${\mathbf{lk}}>0$.

None.

### Output Parameters

1:     plek( : $:$) – double array
Note: the dimension of the array plek must be at least max (ln,lp,lk)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{ln}},{\mathbf{lp}},{\mathbf{lk}}\right)$.
Prob{Xiki} $\mathrm{Prob}\left\{{X}_{i}\le {k}_{i}\right\}$, the lower tail probabilities.
2:     pgtk( : $:$) – double array
Note: the dimension of the array pgtk must be at least max (ln,lp,lk)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{ln}},{\mathbf{lp}},{\mathbf{lk}}\right)$.
Prob{Xi > ki} $\mathrm{Prob}\left\{{X}_{i}>{k}_{i}\right\}$, the upper tail probabilities.
3:     peqk( : $:$) – double array
Note: the dimension of the array peqk must be at least max (ln,lp,lk)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{ln}},{\mathbf{lp}},{\mathbf{lk}}\right)$.
Prob{Xi = ki} $\mathrm{Prob}\left\{{X}_{i}={k}_{i}\right\}$, the point probabilities.
4:     ivalid( : $:$) – int64int32nag_int array
Note: the dimension of the array ivalid must be at least max (ln,lp,lk)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{ln}},{\mathbf{lp}},{\mathbf{lk}}\right)$.
ivalid(i)${\mathbf{ivalid}}\left(i\right)$ indicates any errors with the input arguments, with
ivalid(i) = 0${\mathbf{ivalid}}\left(i\right)=0$
No error.
ivalid(i) = 1${\mathbf{ivalid}}\left(i\right)=1$
 On entry, ni < 0${n}_{i}<0$.
ivalid(i) = 2${\mathbf{ivalid}}\left(i\right)=2$
 On entry, pi ≤ 0.0${p}_{i}\le 0.0$, or pi ≥ 1.0${p}_{i}\ge 1.0$.
ivalid(i) = 3${\mathbf{ivalid}}\left(i\right)=3$
 On entry, ki < 0${k}_{i}<0$, or ki > ni${k}_{i}>{n}_{i}$.
ivalid(i) = 4${\mathbf{ivalid}}\left(i\right)=4$
 On entry, ni${n}_{i}$ is too large to be represented exactly as a real number.
ivalid(i) = 5${\mathbf{ivalid}}\left(i\right)=5$
 On entry, the variance ( = nipi(1 − pi)$\text{}={n}_{i}{p}_{i}\left(1-{p}_{i}\right)$) exceeds 106${10}^{6}$.
5:     ifail – int64int32nag_int scalar
${\mathrm{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:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

W ifail = 1${\mathbf{ifail}}=1$
On entry, at least one value of n, p or k was invalid.
ifail = 2${\mathbf{ifail}}=2$
Constraint: ln > 0${\mathbf{ln}}>0$.
ifail = 3${\mathbf{ifail}}=3$
Constraint: lp > 0${\mathbf{lp}}>0$.
ifail = 4${\mathbf{ifail}}=4$
Constraint: lk > 0${\mathbf{lk}}>0$.
ifail = 999${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

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

The time taken by nag_stat_prob_binomial_vector (g01sj) to calculate each probability depends on the variance ( = nipi(1pi)$\text{}={n}_{i}{p}_{i}\left(1-{p}_{i}\right)$) and on ki${k}_{i}$. For given variance, the time is greatest when kinipi${k}_{i}\approx {n}_{i}{p}_{i}$ ( = the mean$\text{}=\text{the mean}$), and is then approximately proportional to the square-root of the variance.

## Example

```function nag_stat_prob_binomial_vector_example
n = [int64(4); 19; 100; 2000];
p = [0.500; 0.440; 0.750; 0.330];
k = [int64(2); 13; 67; 700];
[plek, pgtk, peqk, ivalid, ifail] = nag_stat_prob_binomial_vector(n, p, k);

fprintf('\n    N     P      K     PLEK      PGTK      PEQK\n');
ln = numel(n);
lp = numel(p);
lk = numel(k);
len = max ([ln, lp, lk]);
for i=0:len-1
fprintf('%5d%8.3f%5d%10.5f%10.5f%10.5f%3d\n', n(mod(i,ln)+1), ...
p(mod(i,lp)+1), k(mod(i,lk)+1), plek(i+1), pgtk(i+1), peqk(i+1), ivalid(i+1));
end
```
```

N     P      K     PLEK      PGTK      PEQK
4   0.500    2   0.68750   0.31250   0.37500  0
19   0.440   13   0.99138   0.00862   0.01939  0
100   0.750   67   0.04460   0.95540   0.01700  0
2000   0.330  700   0.97251   0.02749   0.00312  0

```
```function g01sj_example
n = [int64(4); 19; 100; 2000];
p = [0.500; 0.440; 0.750; 0.330];
k = [int64(2); 13; 67; 700];
[plek, pgtk, peqk, ivalid, ifail] = g01sj(n, p, k);

fprintf('\n    N     P      K     PLEK      PGTK      PEQK\n');
ln = numel(n);
lp = numel(p);
lk = numel(k);
len = max ([ln, lp, lk]);
for i=0:len-1
fprintf('%5d%8.3f%5d%10.5f%10.5f%10.5f%3d\n', n(mod(i,ln)+1), ...
p(mod(i,lp)+1), k(mod(i,lk)+1), plek(i+1), pgtk(i+1), peqk(i+1), ivalid(i+1));
end
```
```

N     P      K     PLEK      PGTK      PEQK
4   0.500    2   0.68750   0.31250   0.37500  0
19   0.440   13   0.99138   0.00862   0.01939  0
100   0.750   67   0.04460   0.95540   0.01700  0
2000   0.330  700   0.97251   0.02749   0.00312  0

```