Random draws from multivariate normal distribution

For posting your own programs to share with others

Moderators: EViews Gareth, EViews Moderator

j.galimberti
Posts: 23
Joined: Fri Feb 06, 2009 2:53 am

Random draws from multivariate normal distribution

Dear all,

It follows a code that I have developed during some other works. It generates !size series randomly distributed according to a multivariate normal with the covariance matrix also generated randomly. This may be changed to generate according to the desired covariance matrix. Just make sure that the cov. matrix is positive semi-definite.

It might be useful for some...

Cheers!

Code: Select all

`!size=3matrix(!size,!size) rnd_covscall rnd_pos_def_mat(rnd_covs)'' I was having some trouble with the near singular matrix problem. This while was my best solution for now...while @issingular(@implode(rnd_covs))=1 ''   call rnd_pos_def_mat(rnd_covs)wendcall rnd_multivariate_normal(rnd_covs)subroutine local rnd_pos_def_mat(matrix mat_res)!size=@columns(mat_res)matrix(!size,!size) tmp=0for !i=1 to !size   tmp(!i,!i)=@sqrt(@rfdist(100,100))   for !j=!i+1 to !size      tmp(!i,!j)=@nrnd   nextnextmatrix mat_res=@transpose(tmp)*tmpendsubsubroutine rnd_multivariate_normal(matrix covs)sym covs_sym=@implode(covs)matrix cholesky=@cholesky(covs_sym)matrix(@columns(covs),@obssmpl) vz_matfor !i=1 to @columns(covs)   series z_norm=nrnd   stom(z_norm,vz_norm)   rowplace(vz_mat,@transpose(vz_norm),!i)   delete z_norm vz_normnextmatrix mat_xs=cholesky*vz_matmatrix mat_xs=@transpose(mat_xs)mtos(mat_xs,xs)delete covs_sym cholesky vz_mat mat_xsendsub`
Galimberti, J. K.
MSc in Economics at Federal University of Santa Catarina (Brazil)
Going to PhD in Economics at University of Manchester (UK)

miksterz1
Posts: 33
Joined: Fri May 20, 2011 5:20 am

Re: Random draws from multivariate normal distribution

You seem to be calling subroutines that are not defined within the code you provided. Unless I am missing something obvious, this makes it impossible to use. Would also be nice if there were some comments within the code just to be clear about what each step is doing!

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 11943
Joined: Tue Sep 16, 2008 5:38 pm

Re: Random draws from multivariate normal distribution

I think all the subroutines are defined. I also think it is relatively straight forward to follow.

You can run his program "as is" (as long as you have a workfile open).

miksterz1
Posts: 33
Joined: Fri May 20, 2011 5:20 am

Re: Random draws from multivariate normal distribution

Sorry about that. Was making a silly mistake but I see now it can be run as is. I am trying to use it with my own defined covariance matrix rather than the random one generated in this code. Am I correct that only the following code is relevant once I have defined by covariance matrix?

Code: Select all

`' this is my covariance matrix of the residuals from a var I estimatedmatrix covs = myvar.@residcov' this is the subroutine taken from the posted codesubroutine rnd_multivariate_normalsym covs_sym=@implode(covs)matrix cholesky=@cholesky(covs_sym)matrix(@columns(covs),@obssmpl) vz_matfor !i=1 to @columns(covs)   series z_norm=nrnd   stom(z_norm,vz_norm)   rowplace(vz_mat,@transpose(vz_norm),!i)   delete z_norm vz_normnextmatrix mat_xs=cholesky*vz_matmatrix mat_xs=@transpose(mat_xs)mtos(mat_xs,xs)delete covs_sym cholesky vz_mat mat_xsendsub`

When I call this subroutine it seems to work, generating 3 series which should follow a multivariate normal distribution. However, when I obtain the covariance matrix of these generated series it does not match the one I specified. Any idea what I am missing?

j.galimberti
Posts: 23
Joined: Fri Feb 06, 2009 2:53 am

Re: Random draws from multivariate normal distribution

Can you please send us your covariance matrix and the number of observations?
Notice that things are not supposed to match exactly (as they are random), but asymptoticaly they tend to be equal. Thus the number of observations in your sample may affect how these matrices "match". One way to test for this is to copy your covariance matrix into a new workfile (or page) at which the number of observations is bigger (say 100000). Then generate the random draws and compare the covariance matrix of the generated series with the original one.

Best regards.

miksterz1
Posts: 33
Joined: Fri May 20, 2011 5:20 am

Re: Random draws from multivariate normal distribution

I increased the number of observations and it seems to get closer, so that must be it. Thanks for this program. It is extremely useful.