Page 111 - Computational Statistics Handbook with MATLAB
P. 111

98                         Computational Statistics Handbook with MATLAB


                             Example 4.10
                             The function csmvrnd generates multivariate normal random variables
                             using the Cholesky factorization. Note that we are transposing the transfor-
                             mation given in Equation 4.18, yielding the following

                                                        X =  ZR +  µ T  ,

                             where X is an  n ×  d   matrix of d-dimensional random variables and Z is an
                             n ×  d   matrix of standard normal random variables.

                                % function X = csmvrnd(mu,covm,n);
                                % This function will return n multivariate random
                                % normal variables with d-dimensional mean mu and
                                % covariance matrix covm. Note that the covariance
                                % matrix must be positive definite (all eigenvalues
                                % are greater than zero), and the mean
                                % vector is a column

                                function X = csmvrnd(mu,covm,n)
                                d = length(mu);
                                % Get Cholesky factorization of covariance.
                                R = chol(covm);
                                % Generate the standard normal random variables.
                                Z = randn(n,d);
                                X = Z*R + ones(n,1)*mu';
                             We illustrate its use by generating some multivariate normal random vari-
                                              ,
                                        T
                             ables with µ =  – (  23)   and covariance

                                                              10.7
                                                        Σ =         .
                                                             0.7 1

                                % Generate the multivariate random normal variables.
                                mu = [-2;3];
                                covm = [1 0.7 ; 0.7 1];
                                X = csmvrnd(mu,covm,500);

                             To check the results, we plot the random variables in a scatterplot in
                             Figure 4.7. We can also calculate the sample mean and sample covariance
                             matrix to compare with what we used as input arguments to csmvrnd. By
                             typing mean(X) at the command line, we get

                                -2.0629    2.9394

                             Similarly, entering corrcoef(X)at the command line yields


                            © 2002 by Chapman & Hall/CRC
   106   107   108   109   110   111   112   113   114   115   116