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: 13294
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.

GabrielV
Posts: 2
Joined: Wed Jan 13, 2021 9:30 am

Re: Random draws from multivariate normal distribution

Postby GabrielV » Wed Jan 13, 2021 9:37 am

Hi,
Does anybody know how to generate Random draws from multivariate normal distribution in eviews?
I found the function @rmvnorm(S[, n]), but i am not able to generate the shocks.
I cant understand which is the problem. I am using S as a variance covariance matrix.
Thanks!
Gabriel

scalar Dim = 2
matrix(1000,Dim) multishock
matrix(Dim ,Dim ) S

S(1,1)=1
S(2,2)=1
S(1,2)=0'-0.7
S(2,1)=0'-0.7

multishock =@rmvnormc(S,1000)

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

Re: Random draws from multivariate normal distribution

Postby EViews Gareth » Wed Jan 13, 2021 1:14 pm

S needs to be a sym, not a matrix.
Follow us on Twitter @IHSEViews

GabrielV
Posts: 2
Joined: Wed Jan 13, 2021 9:30 am

Re: Random draws from multivariate normal distribution

Postby GabrielV » Tue Jan 26, 2021 3:59 am

Hi,
thanks for the feedback. i tried with S being Sym and not matrix but it didn't work. Any other suggestions?

EViews Matt
EViews Developer
Posts: 557
Joined: Thu Apr 25, 2013 7:48 pm

Re: Random draws from multivariate normal distribution

Postby EViews Matt » Tue Jan 26, 2021 10:07 am

Hello,

Your posted code with the declaration of S changed to type sym runs successfully for me. What is the error or failure you're observing?


Return to “Program Repository”

Who is online

Users browsing this forum: No registered users and 6 guests