A problem for Multivariate Garch-mean > three variables

For technical questions regarding estimation of single equations, systems, VARs, Factor analysis and State Space Models in EViews. General econometric questions and advice should go in the Econometric Discussions forum.

Moderators: EViews Gareth, EViews Moderator

liumartin
Posts: 10
Joined: Mon Mar 15, 2010 6:01 pm

A problem for Multivariate Garch-mean > three variables

Postby liumartin » Sun Oct 17, 2010 9:50 am

Dear

I have seen the discussion in the following link for programming Multivariate GARCH-mean model.
http://forums.eviews.com/viewtopic.php?f=4&t=273#p971

However, the discussion is about bi/trivariate GARCH. In these cases, we could spread out the var-covariance matrix to compute the matrix determinant easily. When the variables are going to be large, saying 7 variables in my cases, I think it's better to use matrix operation to @det(X) for determinants and others like a matrix multiplying a group.

Thus, I have modified the TV_GARCH example. But the results I got are not correct as following. Also, I have my code below. appreciate anyone help.
Capture.JPG
error infor
Capture.JPG (137.44 KiB) Viewed 5293 times

Here is my code. I suspect whether the matrix operation is incorrect. I appreciate anyone's help.

' TV_GARCH.PRG (10/10/2000)
' example program for EViews LogL object
' revised for version 5.0 (11/19/2009)
'
' restricted version of
' tri-variate BEKK of Engle and Kroner (1995):
'
' y = mu + res
' res ~ N(0,H)
'
' H = omega*omega' + beta H(-1) beta' + alpha res(-1) res(-1)' alpha'
'
' where,
' y = 3 x 1
' mu = 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 y2 (saved as cov_y1y3)
' H(2,2) = variance of y2 (saved as var_y2)
' H(2,3) = cov of y1 and y3 (saved as cov_y2y3)
' H(3,3) = variance of y3 (saved as var_y3)
' omega = 3 x 3 low triangular
' beta = 3 x 3 diagonal
' alpha = 3 x 3 diagonal
'

'change path to program path
wfopen "G:\Liu Xiaochun\Research\2010 papers\Pre topics for papers\International Finance\Expenditure switching effect estimatin paper\Expenditure switching paper writing\ESE eviews code\ese eviews data.wf1"

delete s0
delete s1
delete deth
delete var
delete cov

' dependent variables of all series must be continues
series y1 = logusuk
series y2 = log(us_exppr)
series y3 =log(us_imppr)
series y4 =log(us_ppi)
series y5 =log(us_cpi)
series y6=log(us_mpuk)
series y7 = log(us_hcuk)

' set sample
' first observation of s1 need to be one or two periods after
' the first observation of s0
sample s0 1980M02 2010M04
sample s1 1980M04 2010M04

' initialization of parameters and starting values
' change below only to change the specification of model
smpl s0

'get starting values from univariate GARCH
equation eq1.arch(m=100,c=1e-5) y1 c
equation eq2.arch(m=100,c=1e-5) y2 c
equation eq3.arch(m=100,c=1e-5) y3 c
equation eq4.arch(m=100,c=1e-5) y4 c
equation eq5.arch(m=100,c=1e-5) y5 c
equation eq6.arch(m=100,c=1e-5) y6 c
equation eq7.arch(m=100,c=1e-5) y7 c



' declare coef vectors to use in GARCH model
coef(8) mu
mu(1) = eq1.c(1)
mu(2) = eq2.c(1)
mu(3) = eq3.c(1)
mu(4) = eq4.c(1)
mu(5) =eq5.c(1)
mu(6) =eq6.c(1)
mu(7) =eq7.c(1)
mu(8)=0.01

coef(14) omega
omega(1) = (eq1.c(2))^.5
omega(2) = 0
omega(3) = 0
omega(4) = eq2.c(2)^.5
omega(5) = 0
omega(6) = eq3.c(3)^.5
omega(7) =0
omega(8) =eq4.c(3)^.5
omega(9)=0
omega(10)=eq5.c(3)^.5
omega(11)=0
omega(12)=eq6.c(3)^.5
omega(13)=0
omega(14)=eq7.c(3)^.5

coef(7) alpha
alpha(1) = (eq1.c(3))^.5
alpha(2) = (eq2.c(3))^.5
alpha(3) = (eq3.c(3))^.5
alpha(4) = (eq4.c(3))^.5
alpha(5) = (eq5.c(3))^.5
alpha(6) = (eq6.c(3))^.5
alpha(7) = (eq7.c(3))^.5

coef(7) beta
beta(1) = (@abs(eq1.c(4)))^.5
beta(2) = (@abs(eq2.c(4)))^.5
beta(3) = (@abs(eq3.c(4)))^.5
beta(4) = (@abs(eq4.c(4)))^.5
beta(5) = (@abs(eq5.c(4)))^.5
beta(6) = (@abs(eq6.c(4)))^.5
beta(7) = (@abs(eq7.c(4)))^.5

' use sample var-cov as starting value of variance-covariance matrix
series res1 = y1-mu(1)+mu(8)*var_y1
series res2 = y2-mu(2)
series res3 = y3-mu(3)
series res4 = y4-mu(4)
series res5 = y5-mu(5)
series res6 = y6-mu(6)
series res7 = y7-mu(7)

series cov_y1y2 = @cov(res1, res2)
series cov_y1y3 = @cov(y1-mu(1), y3-mu(3))
series cov_y1y4 = @cov(y1-mu(1), y4-mu(4))
series cov_y1y5 = @cov(y1-mu(1), y5-mu(5))
series cov_y1y6 = @cov(y1-mu(1), y6-mu(6))
series cov_y1y7 = @cov(y1-mu(1), y7-mu(7))
series cov_y2y3 = @cov(y2-mu(2), y3-mu(3))
series cov_y2y4 = @cov(y2-mu(2), y4-mu(4))
series cov_y2y5 = @cov(y2-mu(2), y5-mu(5))
series cov_y2y6 = @cov(y2-mu(2), y6-mu(6))
series cov_y2y7 = @cov(y2-mu(2), y7-mu(7))
series cov_y3y4 = @cov(y3-mu(3), y4-mu(4))
series cov_y3y5 = @cov(y3-mu(3), y5-mu(5))
series cov_y3y6 = @cov(y3-mu(3), y6-mu(6))
series cov_y3y7 = @cov(y3-mu(3), y7-mu(7))
series cov_y4y5 = @cov(y4-mu(4), y5-mu(5))
series cov_y4y6 = @cov(y4-mu(4), y6-mu(6))
series cov_y4y7 = @cov(y4-mu(4), y7-mu(7))
series cov_y5y6 = @cov(y5-mu(5), y6-mu(6))
series cov_y5y7 = @cov(y5-mu(5), y7-mu(7))
series cov_y6y7 = @cov(y6-mu(6), y7-mu(7))

series var_y1 = @var(y1)
series var_y2 = @var(y2)
series var_y3 = @var(y3)
series var_y4 = @var(y4)
series var_y5 = @var(y5)
series var_y6 = @var(y6)
series var_y7 = @var(y7)



series sqres1 = res1^2
series sqres2 = res2^2
series sqres3 = res3^2
series sqres4 = res4^2
series sqres5 = res5^2
series sqres6 = res6^2
series sqres7 = res7^2


series res1res2 = res1*res2
series res1res3 = res1*res3
series res1res4 = res1*res4
series res1res5 = res1*res5
series res1res6 = res1*res6
series res1res7 = res1*res7
series res2res3 = res2*res3
series res2res4 = res2*res4
series res2res5 = res2*res5
series res2res6 = res2*res6
series res2res7 = res2*res7
series res3res4 = res3*res4
series res3res5 = res3*res5
series res3res6 = res3*res6
series res3res7 = res3*res7
series res4res5 = res4*res5
series res4res6 = res4*res6
series res4res7 = res4*res7
series res5res6 = res5*res6
series res5res7 = res5*res7
series res6res7 = res6*res7

' constant adjustment for log likelihood
!mlog2pi = 7*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 res1 = y1-mu(1)+mu(8)*var_y1
tvgarch.append res2 = y2-mu(2)
tvgarch.append res3 = y3-mu(3)
tvgarch.append res4 = y4-mu(4)
tvgarch.append res5 = y5-mu(5)
tvgarch.append res6 = y6-mu(6)
tvgarch.append res7 = y7-mu(7)


tvgarch.append sqres1 = res1^2
tvgarch.append sqres2 = res2^2
tvgarch.append sqres3 = res3^2
tvgarch.append sqres4 = res4^2
tvgarch.append sqres5 = res5^2
tvgarch.append sqres6 = res6^2
tvgarch.append sqres7 = res7^2


tvgarch.append res1res2 = res1*res2
tvgarch.append res1res3 = res1*res3
tvgarch.append res1res4 = res1*res4
tvgarch.append res1res5 = res1*res5
tvgarch.append res1res6 = res1*res6
tvgarch.append res1res7 = res1*res7
tvgarch.append res2res3 = res2*res3
tvgarch.append res2res4 = res2*res4
tvgarch.append res2res5 = res2*res5
tvgarch.append res2res6 = res2*res6
tvgarch.append res2res7 = res2*res7
tvgarch.append res3res4 = res3*res4
tvgarch.append res3res5 = res3*res5
tvgarch.append res3res6 = res3*res6
tvgarch.append res3res7 = res3*res7
tvgarch.append res4res5 = res4*res5
tvgarch.append res4res6 = res4*res6
tvgarch.append res4res7 = res4*res7
tvgarch.append res5res6 = res5*res6
tvgarch.append res5res7 = res5*res7
tvgarch.append res6res7 = res6*res7

' variance and covariance series
tvgarch.append var_y1 = omega(1)^2 + beta(1)^2*var_y1(-1) + alpha(1)^2*sqres1(-1)
tvgarch.append var_y2 = omega(2)^2 +omega(4)^2 + beta(2)^2*var_y2(-1) + alpha(2)^2*sqres2(-1)
tvgarch.append var_y3 = omega(3)^2 +omega(5)^2 +omega(6)^2 + beta(3)^2*var_y3(-1) + alpha(3)^2*sqres3(-1)
tvgarch.append var_y4 = omega(5)^2 +omega(7)^2 +omega(8)^2 +beta(4)^2*var_y4(-1) + alpha(4)^2*sqres4(-1)
tvgarch.append var_y5 = omega(7)^2 +omega(9)^2 +omega(10)^2 +beta(5)^2*var_y5(-1) + alpha(5)^2*sqres5(-1)
tvgarch.append var_y6 = omega(9)^2 +omega(11)^2 +omega(12)^2 +beta(6)^2*var_y6(-1) + alpha(6)^2*sqres6(-1)
tvgarch.append var_y7 = omega(11)^2+omega(13)^2 +omega(14)^2 +beta(7)^2*var_y7(-1) + alpha(7)^2*sqres7(-1)

tvgarch.append cov_y1y2 = omega(1)*omega(2) +beta(1)* beta(2)*cov_y1y2(-1) + alpha(1)*alpha(2)*res1res2(-1)
tvgarch.append cov_y1y3 = omega(1)*omega(3) + beta(1)*beta(3)*cov_y1y3(-1) + alpha(1)*alpha(3)*res1res3(-1)
tvgarch.append cov_y1y4 = omega(1)*omega(5) + beta(1)*beta(4)*cov_y1y4(-1) + alpha(1)*alpha(4)*res1res4(-1)
tvgarch.append cov_y1y5 = omega(1)*omega(7) + beta(1)*beta(5)*cov_y1y5(-1) + alpha(1)*alpha(5)*res1res5(-1)
tvgarch.append cov_y1y6 = omega(1)*omega(9) + beta(1)*beta(6)*cov_y1y6(-1) + alpha(1)*alpha(6)*res1res6(-1)
tvgarch.append cov_y1y7 = omega(1)*omega(11) + beta(1)*beta(7)*cov_y1y7(-1) + alpha(1)*alpha(7)*res1res7(-1)

tvgarch.append cov_y2y3 = omega(2)*omega(3) + omega(4)*omega(5) +beta(2)* beta(3)*cov_y2y3(-1) + alpha(2)*alpha(3)*res2res3(-1)
tvgarch.append cov_y2y4 = omega(2)*omega(5) + omega(6)*omega(7) +beta(2)* beta(4)*cov_y2y4(-1) + alpha(2)*alpha(4)*res2res4(-1)
tvgarch.append cov_y2y5 = omega(2)*omega(7) + omega(8)*omega(9) +beta(2)* beta(5)*cov_y2y5(-1) + alpha(2)*alpha(5)*res2res5(-1)
tvgarch.append cov_y2y6 = omega(2)*omega(9) + omega(10)*omega(11) +beta(2)* beta(6)*cov_y2y6(-1) + alpha(2)*alpha(6)*res2res6(-1)
tvgarch.append cov_y2y7 = omega(2)*omega(11) + omega(12)*omega(13) +beta(2)* beta(7)*cov_y2y7(-1) + alpha(2)*alpha(7)*res2res7(-1)

tvgarch.append cov_y3y4 = omega(3)*omega(5) + omega(6)*omega(7) +beta(3)* beta(4)*cov_y3y4(-1) + alpha(3)*alpha(4)*res3res4(-1)
tvgarch.append cov_y3y5 = omega(3)*omega(7) + omega(8)*omega(9) +beta(3)* beta(5)*cov_y3y5(-1) + alpha(3)*alpha(5)*res3res5(-1)
tvgarch.append cov_y3y6 = omega(3)*omega(9) + omega(10)*omega(11) +beta(3)* beta(6)*cov_y3y6(-1) + alpha(3)*alpha(6)*res3res6(-1)
tvgarch.append cov_y3y7 = omega(3)*omega(11) + omega(12)*omega(13) +beta(3)* beta(7)*cov_y3y7(-1) + alpha(3)*alpha(7)*res3res7(-1)

tvgarch.append cov_y4y5 = omega(4)*omega(7) + omega(8)*omega(9) +beta(4)* beta(5)*cov_y4y5(-1) + alpha(4)*alpha(5)*res4res5(-1)
tvgarch.append cov_y4y6 = omega(4)*omega(9) + omega(10)*omega(11) +beta(4)* beta(6)*cov_y4y6(-1) + alpha(4)*alpha(6)*res4res6(-1)
tvgarch.append cov_y4y7 = omega(4)*omega(11) + omega(12)*omega(13) +beta(4)* beta(7)*cov_y4y7(-1) + alpha(4)*alpha(7)*res4res7(-1)

tvgarch.append cov_y5y6 = omega(5)*omega(9) + omega(10)*omega(11) +beta(5)* beta(6)*cov_y5y6(-1) + alpha(5)*alpha(6)*res5res6(-1)
tvgarch.append cov_y5y7 = omega(5)*omega(11) + omega(12)*omega(13) +beta(5)* beta(7)*cov_y5y7(-1) + alpha(5)*alpha(7)*res5res7(-1)

tvgarch.append cov_y6y7 = omega(6)*omega(11) + omega(12)*omega(13) +beta(6)* beta(7)*cov_y6y7(-1) + alpha(6)*alpha(7)*res6res7(-1)



' determinant of the variance-covariance matrix


group group_var1 var_y1 cov_y1y2 cov_y1y3 cov_y1y4 cov_y1y5 cov_y1y6 cov_y1y7
group group_var2 cov_y1y2 var_y2 cov_y2y3 cov_y2y4 cov_y2y5 cov_y2y6 cov_y2y7
group group_var3 cov_y1y3 cov_y2y3 var_y3 cov_y3y4 cov_y3y5 cov_y3y6 cov_y3y7
group group_var4 cov_y1y4 cov_y2y4 cov_y3y4 var_y4 cov_y4y5 cov_y4y6 cov_y4y7
group group_var5 cov_y1y5 cov_y2y5 cov_y3y5 cov_y4y5 var_y5 cov_y5y6 cov_y5y7
group group_var6 cov_y1y6 cov_y2y6 cov_y3y6 cov_y4y6 cov_y5y6 var_y6 cov_y6y7
group group_var7 cov_y1y7 cov_y2y7 cov_y3y7 cov_y4y7 cov_y5y7 cov_y6y7 var_y7
group group_res res1 res2 res3 res4 res5 res6 res7

matrix mat_var1=group_var1
matrix mat_var2=group_var2
matrix mat_var3=group_var3
matrix mat_var4=group_var4
matrix mat_var5=group_var5
matrix mat_var6=group_var6
matrix mat_var7=group_var7
matrix mat_res=group_res

!m1=@rows(mat_var1)
!m2=@columns(mat_var1)
matrix(!m2,!m2) mat_var
rowvector(!m2) vect_var1
rowvector(!m2) vect_var2
rowvector(!m2) vect_var3
rowvector(!m2) vect_var4
rowvector(!m2) vect_var5
rowvector(!m2) vect_var6
rowvector(!m2) vect_var7
rowvector(!m2) vect_res
vector(!m1) vect_h
vector(!m1) deth

for !i=1 to !m1
matrix mat_varr=mat_var
vect_var1=@rowextract(mat_var1,!i)
vect_var2=@rowextract(mat_var2,!i)
vect_var3=@rowextract(mat_var3,!i)
vect_var4=@rowextract(mat_var4,!i)
vect_var5=@rowextract(mat_var5,!i)
vect_var6=@rowextract(mat_var6,!i)
vect_var7=@rowextract(mat_var7,!i)
vect_res =@rowextract(mat_res,!i)
rowplace(mat_var,vect_var1,1)
rowplace(mat_var,vect_var2,2)
rowplace(mat_var,vect_var3,3)
rowplace(mat_var,vect_var4,4)
rowplace(mat_var,vect_var5,5)
rowplace(mat_var,vect_var6,6)
rowplace(mat_var,vect_var7,7)
vector(1) my=vect_res*@inverse(mat_var)*@transpose(vect_res)
vect_h(!i)=my(1)
deth(!i)=@det(mat_var)
next



mtos(vect_h, s_h)
mtos(deth, s_deth)




' log-likelihood series
'tvgarch.append logl = -0.5*(!mlog2pi + s_h+ log(mu(8)) )

tvgarch.append logl = log(@dnorm(@sqrt(s_h)))-log(@abs(s_deth))/2

' 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=500, c=1e-5)

' change below to display different output
show tvgarch.output
graph var.line var_y1 var_y2 var_y3 var_y4 var_y5 var_y6 var_y7
graph cov.line cov_y1y2 cov_y1y3 cov_y1y4 cov_y1y5 cov_y1y6 cov_y1y7 cov_y2y3 cov_y2y4 cov_y2y5 cov_y2y6 cov_y2y7 cov_y3y4 cov_y3y5 cov_y3y6 cov_y3y7 cov_y4y5 cov_y4y6 cov_y4y7 cov_y5y6 cov_y5y7 cov_y6y7
show var
show cov

liumartin
Posts: 10
Joined: Mon Mar 15, 2010 6:01 pm

Re: A problem for Multivariate Garch-mean > three variables

Postby liumartin » Sun Oct 17, 2010 9:53 am

sorry, I forgot to say I only add one variance (var_y1) into this 7 variable GARCH-mean model as a test purpose.

trubador
Did you use forum search?
Posts: 1520
Joined: Thu Nov 20, 2008 12:04 pm

Re: A problem for Multivariate Garch-mean > three variables

Postby trubador » Mon Oct 18, 2010 1:55 am

There are several problems with your code, but the most crucial one concerns the likelihood function. Since the matrix operations are not allowed in LogL object, you need to write out the expression for the determinants, not the result itself. @det() function only gives you a fixed number, the expression should be written in the functional form. You should make certain that all coefficients really do affect the value of one or more of the likelihood contributions. Otherwise, the parameter will have no effect upon the likelihood and lead to a singularity error in your estimation.

Of course, you can often workaround this issue with defining several loops in order to carry out the symbolic matrix operations...

liumartin
Posts: 10
Joined: Mon Mar 15, 2010 6:01 pm

Re: A problem for Multivariate Garch-mean > three variables

Postby liumartin » Mon Oct 18, 2010 9:37 am

Thanks trubador. Now I know Matrix cannot be used in logl.

However, for higher dimensional GARCH, to write out expression for determinants is really tedious. Is there any way to work this out? something like writing a function or subroutine, then call the subroutine from logl.............

liumartin
Posts: 10
Joined: Mon Mar 15, 2010 6:01 pm

Re: A problem for Multivariate Garch-mean > three variables

Postby liumartin » Mon Oct 18, 2010 12:37 pm

Is there a way to not use matrix in my code for logl? I cannot figure out how to do my code if I do not use loops or matrix? Thanks

trubador
Did you use forum search?
Posts: 1520
Joined: Thu Nov 20, 2008 12:04 pm

Re: A problem for Multivariate Garch-mean > three variables

Postby trubador » Mon Oct 18, 2010 1:14 pm

You cannot call the subroutine from within the LogL object, but you can write a small code prior to LogL estimation that generates the matrix expressions and put them into a one long string. It is difficult, but possible especially with the new string features of EViews. Most of the equation specifications in your model can be looped in a similar manner to ease the syntax.

liumartin
Posts: 10
Joined: Mon Mar 15, 2010 6:01 pm

Re: A problem for Multivariate Garch-mean > three variables

Postby liumartin » Mon Oct 18, 2010 1:26 pm

There are several problems with your code, but the most crucial one concerns the likelihood function. Since the matrix operations are not allowed in LogL object, you need to write out the expression for the determinants, not the result itself. @det() function only gives you a fixed number, the expression should be written in the functional form. You should make certain that all coefficients really do affect the value of one or more of the likelihood contributions. Otherwise, the parameter will have no effect upon the likelihood and lead to a singularity error in your estimation.

Of course, you can often workaround this issue with defining several loops in order to carry out the symbolic matrix operations...
Eviews 6.0 can do Multivariate GARCH model in a system object. Does Eviews programming team also writing out expressions for determinants? or Do they have tricks on this?


Return to “Estimation”

Who is online

Users browsing this forum: No registered users and 2 guests