# NAG Library Routine Document

## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine g05tgf ( mode, n, m, k, p, r, lr, x, ldx,
 Integer, Intent (In) :: mode, n, m, k, lr, ldx Integer, Intent (Inout) :: state(*), x(ldx,k), ifail Real (Kind=nag_wp), Intent (In) :: p(k) Real (Kind=nag_wp), Intent (Inout) :: r(lr)
#include nagmk26.h
 void g05tgf_ (const Integer *mode, const Integer *n, const Integer *m, const Integer *k, const double p[], double r[], const Integer *lr, Integer state[], Integer x[], const Integer *ldx, Integer *ifail)

## 3Description

g05tgf generates a sequence of $n$ groups of $k$ integers ${x}_{\mathit{i},\mathit{j}}$, for $\mathit{j}=1,2,\dots ,k$ and $\mathit{i}=1,2,\dots ,n$, from a multinomial distribution with $m$ trials and $k$ outcomes, where the probability of ${x}_{\mathit{i},\mathit{j}}={I}_{j}$ for each $j=1,2,\dots ,k$ is
 $Pi1=I1,…,ik=Ik= m! ∏j=1k Ij! ∏j=1k pjIj= m! I1!I2!⋯Ik! p1I1p2I2⋯pkIk,$
where
 $∑j= 1k pj= 1 and ∑j= 1k Ij=m.$
A single trial can have several outcomes ($k$) and the probability of achieving each outcome is known (${p}_{j}$). After $m$ trials each outcome will have occurred a certain number of times. The $k$ numbers representing the numbers of occurrences for each outcome after $m$ trials is then a single sample from the multinomial distribution defined by the parameters $k$, $m$ and ${p}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,k$. This routine returns $n$ such samples.
When $k=2$ this distribution is equivalent to the binomial distribution with parameters $m$ and $p={p}_{1}$ (see g05taf).
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 g05tgf 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 g05taf); 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$.
One of the initialization routines g05kff (for a repeatable sequence if computed sequentially) or g05kgf (for a non-repeatable sequence) must be called prior to the first call to g05tgf.

## 4References

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

## 5Arguments

1:     $\mathbf{mode}$ – IntegerInput
On entry: a code for selecting the operation to be performed by the routine.
${\mathbf{mode}}=0$
Set up reference vector only.
${\mathbf{mode}}=1$
Generate variates using reference vector set up in a prior call to g05tgf.
${\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:     $\mathbf{n}$ – IntegerInput
On entry: $n$, the number of pseudorandom numbers to be generated.
Constraint: ${\mathbf{n}}\ge 0$.
3:     $\mathbf{m}$ – IntegerInput
On entry: $m$, the number of trials of the multinomial distribution.
Constraint: ${\mathbf{m}}\ge 0$.
4:     $\mathbf{k}$ – IntegerInput
On entry: $k$, the number of possible outcomes of the multinomial distribution.
Constraint: ${\mathbf{k}}\ge 2$.
5:     $\mathbf{p}\left({\mathbf{k}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: contains the probabilities ${p}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,k$, of the $k$ possible outcomes of the multinomial distribution.
Constraint: $0.0\le {\mathbf{p}}\left(j\right)\le 1.0$ and $\sum _{j=1}^{k}{\mathbf{p}}\left(j\right)=1.0$.
6:     $\mathbf{r}\left({\mathbf{lr}}\right)$ – Real (Kind=nag_wp) arrayCommunication Array
On entry: if ${\mathbf{mode}}=1$, the reference vector from the previous call to g05tgf.
If ${\mathbf{mode}}=3$, r is not referenced.
On exit: if ${\mathbf{mode}}\ne 3$, the reference vector.
7:     $\mathbf{lr}$ – IntegerInput
Note: for convenience p_max will be used here to denote the expression $\mathit{p_max}=\underset{j}{\mathrm{max}}\phantom{\rule{0.25em}{0ex}}\left({\mathbf{p}}\left(j\right)\right)$.
On entry: the dimension of the array r as declared in the (sub)program from which g05tgf is called.
Suggested values:
• if ${\mathbf{mode}}\ne 3$, ${\mathbf{lr}}=30+20×\sqrt{{\mathbf{m}}×\mathit{p_max}×\left(1-\mathit{p_max}\right)}$;
• otherwise ${\mathbf{lr}}=1$.
Constraints:
• if ${\mathbf{mode}}=0$ or $2$,
$\begin{array}{lll}{\mathbf{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 ${\mathbf{mode}}=1$, lr must remain unchanged from the previous call to g05tgf.
8:     $\mathbf{state}\left(*\right)$ – Integer arrayCommunication Array
Note: the actual argument supplied must be the array state supplied to the initialization routines g05kff or g05kgf.
On entry: contains information on the selected base generator and its current state.
On exit: contains updated information on the state of the generator.
9:     $\mathbf{x}\left({\mathbf{ldx}},{\mathbf{k}}\right)$ – Integer arrayOutput
On exit: the first $n$ rows of ${\mathbf{x}}\left(i,j\right)$ each contain $k$ pseudorandom numbers representing a $k$-dimensional variate from the specified multinomial distribution.
10:   $\mathbf{ldx}$ – IntegerInput
On entry: the first dimension of the array x as declared in the (sub)program from which g05tgf is called.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{n}}$.
11:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{mode}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mode}}=0$, $1$, $2$ or $3$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 0$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m}}\ge 0$.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{k}}\ge 2$.
${\mathbf{ifail}}=5$
On entry, at least one element of the vector p is less than $0.0$ or greater than $1.0$.
On entry, the sum of the elements of p do not equal one.
${\mathbf{ifail}}=6$
On entry, some of the elements of the array r have been corrupted or have not been initialized.
The value of m or k is not the same as when r was set up in a previous call.
Previous value of ${\mathbf{m}}=〈\mathit{\text{value}}〉$ and ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Previous value of ${\mathbf{k}}=〈\mathit{\text{value}}〉$ and ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=7$
On entry, lr is too small when ${\mathbf{mode}}=0$ or $2$: ${\mathbf{lr}}=〈\mathit{\text{value}}〉$, minimum length required $\text{}=〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=8$
On entry, state vector has been corrupted or not initialized.
${\mathbf{ifail}}=10$
On entry, ${\mathbf{ldx}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{n}}$.
${\mathbf{ifail}}=210$
On entry, ${\mathbf{ldx}}=〈\mathit{\text{value}}〉$ and ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{k}}$.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

Not applicable.

## 8Parallelism and Performance

g05tgf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

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.

## 10Example

This example prints $20$ pseudorandom $k$-dimensional variates from a multinomial distribution with $k=4$, $m=6000$, ${p}_{1}=0.08$, ${p}_{2}=0.1$, ${p}_{3}=0.8$ and ${p}_{4}=0.02$, generated by a single call to g05tgf, after initialization by g05kff.

### 10.1Program Text

Program Text (g05tgfe.f90)

### 10.2Program Data

Program Data (g05tgfe.d)

### 10.3Program Results

Program Results (g05tgfe.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017