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

PDF version (NAG web site, 64-bit version, 64-bit version)
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$m$ and probability p$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$n$ integers xi${x}_{i}$ from a discrete negative binomial distribution, where the probability of xi = I${x}_{i}=I$ (I$I$ successes before m$m$ failures) is
 P(xi = I) = ((m + I − 1) ! )/(I ! (m − 1) ! ) × pI × (1 − p)m,  I = 0,1, … . $P(xi=I)= (m+I-1)! I!(m-1)! ×pI×(1-p)m, 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:     mode – int64int32nag_int scalar
A code for selecting the operation to be performed by the function.
mode = 0${\mathbf{mode}}=0$
Set up reference vector only.
mode = 1${\mathbf{mode}}=1$
Generate variates using reference vector set up in a prior call to nag_rand_int_negbin (g05th).
mode = 2${\mathbf{mode}}=2$
Set up reference vector and generate variates.
mode = 3${\mathbf{mode}}=3$
Generate variates without using the reference vector.
Constraint: mode = 0${\mathbf{mode}}=0$, 1$1$, 2$2$ or 3$3$.
2:     n – int64int32nag_int scalar
n$n$, the number of pseudorandom numbers to be generated.
Constraint: n0${\mathbf{n}}\ge 0$.
3:     m – int64int32nag_int scalar
m$m$, the number of failures of the distribution.
Constraint: m0${\mathbf{m}}\ge 0$.
4:     p – double scalar
p$p$, the parameter of the negative binomial distribution representing the probability of success at a single trial.
Constraint: 0.0p < 1.0$0.0\le {\mathbf{p}}<1.0$.
5:     r(lr) – double array
lr, the dimension of the array, must satisfy the constraint
• if mode = 0${\mathbf{mode}}=0$ or 2$2$,
lr > ( ( m × p + 7.15 × sqrt(m × p) + 20.15 × p )/(1 − p) + 8.5 ) int
− ()
max ( ( m × p − 7.15 × sqrt(m × p) )/(1 − p) ) 0,int
+ 9
$\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 mode = 1${\mathbf{mode}}=1$, lr must remain unchanged from the previous call to nag_rand_int_negbin (g05th).
If mode = 1${\mathbf{mode}}=1$, the reference vector from the previous call to nag_rand_int_negbin (g05th).
If mode = 3${\mathbf{mode}}=3$, r is not referenced by nag_rand_int_negbin (g05th).
6:     state( : $:$) – 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.

lr

### Output Parameters

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

Not applicable.

None.

## Example

```function nag_rand_int_negbin_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);
mode = int64(3);
n = int64(20);
m = int64(60);
p = 0.999;
r = zeros(1, 1);
% Initialize the generator to a repeatable sequence
[state, ifail] = nag_rand_init_repeat(genid, subid, seed);
[r, state, x, ifail] = nag_rand_int_negbin(mode, n, m, p, r, state)
```
```

r =

0

state =

17
1234
1
0
6694
27818
10435
15383
17917
13895
19930
8
0
1234
1
1
1234

x =

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

ifail =

0

```
```function g05th_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);
mode = int64(3);
n = int64(20);
m = int64(60);
p = 0.999;
r = zeros(1, 1);
% Initialize the generator to a repeatable sequence
[state, ifail] = g05kf(genid, subid, seed);
[r, state, x, ifail] = g05th(mode, n, m, p, r, state)
```
```

r =

0

state =

17
1234
1
0
6694
27818
10435
15383
17917
13895
19930
8
0
1234
1
1
1234

x =

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

ifail =

0

```

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013