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_multinomial (g05tg)

## Purpose

nag_rand_int_multinomial (g05tg) generates a sequence of n$n$ variates, each consisting of k$k$ pseudorandom integers, from the discrete multinomial distribution with k$k$ outcomes and m$m$ trials, where the outcomes have probabilities p1,p2,,pk${p}_{1},{p}_{2},\dots ,{p}_{k}$ respectively.

## Syntax

[r, state, x, ifail] = g05tg(mode, n, m, p, r, state, 'k', k)
[r, state, x, ifail] = nag_rand_int_multinomial(mode, n, m, p, r, state, 'k', k)

## Description

nag_rand_int_multinomial (g05tg) generates a sequence of n$n$ groups of k$k$ integers xi,j${x}_{\mathit{i},\mathit{j}}$, for j = 1,2,,k$\mathit{j}=1,2,\dots ,k$ and i = 1,2,,n$\mathit{i}=1,2,\dots ,n$, from a multinomial distribution with m$m$ trials and k$k$ outcomes, where the probability of xi,j = Ij${x}_{\mathit{i},\mathit{j}}={I}_{j}$ for each j = 1,2,,k$j=1,2,\dots ,k$ is
 k P(i1 = I1, … ,ik = Ik) = (m ! )/( ∏ j = 1k Ij ! ) ∏ pjIj = (m ! )/(I1 ! I2 ! ⋯ Ik ! )p1I1p2I2 ⋯ pkIk, j = 1
$P(i1=I1,…,ik=Ik)= m! ∏j=1k Ij! ∏j=1k pjIj= m! I1!I2!⋯Ik! p1I1p2I2⋯pkIk,$
where
 k k ∑ pj = 1  and ∑ Ij = m. j = 1 j = 1
$∑j= 1k pj= 1 and ∑j= 1k Ij=m.$
A single trial can have several outcomes (k$k$) and the probability of achieving each outcome is known (pj${p}_{j}$). After m$m$ trials each outcome will have occurred a certain number of times. The k$k$ numbers representing the numbers of occurrences for each outcome after m$m$ trials is then a single sample from the multinomial distribution defined by the parameters k$k$, m$m$ and pj${p}_{\mathit{j}}$, for j = 1,2,,k$\mathit{j}=1,2,\dots ,k$. This function returns n$n$ such samples.
When k = 2$k=2$ this distribution is equivalent to the binomial distribution with parameters m$m$ and p = p1$p={p}_{1}$ (see nag_rand_int_binomial (g05ta)).
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_multinomial (g05tg) with the same parameter values can then use this reference vector to generate further variates. The reference array is generated only for the outcome with greatest probability. The number of successes for the outcome with greatest probability is calculated first as for the binomial distribution (see nag_rand_int_binomial (g05ta)); the number of successes for other outcomes are calculated in turn for the remaining reduced multinomial distribution; the number of successes for the final outcome is simply calculated to ensure that the total number of successes is m$m$.
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_multinomial (g05tg).

## 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_multinomial (g05tg).
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 trials of the multinomial distribution.
Constraint: m0${\mathbf{m}}\ge 0$.
4:     p(k) – double array
k, the dimension of the array, must satisfy the constraint k2${\mathbf{k}}\ge 2$.
Contains the probabilities pj${p}_{\mathit{j}}$, for j = 1,2,,k$\mathit{j}=1,2,\dots ,k$, of the k$k$ possible outcomes of the multinomial distribution.
Constraint: 0.0p(j)1.0$0.0\le {\mathbf{p}}\left(j\right)\le 1.0$ and j = 1kp(j) = 1.0$\sum _{j=1}^{k}{\mathbf{p}}\left(j\right)=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 > min (m,INT[m × p_max + 7.25 × sqrt(m × p_max × (1 − p_max) ) + 8.5]) − max (0,INT[m × p_max − 7.25 × sqrt(m × p_max × (1 − p_max) )]) + 9
$\begin{array}{lll}\mathit{lr}& >& \mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{m}},\mathrm{INT}\left[{\mathbf{m}}×\mathit{p_max}+7.25×\sqrt{{\mathbf{m}}×\mathit{p_max}×\left(1-\mathit{p_max}\right)}+8.5\right]\right)\\ & & -\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(0,\mathrm{INT}\left[{\mathbf{m}}×\mathit{p_max}-7.25×\sqrt{{\mathbf{m}}×\mathit{p_max}×\left(1-\mathit{p_max}\right)}\right]\right)+9\end{array}$;
• if mode = 1${\mathbf{mode}}=1$, lr must remain unchanged from the previous call to nag_rand_int_multinomial (g05tg).
If mode = 1${\mathbf{mode}}=1$, the reference vector from the previous call to nag_rand_int_multinomial (g05tg).
If mode = 3${\mathbf{mode}}=3$, r is not referenced by nag_rand_int_multinomial (g05tg).
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.

### Optional Input Parameters

1:     k – int64int32nag_int scalar
Default: The dimension of the array p.
k$k$, the number of possible outcomes of the multinomial distribution.
Constraint: k2${\mathbf{k}}\ge 2$.

lr ldx

### 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(ldx,k) – int64int32nag_int array
ldxn$\mathit{ldx}\ge {\mathbf{n}}$.
The first n$n$ rows of x(i,j)${\mathbf{x}}\left(i,j\right)$ each contain k$k$ pseudorandom numbers representing a k$k$-dimensional variate from the specified multinomial 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, k < 2${\mathbf{k}}<2$.
ifail = 5${\mathbf{ifail}}=5$
On entry, p(j) < 0.0${\mathbf{p}}\left(j\right)<0.0$ or p(j) > 1.0${\mathbf{p}}\left(j\right)>1.0$ for at least one value of j$j$.
The probabilities p(j)${\mathbf{p}}\left(\mathit{j}\right)$, for j = 1,2,,k$\mathit{j}=1,2,\dots ,{\mathbf{k}}$, do not add up to 1$1$.
ifail = 6${\mathbf{ifail}}=6$
The maximum value of p(j)${\mathbf{p}}\left(\mathit{j}\right)$, for j = 1,2,,k$\mathit{j}=1,2,\dots ,{\mathbf{k}}$, or m is not the same as when r was set up in a previous call to nag_rand_int_multinomial (g05tg) with mode = 0${\mathbf{mode}}=0$ or 2$2$.
On entry, the r vector was not initialized correctly, or has been corrupted.
ifail = 7${\mathbf{ifail}}=7$
On entry, lr is too small when mode = 0${\mathbf{mode}}=0$ or 2$2$.
ifail = 8${\mathbf{ifail}}=8$
 On entry, state vector was not initialized or has been corrupted.
ifail = 10${\mathbf{ifail}}=10$
On entry, ldx < n$\mathit{ldx}<{\mathbf{n}}$.

## Accuracy

Not applicable.

The reference vector for only one outcome can be set up because the conditional distributions cannot be known in advance of the generation of variates. The outcome with greatest probability of success is chosen for the reference vector because it will have the greatest spread of likely values.

## Example

```function nag_rand_int_multinomial_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);
mode = int64(2);
n = int64(20);
m = int64(6000);
p = [0.08; 0.1; 0.8; 0.02];
r = zeros(6007, 1);
% Initialize the generator to a repeatable sequence
[state, ifail] = nag_rand_init_repeat(genid, subid, seed);
[r, state, x, ifail] = nag_rand_int_multinomial(mode, n, m, p, r, state);
x, ifail
```
```

x =

468                  603                 4811                  118
490                  630                 4761                  119
482                  575                 4821                  122
495                  591                 4826                   88
512                  611                 4761                  116
474                  601                 4800                  125
485                  595                 4791                  129
468                  582                 4825                  125
485                  598                 4800                  117
485                  573                 4814                  128
501                  634                 4749                  116
482                  618                 4780                  120
470                  584                 4810                  136
479                  642                 4750                  129
476                  608                 4807                  109
473                  631                 4782                  114
509                  596                 4778                  117
450                  565                 4877                  108
484                  556                 4840                  120
466                  615                 4802                  117

ifail =

0

```
```function g05tg_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);
mode = int64(2);
n = int64(20);
m = int64(6000);
p = [0.08; 0.1; 0.8; 0.02];
r = zeros(6007, 1);
% Initialize the generator to a repeatable sequence
[state, ifail] = g05kf(genid, subid, seed);
[r, state, x, ifail] = g05tg(mode, n, m, p, r, state);
x, ifail
```
```

x =

468                  603                 4811                  118
490                  630                 4761                  119
482                  575                 4821                  122
495                  591                 4826                   88
512                  611                 4761                  116
474                  601                 4800                  125
485                  595                 4791                  129
468                  582                 4825                  125
485                  598                 4800                  117
485                  573                 4814                  128
501                  634                 4749                  116
482                  618                 4780                  120
470                  584                 4810                  136
479                  642                 4750                  129
476                  608                 4807                  109
473                  631                 4782                  114
509                  596                 4778                  117
450                  565                 4877                  108
484                  556                 4840                  120
466                  615                 4802                  117

ifail =

0

```