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 ofp
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
for \(x_i\geq 0\) and
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