Random draw from Wishart

For posting your own programs to share with others

Moderators: EViews Gareth, EViews Moderator

EViews Esther
EViews Developer
Posts: 149
Joined: Fri Sep 03, 2010 7:57 am

Random draw from Wishart

Postby EViews Esther » Wed Jul 02, 2014 10:47 am

Below is a subroutine to produce a random draw from the specified Wishart distribution.

Code: Select all

'command: wish(S, h, n) -- generates multivariate Wishart random variables
'Inputs: h --- m-by-m scale matrix
'         n --- scalar degree of freedom
'Outputs: S = m-by-m matrix draw from the Wishart distribution
'Note: used Bartlett decomposition
matrix(2,2) s
sym(2) h = @identity(2)
scalar n = 5
call wish(s,h,n)

subroutine local wish(matrix S, sym h, scalar n)
   !p = @rows(h)
   matrix T = @zeros(!p,!p)
   for !i=1 to !p
      for !j=1 to !i
         if !i = !j then
            !ntemp = @mrnd(1,1)
            !chirnd = @qchisq(!ntemp,n-!i+1)
            T(!i,!j) = @sqrt(!chirnd)
            !normrnd = @mnrnd(1,1)
            T(!i,!j) = !normrnd
   matrix A = T*@transpose(T)
   matrix Y = @cholesky(h)
   S = @transpose(Y)*A*Y

Return to “Program Repository”

Who is online

Users browsing this forum: No registered users and 4 guests