g05tg 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 p1,p2,,pk respectively.

Syntax

C#
public static void g05tg(
	int mode,
	int n,
	int m,
	int k,
	double[] p,
	double[] r,
	G05..::..G05State g05state,
	int[,] x,
	out int ifail
)
Visual Basic
Public Shared Sub g05tg ( _
	mode As Integer, _
	n As Integer, _
	m As Integer, _
	k As Integer, _
	p As Double(), _
	r As Double(), _
	g05state As G05..::..G05State, _
	x As Integer(,), _
	<OutAttribute> ByRef ifail As Integer _
)
Visual C++
public:
static void g05tg(
	int mode, 
	int n, 
	int m, 
	int k, 
	array<double>^ p, 
	array<double>^ r, 
	G05..::..G05State^ g05state, 
	array<int,2>^ x, 
	[OutAttribute] int% ifail
)
F#
static member g05tg : 
        mode : int * 
        n : int * 
        m : int * 
        k : int * 
        p : float[] * 
        r : float[] * 
        g05state : G05..::..G05State * 
        x : int[,] * 
        ifail : int byref -> unit 

Parameters

mode
Type: System..::..Int32
On entry: a code for selecting the operation to be performed by the method.
mode=0
Set up reference vector only.
mode=1
Generate variates using reference vector set up in a prior call to g05tg.
mode=2
Set up reference vector and generate variates.
mode=3
Generate variates without using the reference vector.
Constraint: mode=0, 1, 2 or 3.
n
Type: System..::..Int32
On entry: n, the number of pseudorandom numbers to be generated.
Constraint: n0.
m
Type: System..::..Int32
On entry: m, the number of trials of the multinomial distribution.
Constraint: m0.
k
Type: System..::..Int32
On entry: k, the number of possible outcomes of the multinomial distribution.
Constraint: k2.
p
Type: array<System..::..Double>[]()[][]
An array of size [k]
On entry: contains the probabilities pj, for j=1,2,,k, of the k possible outcomes of the multinomial distribution.
Constraint: 0.0p[j-1]1.0 and j=1kp[j-1]=1.0.
r
Type: array<System..::..Double>[]()[][]
An array of size [lr]
On entry: if mode=1, the reference vector from the previous call to g05tg.
If mode=3, r is not referenced.
On exit: if mode3, the reference vector.
g05state
Type: NagLibrary..::..G05..::..G05State
An Object of type G05.G05State.
x
Type: array<System..::..Int32,2>[,](,)[,][,]
An array of size [dim1, _tdx]
Note: dim1 must satisfy the constraint: dim1n
On exit: the first n rows of x[i-1,j-1] each contain k pseudorandom numbers representing a k-dimensional variate from the specified multinomial distribution.
ifail
Type: System..::..Int32%
On exit: ifail=0 unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

Description

g05tg generates a sequence of n groups of k integers xi,j, for j=1,2,,k and i=1,2,,n, from a multinomial distribution with m trials and k outcomes, where the probability of xi,j=Ij for each j=1,2,,k is
Pi1=I1,,ik=Ik=m!j=1kIj!j=1kpjIj=m!I1!I2!Ik!p1I1p2I2pkIk,
where
j=1kpj=1  and  j=1kIj=m.
A single trial can have several outcomes (k) and the probability of achieving each outcome is known (pj). 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 pj, for j=1,2,,k. This method returns n such samples.
When k=2 this distribution is equivalent to the binomial distribution with parameters m and p=p1 (see 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 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 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.
One of the initialization methods (G05KFF not in this release) (for a repeatable sequence if computed sequentially) or (G05KGF not in this release) (for a non-repeatable sequence) must be called prior to the first call to g05tg.

References

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

Error Indicators and Warnings

Errors or warnings detected by the method:
Some error messages may refer to parameters that are dropped from this interface (LDX) In these cases, an error in another parameter has usually caused an incorrect value to be inferred.
ifail=1
On entry, mode0, 1, 2 or 3.
ifail=2
On entry, n<0.
ifail=3
On entry, m<0.
ifail=4
On entry, k<2.
ifail=5
On entry, p[j-1]<0.0 or p[j-1]>1.0 for at least one value of j.
The probabilities p[j-1], for j=1,2,,k, do not add up to 1.
ifail=6
The maximum value of p[j-1], for j=1,2,,k, or m is not the same as when r was set up in a previous call to g05tg with mode=0 or 2.
On entry, the r vector was not initialized correctly, or has been corrupted.
ifail=7
On entry, lr is too small when mode=0 or 2.
ifail=8
On entry,state vector was not initialized or has been corrupted.
ifail=10
On entry, ldx<n.
ifail=-9000
An error occured, see message report.
ifail=-6000
Invalid Parameters value
ifail=-4000
Invalid dimension for array value
ifail=-8000
Negative dimension for array value
ifail=-6000
Invalid Parameters value

Accuracy

Not applicable.

Parallelism and Performance

None.

Further Comments

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

See Also