' TRIVARIATE GARCH PROGRAM (2011/11/27)
' example program for EViews 7.0 LogL object
'' restricted (diagonal) version of 
' tri-variate BEKK of Engle and Kroner (1995):
'
' Y(t) = mu + rho*Y(t-1)+ + pai*Y(t-2)+ theta*FSI + res
'  res ~ N(0,H)
'
'  H = omega*omega' +alpha*res(-1)*res(-1)'*alpha' + beta*H(-1)*beta'  + eta*FSI
'
'  where,
'    y = 3 x 1
'    mu = 3 x 1
'    rho = 3 x 1
'    pai = 3 x 1
'    theta = 3 x 1
'     H = 3 x 3 (symmetric)
'         H(1,1) = variance of y1   (saved as var_y1)
'         H(1,2) = cov of y1 and y2 (saved as cov_y1y2)
'         H(1,3) = cov of y1 and y3 (saved as cov_y1y3)
'         H(2,2) = variance of y2   (saved as var_y2)
'         H(2,3) = cov of y2 and y3 (saved as cov_y2y3)
'         H(3,3) = variance of y3   (saved as var_y3)
'   omega = 3 x 3 (low triangular)
'   alpha = 3 x 3 (diagonal)
'   beta = 3 x 3 (diagonal)
'   eta  = 3 x 3 (symmetric)
'   
'change path to program path
%path = @runpath
cd %path 

' load workfile
load godhelpme.wf1

' dependent variables of all series must be continuous
series y1 = d_exch
series y1(-1) = d_exch(-1)
series y1(-2) = d_exch(-2)
series y2 = d_bond
series y2(-1) = d_bond(-1)
series y2(-2) = d_bond(-2)
series y3 = d_stock
series y3(-1) = d_stock(-1)
series y3(-2) = d_stock(-2)
series fsi = fsihp_resid01
series fsi(-1) = fsihp_resid01(-1)
series fsi(-2) = fsihp_resid01(-2)

' set sample 
' first observation of s1 need to be one or two periods after
' the first observation of s0 
sample s0 1998m04  2011m08
sample s1 1998m06  2011m08

' initialization of parameters and starting values
' change below only to change the specification of model 
smpl s0

'get starting values from univariate GARCH-in-mean
equation eq1.arch(1, 1, m=1000,h) y1 c y1(-1) y1(-2) fsi @ fsi
equation eq2.arch(1, 1, m=1000, h) y2 c y2(-1) y2(-2) fsi @ fsi
equation eq3.arch(1, 1, m=1000, h) y3 c y3(-1) y3(-2) fsi @ fsi

'save the conditional variances
'eq1.makegarch garch1 
'eq2.makegarch garch2
'eq3.makegarch garch3

' declare coef vectors to use in TVGARCH model
coef(3) mu
mu(1) = 0.5
mu(2) = 0.5
mu(3) =0.5

coef(3) rho
rho(1) =0.5
rho(2) = 0.5
rho(3) = 0.5

coef(3) pai
pai(1) = 0.5
pai(2) = 0.5
pai(3) = 0.5

coef(3) theta
theta(1) =0.5
theta(2) =0.5
theta(3) = 0.5


coef(6) omega
omega(1) = 0.5
omega(2) = 0.5
omega(3) = 0.5
omega(4) =0.5
omega(5) = 0.5
omega(6) =0.5

coef(3) alpha
alpha(1) =0.5
alpha(2) = 0.5
alpha(3) =0.5

coef(3) beta 
beta(1) = 0.5
beta(2) = 0.5
beta(3) = 0.5

coef(6) eta
eta(1) = 0.5
eta(2) =0.5
eta(3) = 0.5
eta(4) =0.5
eta(5) = 0.5
eta(6) =0.5
' use sample var-cov as starting value of variance-covariance matrix
series cov_y1y2 = @cov(y1-mu(1)-rho(1)*y1(-1)-pai(1)*y1(-2)-theta(1)*fsi, y2-mu(2)-rho(2)*y2(-1)-pai(2)*y2(-2)-theta(2)*fsi)

series cov_y1y3 = @cov(y1-mu(1)-rho(1)*y1(-1)-pai(1)*y1(-2)-theta(1)*fsi, y3-mu(3)-rho(3)*y3(-1)-pai(3)*y3(-2)-theta(3)*fsi)

series cov_y2y3 = @cov(y2-mu(2)-rho(2)*y2(-1)-pai(2)*y2(-2)-theta(2)*fsi, y3-mu(3)-rho(3)*y3(-1)-pai(3)*y3(-2)-theta(3)*fsi)

series var_y1 = @var(y1-rho(1)*y1(-1)-pai(1)*y1(-2)-theta(1)*fsi)
series var_y2 = @var(y2-rho(2)*y2(-1)-pai(2)*y2(-2)-theta(2)*fsi)
series var_y3 = @var(y3-rho(3)*y3(-1)-pai(3)*y3(-2)-theta(3)*fsi)

series res1res2 = (y1-mu(1)-rho(1)*y1(-1)-pai(1)*y1(-2)-theta(1)*fsi)*(y2-mu(2)-rho(2)*y2(-1)-pai(2)*y2(-2)-theta(2)*fsi)

series res1res3 = (y1-mu(1)-rho(1)*y1(-1)-pai(1)*y1(-2)-theta(1)*fsi)*(y3-mu(3)-rho(3)*y3(-1)-pai(3)*y3(-2)-theta(3)*fsi)

series res2res3 = (y2-mu(2)-rho(2)*y2(-1)-pai(2)*y2(-2)-theta(2)*fsi)*(y3-mu(3)-rho(3)*y3(-1)-pai(3)*y3(-2)-theta(3)*fsi)

' constant adjustment for log likelihood
!mlog2pi = 3*log(2*@acos(-1))


' ...........................................................
' LOG LIKELIHOOD
' set up the likelihood 
' 1) open a new blank likelihood object name tvgarch
' 2) specify the log likelihood model by append
' ...........................................................

logl tvgarch

' squared errors and cross errors
tvgarch.append @logl logl
tvgarch.append sqres1 = (y1-mu(1)-rho(1)*y1(-1)-pai(1)*y1(-2)-theta(1)*fsi)^2
tvgarch.append sqres2 = (y2-mu(2)-rho(2)*y2(-1)-pai(2)*y2(-2)-theta(2)*fsi)^2
tvgarch.append sqres3 = (y3-mu(3)-rho(3)*y3(-1)-pai(3)*y3(-2)-theta(3)*fsi)^2

tvgarch.append res1res2 = (y1-mu(1)-rho(1)*y1(-1)-pai(1)*y1(-2)-theta(1)*fsi)*(y2-mu(2)-rho(2)*y2(-1)-pai(2)*y2(-2)-theta(2)*fsi)

tvgarch.append res1res3 = (y1-mu(1)-rho(1)*y1(-1)-pai(1)*y1(-2)-theta(1)*fsi)*(y3-mu(3)-rho(3)*y3(-1)-pai(3)*y3(-2)-theta(3)*fsi)

tvgarch.append res2res3 = (y2-mu(2)-rho(2)*y2(-1)-pai(2)*y2(-2)-theta(2)*fsi)*(y3-mu(3)-rho(3)*y3(-1)-pai(3)*y3(-2)-theta(3)*fsi)

' variance and covariance series 
tvgarch.append var_y1  =  omega(1)^2 + beta(1)^2*var_y1(-1) + alpha(1)^2*sqres1(-1) + eta(1)*fsi
tvgarch.append var_y2  = omega(2)^2+omega(4)^2 + beta(2)^2*var_y2(-1) + alpha(2)^2*sqres2(-1) + eta(4)*fsi
tvgarch.append var_y3  = omega(3)^2+omega(5)^2+omega(6)^2 + beta(3)^2*var_y3(-1) + alpha(3)^2*sqres3(-1) + eta(6)*fsi

tvgarch.append cov_y1y2 = omega(1)*omega(2) + beta(2)*beta(1)*cov_y1y2(-1) + alpha(2)*alpha(1)*res1res2(-1) + eta(2)*fsi
tvgarch.append cov_y1y3 = omega(1)*omega(3) + beta(3)*beta(1)*cov_y1y3(-1) + alpha(3)*alpha(1)*res1res3(-1) + eta(3)*fsi
tvgarch.append cov_y2y3 = omega(2)*omega(3) + omega(4)*omega(5) + beta(3)*beta(2)*cov_y2y3(-1) + alpha(3)*alpha(2)*res2res3(-1) + eta(5)*fsi

' determinant of the variance-covariance matrix
tvgarch.append deth = var_y1*var_y2*var_y3 - var_y1*cov_y2y3^2-cov_y1y2^2*var_y3+2*cov_y1y2*cov_y2y3*cov_y1y3-cov_y1y3^2*var_y2

' calculate the elements of the inverse of var_cov (H) matrix 
' numbered as vech(inv(H))
tvgarch.append invh1 = (var_y2*var_y3-cov_y2y3^2)/deth
tvgarch.append invh2 = -(cov_y1y2*var_y3-cov_y1y3*cov_y2y3)/deth
tvgarch.append invh3 = (cov_y1y2*cov_y2y3-cov_y1y3*var_y2)/deth
tvgarch.append invh4 = (var_y1*var_y3-cov_y1y3^2)/deth
tvgarch.append invh5 = -(var_y1*cov_y2y3-cov_y1y2*cov_y1y3)/deth
tvgarch.append invh6 = (var_y1*var_y2-cov_y1y2^2)/deth

' log-likelihood series
tvgarch.append logl = -0.5*(!mlog2pi + (invh1*sqres1+invh4*sqres2+invh6*sqres3 +2*invh2*res1res2 +2*invh3*res1res3+2*invh5*res2res3 ) + log(deth))

' remove some of the intermediary series
tvgarch.append @temp invh1 invh2 invh3 invh4 invh5 invh6 sqres1 sqres2 sqres3 res1res2 res1res3 res2res3 deth

' estimate the model
smpl s1
tvgarch.ml(showopts, m=1000, h, b)

' change below to display different output
show tvgarch.output
graph var.line var_y1 var_y2 var_y3
graph cov.line cov_y1y2 cov_y1y3 cov_y2y3
show var
show cov
