PDF version (NAG web site
, 64-bit version, 64-bit version)
NAG Toolbox: nag_rand_int_multinomial (g05tg)
Purpose
nag_rand_int_multinomial (g05tg) generates a sequence of variates, each consisting of pseudorandom integers, from the discrete multinomial distribution with outcomes and trials, where the outcomes have probabilities 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
groups of
integers
, for
and
, from a multinomial distribution with
trials and
outcomes, where the probability of
for each
is
where
A single trial can have several outcomes (
) and the probability of achieving each outcome is known (
). After
trials each outcome will have occurred a certain number of times. The
numbers representing the numbers of occurrences for each outcome after
trials is then a single sample from the multinomial distribution defined by the parameters
,
and
, for
. This function returns
such samples.
When
this distribution is equivalent to the binomial distribution with parameters
and
(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
.
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:
– int64int32nag_int scalar
-
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_rand_int_multinomial (g05tg).
- Set up reference vector and generate variates.
- Generate variates without using the reference vector.
Constraint:
, , or .
- 2:
– int64int32nag_int scalar
-
, the number of pseudorandom numbers to be generated.
Constraint:
.
- 3:
– int64int32nag_int scalar
-
, the number of trials of the multinomial distribution.
Constraint:
.
- 4:
– double array
-
Contains the probabilities
, for , of the possible outcomes of the multinomial distribution.
Constraint:
and .
- 5:
– double array
-
lr, the dimension of the array, must satisfy the constraint
- if or ,
;
- if , lr must remain unchanged from the previous call to nag_rand_int_multinomial (g05tg).
If
, the reference vector from the previous call to
nag_rand_int_multinomial (g05tg).
If
,
r is not referenced.
- 6:
– 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:
– int64int32nag_int scalar
-
Default:
the dimension of the array
p.
, the number of possible outcomes of the multinomial distribution.
Constraint:
.
Output Parameters
- 1:
– double array
-
If , the reference vector.
- 2:
– int64int32nag_int array
-
Contains updated information on the state of the generator.
- 3:
– int64int32nag_int array
-
The first rows of each contain pseudorandom numbers representing a -dimensional variate from the specified multinomial distribution.
- 4:
– int64int32nag_int scalar
unless the function detects an error (see
Error Indicators and Warnings).
Error Indicators and Warnings
Errors or warnings detected by the function:
-
-
Constraint: , , or .
-
-
Constraint: .
-
-
Constraint: .
-
-
Constraint: .
-
-
On entry, at least one element of the vector
p is less than
or greater than
.
On entry, the sum of the elements of
p do not equal one.
-
-
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.
-
-
On entry, lr is too small when or .
-
-
On entry,
state vector has been corrupted or not initialized.
-
-
Constraint: .
-
-
Constraint: .
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
Dynamic memory allocation failed.
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
This example prints
pseudorandom
-dimensional variates from a multinomial distribution with
,
,
,
,
and
, generated by a single call to
nag_rand_int_multinomial (g05tg), after initialization by
nag_rand_init_repeat (g05kf).
Open in the MATLAB editor:
g05tg_example
function g05tg_example
fprintf('g05tg example results\n\n');
seed = [int64(1762543)];
genid = int64(1);
subid = int64(1);
[state, ifail] = g05kf( ...
genid, subid, seed);
n = int64(20);
m = int64(6000);
p = [0.08; 0.1; 0.8; 0.02];
mode = int64(2);
r = zeros(6007, 1);
[r, state, x, ifail] = g05tg( ...
mode, n, m, p, r, state);
disp('Variates');
disp(double(x));
g05tg example results
Variates
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
PDF version (NAG web site
, 64-bit version, 64-bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015