Page 1 of 1

Random draws from multivariate normal distribution

Posted: Mon Aug 02, 2010 4:11 pm
by j.galimberti
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

Re: Random draws from multivariate normal distribution

Posted: Fri May 20, 2011 7:08 am
by miksterz1
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!

Re: Random draws from multivariate normal distribution

Posted: Fri May 20, 2011 8:25 am
by EViews Gareth
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).

Re: Random draws from multivariate normal distribution

Posted: Sun May 22, 2011 1:10 am
by miksterz1
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?

Re: Random draws from multivariate normal distribution

Posted: Sun May 22, 2011 2:43 am
by j.galimberti
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.

Re: Random draws from multivariate normal distribution

Posted: Mon May 23, 2011 2:54 pm
by miksterz1
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.

Re: Random draws from multivariate normal distribution

Posted: Wed Jan 13, 2021 9:37 am
by GabrielV
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)

Re: Random draws from multivariate normal distribution

Posted: Wed Jan 13, 2021 1:14 pm
by EViews Gareth
S needs to be a sym, not a matrix.

Re: Random draws from multivariate normal distribution

Posted: Tue Jan 26, 2021 3:59 am
by GabrielV
Hi,
thanks for the feedback. i tried with S being Sym and not matrix but it didn't work. Any other suggestions?

Re: Random draws from multivariate normal distribution

Posted: Tue Jan 26, 2021 10:07 am
by EViews Matt
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?