RANDMULTINOMIAL (N, NumTrials, Prob);
The RANDMULTINOMIAL function is part of the IMLMLIB library . The RANDMULTINOMIAL function generates a random sample from a multinomial distribution, which is a multivariate generalization of the binomial distribution.
The input parameters are as follows:
is the number of observations to sample.
is the number of trials. , for
.
is a vector of probabilities with
and
.
For each trial, is the probability of event
, where the
are mutually exclusive and
.
The RANDMULTINOMIAL function returns an matrix that contains N observations of
random draws from the multinomial distribution. Each row of the resulting matrix is an integer vector
with
. That is, for each row,
indicates how many times event
occurred in
trials.
If follows a multinomial distribution with n trials and probabilities
, then
the probability density function for x is
the expected value of is
.
the variance of is
.
the covariance of with
is
.
if then X is constant.
if then
is Binomial
and
is Binomial
.
The following example generates 1,000 samples from a multinomial distribution with three mutually exclusive events. For each
sample, 10 events are generated. Each row of the returned matrix x
represents the number of times each event is observed. The example also computes the sample mean and covariance and compares
them with the expected values.
call randseed(1); prob = {0.3,0.6,0.1}; NumTrials = 10; N = 1000; x = RandMultinomial(N,NumTrials,prob); /* population mean and covariance */ Mean = NumTrials * prob`; Cov = -NumTrials*prob*prob`; /* replace diagonal elements of Cov with Variance */ Variance = NumTrials*prob#(1-prob); do i = 1 to nrow(prob); Cov[i,i] = Variance[i]; end; SampleMean = mean(x); SampleCov = cov(x); print SampleMean Mean, SampleCov Cov;
For further details about sampling from the multinomial distribution, see Gentle (2003), or Fishman (1996).