' MV_GARCH-M.PRG 
' example program for EViews LogL object
' revised for version 5.0 (8/09/2011)
'
' restricted version of 
' Multi-variate BEKK of Engle and Kroner (1995):
'
'  y = mu + lambda*g + sigma*y(-1) + res
'  res ~ N(0,H)
'
'  H = omega*omega' + beta*H(-1)*beta' + alpha*res(-1)*res(-1)'*alpha'
'
'  where,
'     y = 2x 1
'    mu = 2 x 1
'    lambda = 2 x 2
'    sigma = 2 x 2

'     H = 2 x 2 (symmetric)
'         H(1,1) = variance of y1   (saved as var_y1)
'         H(1,2) = cov of y1 and y2 (saved as cov_y1y2)
'         H(2,2) = variance of y2   (saved as var_y2)

'     g = 2 x 1
'          g(1) = variance of y1   (saved as var_y1)
'          g(2) = cov of y1 and y2 (saved as cov_y1y2)


' omega = 2 x 2 low triangular 
'  beta = 2 x 2 diagonal
' alpha = 2 x 2 diagonal

' dependent variables of all series must be continues
smpl @all
series y1 = log(mr) -log(rf(-1))
series y2 = log(cg)


' set sample 
' first observation of s1 needs to be two periods after
' the first observation of s0 
sample s0 @first  @last
sample s1 @first+2  @last

' initialization of parameters and starting values
' change below only to change the specification of model 
smpl s0

'get starting values from univariate models 
equation eq1.arch(archm=var,m=1000,c=1e-5) y1 c 
equation eq2.ls y2 c y1(-1) y2(-1)

'save the conditional variances
eq1.makegarch garch1 

' declare coef vectors to use in GARCH model
coef(2) mu
 mu(1) = eq1.c(2)
 mu(2) = eq2.c(1)

coef(3) omega
 omega(1) = @sqrt(@abs(eq1.c(3)))
 omega(2) = 0 
 omega(3) = 0

coef(2) alpha
 alpha(1) =  @sqrt(@abs(eq1.c(4)))
 alpha(2) =  0

coef(2) beta 
 beta(1) = @sqrt(@abs(eq1.c(5)))
 beta(2) = 0

coef(2) lambda  
         lambda(1) = eq1.c(1)
         lambda(2) = 0

coef(2) sigma 
         sigma(1) = eq2.c(2)
         sigma(2) = eq2.c(3)
  
' use sample var-cov as starting value of variance-covariance matrix

series cov_y1y2 = @cov(y1-mu(1)-lambda(1)*garch1, y2-mu(2)-sigma(1)*y1(-1)-sigma(2)*y2(-1))
series var_y1 = @var(y1-mu(1)-lambda(1)*garch1)
series var_y2 = @var( y2-mu(2)-sigma(1)*y1(-1)-sigma(2)*y2(-1))

series sqres1 = (y1-mu(1)-lambda(1)*garch1)^2
series sqres2 = (y2-mu(2)-sigma(1)*y1(-1)-sigma(2)*y2(-1))^2

series res1res2 = (y1-mu(1)-lambda(1)*garch1)*(y2-mu(2)-sigma(1)*y1(-1)-sigma(2)*y2(-1))

' constant adjustment for log likelihood
!mlog2pi = 2*log(2*@acos(-1))

' ...........................................................
' LOG LIKELIHOOD
' set up the likelihood 
' 1) open a new blank likelihood object name mvgarch
' 2) specify the log likelihood model by append
' ...........................................................

logl mvgarch Hao

' squared errors and cross errors
mvgarch.append @logl logl
mvgarch.append sqres1 = (y1-mu(1)-lambda(1)*var_y1-lambda(2)*cov_y1y2)^2
mvgarch.append sqres2 = (y2-mu(2)-sigma(1)*y1(-1)-sigma(2)*y2(-1))^2

mvgarch.append res1res2 = (y1-mu(1)-lambda(1)*var_y1-lambda(2)*cov_y1y2)*(y2-mu(2)-sigma(1)*y1(-1)-sigma(2)*y2(-1))

' variance and covariance series 
mvgarch.append var_y1  =  omega(1)^2 + beta(1)^2*var_y1(-1) + alpha(1)^2*sqres1(-1)
mvgarch.append var_y2  =  omega(3)^2+omega(2)^2 + beta(2)^2*var_y2(-1) + alpha(2)^2*sqres2(-1)

mvgarch.append cov_y1y2 = omega(1)*omega(2) + beta(2)*beta(1)*cov_y1y2(-1) + alpha(2)*alpha(1)*res1res2(-1)

' determinant of the variance-covariance matrix
mvgarch.append deth = var_y1*var_y2-cov_y1y2^2

' calculate the elements of the inverse of var_cov (H) matrix 
' numbered as vech(inv(H))
mvgarch.append invh1 = var_y2/deth
mvgarch.append invh2 = -cov_y1y2/deth
mvgarch.append invh3 = var_y1/deth

' log-likelihood series
mvgarch.append logl = -0.5*(!mlog2pi + (invh1*sqres1+invh3*sqres2+2*invh2*res1res2) + log(deth))

' remove some of the intermediary series
'mvgarch.append @temp invh1 invh2 invh3 sqres1 sqres2 res1res2 deth

' estimate the model
smpl s1
mvgarch.ml(showopts,b, tdist,deriv=f, m=1000, c=1e-5)

' change below to display different output
show mvgarch.output

'estimate univariate models using same sample as multirivariate model 
smpl s1
equation eq1.arch(archm=var,m=1000,c=1e-5) y1 c 
equation eq2.ls y2 c y1(-1) y2(-1)

' LR statistic for univariate vs multivariate
scalar lr = -2*( mvgarch.@logl - eq1.@logl - eq2.@logl )
scalar lr_pval = 1 - @cchisq(lr,2)


' MV_GARCH-M.PRG 
' example program for EViews LogL object
' revised for version 5.0 (8/09/2011)
'
' restricted version of 
' Multi-variate BEKK of Engle and Kroner (1995):
'
'  y = mu + lambda*g + sigma*y(-1) + res
'  res ~ N(0,H)
'
'  H = omega*omega' + beta*H(-1)*beta' + alpha*res(-1)*res(-1)'*alpha'
'
'  where,
'     y = 2x 1
'    mu = 2 x 1
'    lambda = 2 x 2
'    sigma = 2 x 2

'     H = 2 x 2 (symmetric)
'         H(1,1) = variance of y1   (saved as var_y1)
'         H(1,2) = cov of y1 and y2 (saved as cov_y1y2)
'         H(2,2) = variance of y2   (saved as var_y2)

'     g = 2 x 1
'          g(1) = variance of y1   (saved as var_y1)
'          g(2) = cov of y1 and y2 (saved as cov_y1y2)


' omega = 2 x 2 low triangular 
'  beta = 2 x 2 diagonal
' alpha = 2 x 2 diagonal

' dependent variables of all series must be continues
smpl @all
series y1 = log(mr) -log(rf(-1))
series y2 = log(cg)


' set sample 
' first observation of s1 needs to be two periods after
' the first observation of s0 
sample s0 @first  @last
sample s1 @first+2  @last

' initialization of parameters and starting values
' change below only to change the specification of model 
smpl s0

'get starting values from univariate models 
equation eq1.arch(archm=var,m=1000,c=1e-5) y1 c 
equation eq2.ls y2 c y1(-1) y2(-1)

'save the conditional variances
eq1.makegarch garch1 

' declare coef vectors to use in GARCH model
coef(2) mu
 mu(1) = eq1.c(2)
 mu(2) = eq2.c(1)

coef(3) omega
 omega(1) = @sqrt(@abs(eq1.c(3)))
 omega(2) = 0 
 omega(3) = 0

coef(2) alpha
 alpha(1) =  @sqrt(@abs(eq1.c(4)))
 alpha(2) =  0

coef(2) beta 
 beta(1) = @sqrt(@abs(eq1.c(5)))
 beta(2) = 0

coef(2) lambda  
         lambda(1) = eq1.c(1)
         lambda(2) = 0

coef(2) sigma 
         sigma(1) = eq2.c(2)
         sigma(2) = eq2.c(3)
  
' use sample var-cov as starting value of variance-covariance matrix

series cov_y1y2 = @cov(y1-mu(1)-lambda(1)*garch1, y2-mu(2)-sigma(1)*y1(-1)-sigma(2)*y2(-1))
series var_y1 = @var(y1-mu(1)-lambda(1)*garch1)
series var_y2 = @var( y2-mu(2)-sigma(1)*y1(-1)-sigma(2)*y2(-1))

series sqres1 = (y1-mu(1)-lambda(1)*garch1)^2
series sqres2 = (y2-mu(2)-sigma(1)*y1(-1)-sigma(2)*y2(-1))^2

series res1res2 = (y1-mu(1)-lambda(1)*garch1)*(y2-mu(2)-sigma(1)*y1(-1)-sigma(2)*y2(-1))

' constant adjustment for log likelihood
!mlog2pi = 2*log(2*@acos(-1))

' ...........................................................
' LOG LIKELIHOOD
' set up the likelihood 
' 1) open a new blank likelihood object name mvgarch
' 2) specify the log likelihood model by append
' ...........................................................

logl mvgarch Hao

' squared errors and cross errors
mvgarch.append @logl logl
mvgarch.append sqres1 = (y1-mu(1)-lambda(1)*var_y1-lambda(2)*cov_y1y2)^2
mvgarch.append sqres2 = (y2-mu(2)-sigma(1)*y1(-1)-sigma(2)*y2(-1))^2

mvgarch.append res1res2 = (y1-mu(1)-lambda(1)*var_y1-lambda(2)*cov_y1y2)*(y2-mu(2)-sigma(1)*y1(-1)-sigma(2)*y2(-1))

' variance and covariance series 
mvgarch.append var_y1  =  omega(1)^2 + beta(1)^2*var_y1(-1) + alpha(1)^2*sqres1(-1)
mvgarch.append var_y2  =  omega(3)^2+omega(2)^2 + beta(2)^2*var_y2(-1) + alpha(2)^2*sqres2(-1)

mvgarch.append cov_y1y2 = omega(1)*omega(2) + beta(2)*beta(1)*cov_y1y2(-1) + alpha(2)*alpha(1)*res1res2(-1)

' determinant of the variance-covariance matrix
mvgarch.append deth = var_y1*var_y2-cov_y1y2^2

' calculate the elements of the inverse of var_cov (H) matrix 
' numbered as vech(inv(H))
mvgarch.append invh1 = var_y2/deth
mvgarch.append invh2 = -cov_y1y2/deth
mvgarch.append invh3 = var_y1/deth

' log-likelihood series
mvgarch.append logl = -0.5*(!mlog2pi + (invh1*sqres1+invh3*sqres2+2*invh2*res1res2) + log(deth))

' remove some of the intermediary series
'mvgarch.append @temp invh1 invh2 invh3 sqres1 sqres2 res1res2 deth

' estimate the model
smpl s1
mvgarch.ml(showopts,b, tdist,deriv=f, m=1000, c=1e-5)

' change below to display different output
show mvgarch.output

'estimate univariate models using same sample as multirivariate model 
smpl s1
equation eq1.arch(archm=var,m=1000,c=1e-5) y1 c 
equation eq2.ls y2 c y1(-1) y2(-1)

' LR statistic for univariate vs multivariate
scalar lr = -2*( mvgarch.@logl - eq1.@logl - eq2.@logl )
scalar lr_pval = 1 - @cchisq(lr,2)

