Multinomial Distribution - 5.2 English - 68552

AOCL API Guide (68552)

Document ID
68552
Release Date
2025-12-29
Version
5.2 English

Generates a matrix of random variates from a Multinomial distribution with probability, \(f (X)\), defined by:

\[f(X) = \frac{M!}{\prod_{i=1}^K X_i!} \left(\prod_{i=1}^K P_i^{X_i}\right)\]

where \(X = {{X_1, X_2,\dotsi,X_K} }, P ={ {P_1, P_2,\dotsi, P_K}},\sum_{𝑖=1}^{𝑘}\ X_i=1\ \) \(\ and\ \sum_{i=1}^{k}\ P_i=1\)

C Generate 100 values from the Multinomial distribution
    INTEGER LSTATE,N,M
    PARAMETER (LSTATE=16,N=100,M=10)
    INTEGER I,J,INFO,SEED(1),STATE(LSTATE)
    INTEGER LDX,K
    INTEGER X(N,M)
    DOUBLE PRECISION P(M)

C Set array sizes
    LDX = N

C Set the seed
    SEED(1) = 1234

C Read in the distributional parameters
    READ(5,*) K
    READ(5,*) (P(I),I=1,K)

C Initialize the STATE vector
    CALL DRANDINITIALIZE(1,1,SEED,1,STATE,LSTATE,INFO)

C Generate N variates from the Multinomial distribution
    CALL DRANDMULTINOMIAL(N,M,P,K,STATE,X,LDX,INFO)

C Print the results
    DO 20 I = 1,N
      WRITE(6,*) (X(I,J),J=1,K)
    20 CONTINUE