randomMultinomial

Generates pseudorandom numbers from a multinomial distribution.

Synopsis

randomMultinomial (nRandom, n, p)

Required Arguments

int nRandom (Input)
Number of random multinomial vectors to generate.
int n (Input)
Multinomial parameter indicating the number of independent trials.
float p[] (Input)
Vector of length k containing the probabilities of the possible outcomes. The elements of p must be positive and must sum to 1.0.

Return Value

nRandom by k matrix containing the random multinomial vectors in its rows.

Description

Function randomMultinomial generates pseudorandom numbers from a K-variate multinomial distribution with parameters n and p. k and n must be positive. Each element of p must be positive and the elements must sum to 1. The probability function (with n = n, k = k, and \(p_i\) = p[i-1]) is

\[f\left(x_1,x_2,\ldots x_{\mathrm{k}}\right) = \frac{n!}{x_1!x_2! \ldots x_{\mathrm{k}}!} p_1^{\mathrm{x}_1} p_2^{\mathrm{x}_2} \ldots p_{\mathrm{k}}^{\mathrm{x}_{\mathrm{k}}}\]

for \(x_i\geq 0\) and

\[\sum_{i=0}^{k-1} x_i = n\]

The deviate in each row of r is produced by generation of the binomial deviate \(x_0\) with parameters n and \(p_i\) and then by successive generations of the conditional binomial deviates \(x_j\) given \(x_0,x_1,\ldots,:x_{j-2}\) with parameters \(n-x_0-x_1-\ldots -x_{j-2}\) and \(p_j/(1-p_0-p_1-\ldots-p_{j-2})\).

Example

In this example, randomMultinomial is used to generate five pseudorandom 3-dimensional multinomial variates with parameters n = 20 and p = [0.1, 0.3, 0.6].

from numpy import *
from pyimsl.stat.randomMultinomial import randomMultinomial
from pyimsl.stat.randomSeedSet import randomSeedSet
from pyimsl.stat.writeMatrix import writeMatrix

n_random = 5
n = 20
k = 3
p = [.1, .3, .6]
randomSeedSet(123457)
r = randomMultinomial(n_random, n, p)
writeMatrix("Multinomial Random Deviates", r,
            noRowLabels=True, noColLabels=True)

Output

 
     Multinomial Random Deviates
          5            4           11
          3            6           11
          3            3           14
          5            5           10
          4            5           11