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_rand_int_negbin (g05th)

## Purpose

nag_rand_int_negbin (g05th) generates a vector of pseudorandom integers from the discrete negative binomial distribution with parameter $m$ and probability $p$ of success at a trial.

## Syntax

[r, state, x, ifail] = g05th(mode, n, m, p, r, state)
[r, state, x, ifail] = nag_rand_int_negbin(mode, n, m, p, r, state)

## Description

nag_rand_int_negbin (g05th) generates $n$ integers ${x}_{i}$ from a discrete negative binomial distribution, where the probability of ${x}_{i}=I$ ($I$ successes before $m$ failures) is
 $Pxi=I= m+I-1! I!m-1! ×pI×1-pm, I=0,1,….$
The variates can be generated with or without using a search table and index. If a search table is used then it is stored with the index in a reference vector and subsequent calls to nag_rand_int_negbin (g05th) with the same parameter value can then use this reference vector to generate further variates.
One of the initialization functions nag_rand_init_repeat (g05kf) (for a repeatable sequence if computed sequentially) or nag_rand_init_nonrepeat (g05kg) (for a non-repeatable sequence) must be called prior to the first call to nag_rand_int_negbin (g05th).

## References

Knuth D E (1981) The Art of Computer Programming (Volume 2) (2nd Edition) Addison–Wesley

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{mode}$int64int32nag_int scalar
A code for selecting the operation to be performed by the function.
${\mathbf{mode}}=0$
Set up reference vector only.
${\mathbf{mode}}=1$
Generate variates using reference vector set up in a prior call to nag_rand_int_negbin (g05th).
${\mathbf{mode}}=2$
Set up reference vector and generate variates.
${\mathbf{mode}}=3$
Generate variates without using the reference vector.
Constraint: ${\mathbf{mode}}=0$, $1$, $2$ or $3$.
2:     $\mathrm{n}$int64int32nag_int scalar
$n$, the number of pseudorandom numbers to be generated.
Constraint: ${\mathbf{n}}\ge 0$.
3:     $\mathrm{m}$int64int32nag_int scalar
$m$, the number of failures of the distribution.
Constraint: ${\mathbf{m}}\ge 0$.
4:     $\mathrm{p}$ – double scalar
$p$, the parameter of the negative binomial distribution representing the probability of success at a single trial.
Constraint: $0.0\le {\mathbf{p}}<1.0$.
5:     $\mathrm{r}\left(\mathit{lr}\right)$ – double array
lr, the dimension of the array, must satisfy the constraint
• if ${\mathbf{mode}}=0$ or $2$,
$\begin{array}{cc}\mathit{lr}>& \mathrm{int}\left(\begin{array}{c}\frac{{\mathbf{m}}×{\mathbf{p}}+7.15×\sqrt{{\mathbf{m}}×{\mathbf{p}}}+20.15×{\mathbf{p}}}{1-{\mathbf{p}}}+8.5\end{array}\right)\\ \\ & -\mathrm{max}\phantom{\rule{0.25em}{0ex}}\left(\begin{array}{c}0,\mathrm{int}\left(\begin{array}{c}\frac{{\mathbf{m}}×{\mathbf{p}}-7.15×\sqrt{{\mathbf{m}}×{\mathbf{p}}}}{1-{\mathbf{p}}}\end{array}\right)\end{array}\right)+9\\ \end{array}$;
• if ${\mathbf{mode}}=1$, lr must remain unchanged from the previous call to nag_rand_int_negbin (g05th).
If ${\mathbf{mode}}=1$, the reference vector from the previous call to nag_rand_int_negbin (g05th).
If ${\mathbf{mode}}=3$, r is not referenced.
6:     $\mathrm{state}\left(:\right)$int64int32nag_int array
Note: the actual argument supplied must be the array state supplied to the initialization routines nag_rand_init_repeat (g05kf) or nag_rand_init_nonrepeat (g05kg).
Contains information on the selected base generator and its current state.

None.

### Output Parameters

1:     $\mathrm{r}\left(\mathit{lr}\right)$ – double array
If ${\mathbf{mode}}\ne 3$, the reference vector.
2:     $\mathrm{state}\left(:\right)$int64int32nag_int array
Contains updated information on the state of the generator.
3:     $\mathrm{x}\left({\mathbf{n}}\right)$int64int32nag_int array
The $n$ pseudorandom numbers from the specified negative binomial distribution.
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$
Constraint: ${\mathbf{mode}}=0$, $1$, $2$ or $3$.
${\mathbf{ifail}}=2$
Constraint: ${\mathbf{n}}\ge 0$.
${\mathbf{ifail}}=3$
Constraint: ${\mathbf{m}}\ge 0$.
${\mathbf{ifail}}=4$
Constraint: $0.0\le {\mathbf{p}}<1.0$.
${\mathbf{ifail}}=5$
On entry, some of the elements of the array r have been corrupted or have not been initialized.
p or m is not the same as when r was set up in a previous call.
${\mathbf{ifail}}=6$
On entry, lr is too small when ${\mathbf{mode}}=0$ or $2$.
${\mathbf{ifail}}=7$
On entry, state vector has been corrupted or not initialized.
${\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.

Not applicable.

None.

## Example

This example prints $20$ pseudorandom integers from a negative binomial distribution with parameters $m=60$ and $p=0.999$, generated by a single call to nag_rand_int_negbin (g05th), after initialization by nag_rand_init_repeat (g05kf).
```function g05th_example

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

% Initialize the base generator to a repeatable sequence
seed  = [int64(1762543)];
genid = int64(1);
subid = int64(1);
[state, ifail] = g05kf( ...
genid, subid, seed);

% Number of variates
n = int64(20);

% Parameters
m = int64(60);
p = 0.999;

% Generate variates from a negative binomial distribution
% without reference vector
mode = int64(3);
r    = ;
[r, state, x, ifail] = g05th( ...
mode, n, m, p, r, state);

disp('Variates');
disp(double(x));

```
```g05th example results

Variates
62339
50505
64863
66289
50434
59461
57365
65965
59572
63104
47833
54735
62075
48018
61458
55190
54263
80995
70129
60200

```