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 pi = p[i-1]) is

f(x1,x2,xk)=n!x1!x2!xk!px11px22pxkk

for xi0 and

k1i=0xi=n

The deviate in each row of r is produced by generation of the binomial deviate x0 with parameters n and pi and then by successive generations of the conditional binomial deviates xj given x0,x1,,:xj2 with parameters nx0x1xj2 and pj/(1p0p1pj2).

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