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

Postby j.galimberti » Mon Aug 02, 2010 4:11 pm

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=3
matrix(!size,!size) rnd_covs
call 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)
wend
call rnd_multivariate_normal(rnd_covs)

subroutine local rnd_pos_def_mat(matrix mat_res)
!size=@columns(mat_res)
matrix(!size,!size) tmp=0
for !i=1 to !size
   tmp(!i,!i)=@sqrt(@rfdist(100,100))
   for !j=!i+1 to !size
      tmp(!i,!j)=@nrnd
   next
next
matrix mat_res=@transpose(tmp)*tmp
endsub

subroutine rnd_multivariate_normal(matrix covs)
sym covs_sym=@implode(covs)
matrix cholesky=@cholesky(covs_sym)
matrix(@columns(covs),@obssmpl) vz_mat
for !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_norm
next
matrix mat_xs=cholesky*vz_mat
matrix mat_xs=@transpose(mat_xs)
mtos(mat_xs,xs)
delete covs_sym cholesky vz_mat mat_xs
endsub
Galimberti, J. K.
MSc in Economics at Federal University of Santa Catarina (Brazil)
Going to PhD in Economics at University of Manchester (UK)
Website: http://sites.google.com/site/jkgeconoeng/

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

Re: Random draws from multivariate normal distribution

Postby miksterz1 » Fri May 20, 2011 7:08 am

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: 11378
Joined: Tue Sep 16, 2008 5:38 pm

Re: Random draws from multivariate normal distribution

Postby EViews Gareth » Fri May 20, 2011 8:25 am

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).
Follow us on Twitter @IHSEViews

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

Re: Random draws from multivariate normal distribution

Postby miksterz1 » Sun May 22, 2011 1:10 am

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 estimated
matrix covs = myvar.@residcov

' this is the subroutine taken from the posted code

subroutine rnd_multivariate_normal
sym covs_sym=@implode(covs)
matrix cholesky=@cholesky(covs_sym)
matrix(@columns(covs),@obssmpl) vz_mat
for !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_norm
next
matrix mat_xs=cholesky*vz_mat
matrix mat_xs=@transpose(mat_xs)
mtos(mat_xs,xs)
delete covs_sym cholesky vz_mat mat_xs
endsub




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

Postby j.galimberti » Sun May 22, 2011 2:43 am

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

Postby miksterz1 » Mon May 23, 2011 2:54 pm

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.


Return to “Program Repository”

Who is online

Users browsing this forum: No registered users and 1 guest