hide long namesshow long names
hide short namesshow short names
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_multinomial (g05tg)

Purpose

nag_rand_int_multinomial (g05tg) generates a sequence of nn variates, each consisting of kk pseudorandom integers, from the discrete multinomial distribution with kk outcomes and mm trials, where the outcomes have probabilities p1,p2,,pkp1,p2,,pk 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 nn groups of kk integers xi,jxi,j, for j = 1,2,,kj=1,2,,k and i = 1,2,,ni=1,2,,n, from a multinomial distribution with mm trials and kk outcomes, where the probability of xi,j = Ijxi,j=Ij for each j = 1,2,,kj=1,2,,k is
k
P(i1 = I1,,ik = Ik) = (m ! )/(j = 1k Ij ! )pjIj = (m ! )/(I1 ! I2 ! Ik ! )p1I1p2I2pkIk,
j = 1
P(i1=I1,,ik=Ik)= m! j=1k Ij! j=1k pjIj= m! I1!I2!Ik! p1I1p2I2pkIk,
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 (kk) and the probability of achieving each outcome is known (pjpj). After mm trials each outcome will have occurred a certain number of times. The kk numbers representing the numbers of occurrences for each outcome after mm trials is then a single sample from the multinomial distribution defined by the parameters kk, mm and pjpj, for j = 1,2,,kj=1,2,,k. This function returns nn such samples.
When k = 2k=2 this distribution is equivalent to the binomial distribution with parameters mm and p = p1p=p1 (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 mm.
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 = 0mode=0
Set up reference vector only.
mode = 1mode=1
Generate variates using reference vector set up in a prior call to nag_rand_int_multinomial (g05tg).
mode = 2mode=2
Set up reference vector and generate variates.
mode = 3mode=3
Generate variates without using the reference vector.
Constraint: mode = 0mode=0, 11, 22 or 33.
2:     n – int64int32nag_int scalar
nn, the number of pseudorandom numbers to be generated.
Constraint: n0n0.
3:     m – int64int32nag_int scalar
mm, the number of trials of the multinomial distribution.
Constraint: m0m0.
4:     p(k) – double array
k, the dimension of the array, must satisfy the constraint k2k2.
Contains the probabilities pjpj, for j = 1,2,,kj=1,2,,k, of the kk possible outcomes of the multinomial distribution.
Constraint: 0.0p(j)1.00.0pj1.0 and j = 1kp(j) = 1.0j=1kpj=1.0.
5:     r(lr) – double array
lr, the dimension of the array, must satisfy the constraint
  • if mode = 0mode=0 or 22,
    lr > min (m,INT[m × p_max + 7.25 × sqrt(m × p_max × (1p_max) ) + 8.5])
    max (0,INT[m × p_max7.25 × sqrt(m × p_max × (1p_max) )]) + 9
    lr > min(m,INT[ m×p_max+7.25 × m× p_max×(1-p_max) +8.5 ]) - max(0,INT[ m×p_max-7.25 × m×p_max× (1-p_max) ]) +9 ;
  • if mode = 1mode=1, lr must remain unchanged from the previous call to nag_rand_int_multinomial (g05tg).
If mode = 1mode=1, the reference vector from the previous call to nag_rand_int_multinomial (g05tg).
If mode = 3mode=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.
kk, the number of possible outcomes of the multinomial distribution.
Constraint: k2k2.

Input Parameters Omitted from the MATLAB Interface

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
ldxnldxn.
The first nn rows of x(i,j)xij each contain kk pseudorandom numbers representing a kk-dimensional variate from the specified multinomial distribution.
4:     ifail – int64int32nag_int scalar
ifail = 0ifail=0 unless the function detects an error (see [Error Indicators and Warnings]).

Error Indicators and Warnings

Errors or warnings detected by the function:
  ifail = 1ifail=1
On entry, mode0mode0, 11, 22 or 33.
  ifail = 2ifail=2
On entry, n < 0n<0.
  ifail = 3ifail=3
On entry, m < 0m<0.
  ifail = 4ifail=4
On entry, k < 2k<2.
  ifail = 5ifail=5
On entry, p(j) < 0.0pj<0.0 or p(j) > 1.0pj>1.0 for at least one value of jj.
The probabilities p(j)pj, for j = 1,2,,kj=1,2,,k, do not add up to 11.
  ifail = 6ifail=6
The maximum value of p(j)pj, for j = 1,2,,kj=1,2,,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 = 0mode=0 or 22.
On entry, the r vector was not initialized correctly, or has been corrupted.
  ifail = 7ifail=7
On entry, lr is too small when mode = 0mode=0 or 22.
  ifail = 8ifail=8
On entry,state vector was not initialized or has been corrupted.
  ifail = 10ifail=10
On entry, ldx < nldx<n.

Accuracy

Not applicable.

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

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



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