nag_rngs_gen_multinomial (g05mrc) (PDF version)
g05 Chapter Contents
g05 Chapter Introduction
NAG C Library Manual

NAG Library Function Document

nag_rngs_gen_multinomial (g05mrc)

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

nag_rngs_gen_multinomial (g05mrc) 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.

2  Specification

#include <nag.h>
#include <nagg05.h>
void  nag_rngs_gen_multinomial (Nag_OrderType order, Integer mode, Integer m, Integer k, const double p[], Integer n, Integer x[], Integer pdx, Integer igen, Integer iseed[], double r[], NagError *fail)

3  Description

nag_rngs_gen_multinomial (g05mrc) 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=1k Ij! j=1k pjIj= m! I1!I2!Ik! p1I1p2I2pkIk,
j= 1k pj= 1  and   j= 1k Ij=m.
A single trial can have several outcomes (k, say) and the probability of achieving each outcome is known (pj, say). 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 function returns n such samples with each sample being stored as a row in a two-dimensional array of integers.
When k=2 this distribution is equivalent to the binomial distribution with parameters m and p=p1 (nag_rngs_binomial (g05mjc)).
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_rngs_gen_multinomial (g05mrc) with the same parameter values can then use this reference vector to generate further variates. The reference array is only generated for the outcome with greatest probability. The number of successes for the outcome with greatest probability is calculated first as for the binomial distribution (nag_rngs_binomial (g05mjc)); 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 functions nag_rngs_init_repeatable (g05kbc) (for a repeatable sequence if computed sequentially) or nag_rngs_init_nonrepeatable (g05kcc) (for a non-repeatable sequence) must be called prior to the first call to nag_rngs_gen_multinomial (g05mrc).

4  References

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

5  Arguments

1:     orderNag_OrderTypeInput
On entry: the order argument specifies the two-dimensional storage scheme being used, i.e., row-major ordering or column-major ordering. C language defined storage is specified by order=Nag_RowMajor. See Section in the Essential Introduction for a more detailed explanation of the use of this argument.
Constraint: order=Nag_RowMajor or Nag_ColMajor.
2:     modeIntegerInput
On entry: a code for selecting the operation to be performed by the function.
Set up reference vector only.
Generate variates using reference vector set up in a prior call to nag_rngs_gen_multinomial (g05mrc).
Set up reference vector and generate variates.
Generate variates without using the reference vector.
Constraint: mode=0, 1, 2 or 3.
3:     mIntegerInput
On entry: m, the number of trials of the multinomial distribution.
Constraint: m0.
4:     kIntegerInput
On entry: k, the number of possible outcomes of the multinomial distribution.
Constraint: k2.
5:     p[k]const doubleInput
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.
6:     nIntegerInput
On entry: n, the number of pseudorandom numbers to be generated.
Constraint: n1.
7:     x[dim]IntegerOutput
Note: the dimension, dim, of the array x must be at least
  • max1,pdx×k when order=Nag_ColMajor;
  • max1,n×pdx when order=Nag_RowMajor.
Where Xi,j appears in this document, it refers to the array element
  • x[j-1×pdx+i-1] when order=Nag_ColMajor;
  • x[i-1×pdx+j-1] when order=Nag_RowMajor.
On exit: the first n rows of Xi,j each contain k pseudorandom numbers representing a k-dimensional variate from the specified multinomial distribution.
8:     pdxIntegerInput
On entry: the stride separating row or column elements (depending on the value of order) in the array x.
  • if order=Nag_ColMajor, pdxn;
  • if order=Nag_RowMajor, pdxk.
9:     igenIntegerInput
On entry: must contain the identification number for the generator to be used to return a pseudorandom number and should remain unchanged following initialization by a prior call to nag_rngs_init_repeatable (g05kbc) or nag_rngs_init_nonrepeatable (g05kcc).
10:   iseed[4]IntegerCommunication Array
On entry: contains values which define the current state of the selected generator.
On exit: contains updated values defining the new state of the selected generator.
11:   r[dim]doubleCommunication Array
Note: the dimension, dim, of the array r must be at least
  • 22+20m×p_max1-p_max when mode3;
  • 1 otherwise.
On entry: if mode=1, the reference vector from the previous call to nag_rngs_gen_multinomial (g05mrc).
On exit: the reference vector.
12:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

6  Error Indicators and Warnings

Dynamic memory allocation failed.
On entry, argument value had an illegal value.
On entry, p[i-1]<0.0 or p[i-1]>1.0 where: i=value and p[i-1]=value.
On entry, k=value.
Constraint: k2.
On entry, m=value.
Constraint: m0.
On entry, mode=value.
Constraint: mode=0, 1, 2 or 3.
On entry, n=value.
Constraint: n1.
On entry, pdx=value.
Constraint: pdx>0.
On entry, pdx=value and k=value.
Constraint: pdxk.
On entry, pdx=value and n=value.
Constraint: pdxn.
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
maxp[i-1] or m is not the same as when r was set up in a previous call. Previous value of maxp[i-1]=value and maxp[i-1]=value. Previous value of m=value and m=value.
On entry, mode=1, but either r has not been set up in prior call or r has become corrupted.
On entry, the sum of p[i-1], for i=1,2,,k, is not unity. The difference from unity in the summation is: value.

7  Accuracy

Not applicable.

8  Further Comments

Only the reference vector for 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.

9  Example

This example prints 20 pseudorandom k-dimensional variates from a multinomial distribution with k=4, m=6000, p1=0.08, p2=0.1, p3=0.8 and p4=0.02, generated by a single call to nag_rngs_gen_multinomial (g05mrc), after initialization by nag_rngs_init_repeatable (g05kbc).

9.1  Program Text

Program Text (g05mrce.c)

9.2  Program Data


9.3  Program Results

Program Results (g05mrce.r)

nag_rngs_gen_multinomial (g05mrc) (PDF version)
g05 Chapter Contents
g05 Chapter Introduction
NAG C Library Manual

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