PROCEDURE MULTNORM NCOL NROW CORMAT MN

* MULTNORM.SRC GENERATES A SAMPLE FROM A MULTIVARIATE
* NORMAL POPULATION. THE USER SPECIFIES THE NUMBER
* OF VARIABLES (NCOL), THE SAMPLE SIZE (NROW), AND
* THE CORRELATION MATRIX (CORMAT). THE USER ALSO DESIGNATES
* THE MATRIX (MN) WHERE THE SAMPLE IS TO BE STORED. BOTH
* CORMAT AND MN SHOULD BE DECLARED IN THE PROGRAM THAT
* CALLS MULTNORM.SRC; FOR EXAMPLE:
*            DEC SYMM CORMAT(NCOL,NCOL)
*            DEC RECT MN(NROW,NCOL)
*
* SINCE CORMAT IS SYMMETRIC, ONLY THE ELEMENTS ON
* AND BELOW THE MAIN DIAGONAL MUST BE SPECIFIED.
* A CORRELATION MATRIX MUST BE POSITIVE DEFINITE.
* BEFORE INVOKING MULTNORM.SRC, THE USER MAY WANT
* TO VERIFY THAT CORMAT IS IN FACT POSITIVE DEFINITE BY
* COMPUTING ITS EIGENVALUES WITH THE RATS INSTRUCTION
* EIGEN.
*
* THE ELEMENTS OF MN ARE STANDARD NORMAL VARIABLES. AFTER
* MN HAS BEEN GENERATED, THE USER CAN EASILY CHANGE THE
* MEANS AND STANDARD DEVIATIONS.

TYPE INTEGER NROW NCOL
TYPE RECT MN
TYPE SYMM CORMAT
LOCAL VEC A
LOCAL RECT C
LOCAL INTEGER I J K
LOCAL REAL SUM
DIM A(NCOL) C(NCOL,NCOL)

* FIND THE SQUARE ROOT OF CORMAT USING THE CHOLESKI
* DECOMPOSITION

COMPUTE C = %DECOMP(CORMAT)

* PUT THE MULTINORMAL SAMPLE INTO ARRAY MN

DO I = 1,NROW
COMPUTE A = %RAN(1.0)
DO J = 1,NCOL
COMPUTE SUM = 0.0
DO K = 1,NCOL
COMPUTE SUM = SUM + C(J,K)*A(K)
END DO K
COMPUTE MN(I,J) = SUM
END DO J
END DO I
END
