Module Library

RANDMULTINOMIAL Function

generates a random sample from a multinomial distribution

RANDMULTINOMIAL( N, NumTrials, Prob )

The inputs are as follows:


n
is the number of desired observations sampled from the distribution.

numtrials
is the number of trials for each observation. numtrials[j] \geq 0, for j = 1  ...  p.

Prob
is a 1 x p vector of probabilities with 0 \lt {prob}[j] \leq 1 and \sigma_{j=1}^p {prob}[j] = 1.

The multinomial distribution is a multivariate generalization of the binomial distribution. For each trial, {prob}[j] is the probability of event e_j, where the e_j are mutually exclusive and \sigma_{j=1}^p {prob}[j] = 1.

The RANDMULTINOMIAL function returns an n x p matrix containing n observations of {numtrials} random draws from the multinomial distribution. Each row of the resulting matrix is an integer vector \{x_1 \, x_2 \,  ...  \, x_p\} with \sigma x_j = {numtrials}. That is, for each row, x_j indicates how many times event e_j occurred in {numtrials} trials.

If x=\{x_1 \, x_2 \,  ...  \, x_p\} follows a multinomial distribution with n trials and probabilities \rho = \{\rho_1 \, \rho_2 \,  ...  \, \rho_p\}, then

The following example generates 1000 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 was observed. The example then computes the sample mean and covariance and compares them with the expected values. Here are the code and the output:

  
    call randseed(1); 
    prob = {0.3,0.6,0.1}; 
    NumTrials = 10; 
    N = 1000; 
    x = RANDMULTINOMIAL(N,NumTrials,prob); 
    ExpectedValue = NumTrials * prob`; 
    Cov = -NumTrials*prob*prob`; 
    /* replace diagonal elements of Cov with Variance */ 
    Variance = -NumTrials*prob#(1-prob); 
    d = nrow(prob); 
    do i = 1 to d; 
       Cov[i,i] = Variance[i]; 
    end; 
  
    SampleMean = x[:,]; 
    n = nrow(x); 
    y = x - repeat( SampleMean, n ); 
    SampleCov = y`*y / (n-1); 
    print SampleMean, ExpectedValue, SampleCov, Cov; 
  
  
             SampleMean                          ExpectedValue 
  
      2.971     5.972     1.057              3        6        1 
  
             SampleCov                               Cov 
  
      2.0622212 -1.746559 -0.315663        -2.1     -1.8     -0.3 
      -1.746559 2.3775936 -0.631035        -1.8     -2.4     -0.6 
      -0.315663 -0.631035 0.9466977        -0.3     -0.6     -0.9
 

For further details about sampling from the multinomial distribution, see Gentle 2003, p. 198, or Fishman 1996, pp. 224 - 225.

Previous Page | Next Page | Top of Page