Trivariate VAR-GARCH model

Discussions of ARCH, GARCH, and related models
jessie
Posts: 6
Joined: Fri Aug 14, 2009 5:28 am

Trivariate VAR-GARCH model

Unread post by jessie »

Hi

I am using a trivariate VAR-MGARCH model to analyse volatility spillovers in my dissertation. I obtained the RATS codes from the author of a journal article that used a VECM-GARCH. However, I have never used RATS before and do not know how to alter the code from a bivariate VECM-GARCH model to a trivariate VAR-GARCH. Can anyone help me with the code please?

Cheers,

Jessie

Code: Select all

* Euro_UK 
*
calendar(daily) 1983 1 3
allocate 100 2002:12:31
* UK interbank (proxy A)  UK cd (proxy B) eurostirling
open data a:\uk.txt
data(format=free,org=obs) / h1 h2 h3 

* period 1, september 92 - erm crisis

smpl 1983:1:3 1992:7:31
SET DSCP =LOG(h1)-LOG(h1{1})
SET DSFP =LOG(h3)-LOG(h3{1})
SET ECT  =LOG(h3)-LOG(h1)

*  Starting and ending points for GARCH estimation. GSTART must
*  be at least entry 2.
*
COMPUTE gstart=1983:1:10, gend=1992:7:31


*
*  Parameters for the regression function
*

NONLIN B10 B11 B12 B13 C11 C12 C13 E11 B20 B21 B22 B23 C21 C22 C23 E22
FRML RESID1 = (DSCP-B10-B11*DSCP{1}-B12*DSCP{2}-B13*DSCP{3}-C11*DSFP{1}-C12*DSFP{2}-C13*DSFP{3}-E11*ECT{1})
FRML RESID2 = (DSFP-B20-B21*DSFP{1}-B22*DSFP{2}-B23*DSFP{3}-C21*DSCP{1}-C22*DSCP{2}-C23*DSCP{3}-E22*ECT{1})

*
*  Do initial regression. Copy initial values for regression parameters
*
LINREG  DSCP / R1
# CONSTANT DSCP{1} DSCP{2} DSCP{3} DSFP{1} DSFP{2} DSFP{3} ECT{1}
COMPUTE B10 = %BETA(1)
COMPUTE B11 = %BETA(2)
COMPUTE B12 = %BETA(3)
COMPUTE B13 = %BETA(4)
COMPUTE C11 = %BETA(5)
COMPUTE C12 = %BETA(6)
COMPUTE C13 = %BETA(7)
COMPUTE E11 = %BETA(8)
LINREG  DSFP / R2
# CONSTANT DSCP{1} DSCP{2} DSCP{3} DSFP{1} DSFP{2} DSFP{3} ECT{1}
COMPUTE B20 = %BETA(1)
COMPUTE B21 = %BETA(2)
COMPUTE B22 = %BETA(3)
COMPUTE B23 = %BETA(4)
COMPUTE C21 = %BETA(5)
COMPUTE C22 = %BETA(6)
COMPUTE C23 = %BETA(7)
COMPUTE E22 = %BETA(8)
*
*  Get the covariance matrix of the residuals.
*
VCV(MATRIX=RR,NOPRINT)
# R1 R2
DECLARE SERIES U1 U2
DECLARE SERIES H11 H12 H22
*
*  Initialize residuals
*
SET U1 = R1
SET U2 = R2

*
*  Seed the covariance matrix with values from it. This is important
*  for GARCH models: if the initial values of the H series are
*  unreasonable, the entire estimation is thrown off.
*
SET H11 = RR(1,1)
SET H22 = RR(2,2)
SET H12 = RR(1,2)

*
*  This is a standard log likelihood formula for any bivariate
*  ARCH, GARCH, ARCH-M,... The difference among these will be in
*  the definitions of H11F,H22F,H12F,RESID1 and RESID2
*
DECLARE SYMMETRIC H
DECLARE VECTOR    U
DECLARE FRML      H11F H12F H22F
*
FRML LOGL = $
	H11(T)=H11F(T),H12(T)=H12F(T),$
        H22(T)=H22F(T),$
	U1(T)=RESID1(T),U2(T)=RESID2(T),$
	H = ||H11(T)|H12(T),H22(T)||,$
	U = ||U1(T),U2(T)||,$
	%LOGDENSITY(H,U)

*
*  Positive definite parameterization (BEKK,EK)
*  This enforces a positive definite covariance matrix by writing the
*  covariance matrix evolution as
*
*   V(t) = C'C + B'u(t)u(t)'B + A'V(t-1)A
*
*  Note that the parameters are not globally identified: changing the signs
*  of all members of C,B or A will have no effect on the function value.
*  Using METHOD=SIMPLEX to begin is quite important with this setup, to
*  pull the estimates away from zero before starting the derivative-based
*  methods.
*

NLPAR(SUBITERATION=1000)
NONLIN(ADD) VC11 VC12 VC22 $
            VA11 VA12 VA21 VA22 $
            VB11 VB12 VB21 VB22 

*
FRML H11F = (VC=||VC11,VC12|0.0,VC22||),$
  (VA=||VA11,VA12|VA21,VA22||),$
  (VB=||VB11,VB12|VB21,VB22||),$
  (H=||H11{1}|H12{1},H22{1}||),$
  (UB=||U1{1},U2{1}||*VB),$
  (H=TR(VC)*VC+%MQFORM(H,VA)+TR(UB)*UB),$
  H(1,1)
FRML H12F = H(1,2)
FRML H22F = H(2,2)

*
*  Initialize c's from the decomp of the covariance matrix
*
COMPUTE CINIT = %DECOMP(RR)
COMPUTE VC11=CINIT(1,1) , VC12 = CINIT(1,2) , VC22=CINIT(2,2)
*
COMPUTE VA11=VA22=0.3 , VA12=VA21=0.1
COMPUTE VB11=VB22=0.2 , VB12=VB21=0.1
*
MAXIMIZE(vcv,METHOD=SIMPLEX,RECURSIVE,robusterrors,ITERS=50) LOGL GSTART GEND
MAXIMIZE(vcv,METHOD=BFGS,RECURSIVE,robusterrors,ITERS=1000) LOGL GSTART GEND
COMPUTE HN = %DECOMP(H)
COMPUTE HN11 = HN(1,1)
COMPUTE HN22 = HN(2,2)
SET NRED1 = U1(T)/((HN11)**0.5)
SET NRED2 = U2(T)/((HN22)**0.5)
SET ROU1 = H12/(H11*H22)**0.5
STATISTICS ROU1

* period 2

smpl 1992:11:1 2002:12:31
SET DSCP =LOG(h1)-LOG(h1{1})
SET DSFP =LOG(h3)-LOG(h3{1})
SET ECT  =LOG(h3)-LOG(h1)

*  Starting and ending points for GARCH estimation. GSTART must
*  be at least entry 2.
*
COMPUTE gstart=1992:11:10, gend=2002:12:31


*
*  Parameters for the regression function
*

NONLIN B10 B11 B12 B13 C11 C12 C13 E11 B20 B21 B22 B23 C21 C22 C23 E22
FRML RESID1 = (DSCP-B10-B11*DSCP{1}-B12*DSCP{2}-B13*DSCP{3}-C11*DSFP{1}-C12*DSFP{2}-C13*DSFP{3}-E11*ECT{1})
FRML RESID2 = (DSFP-B20-B21*DSFP{1}-B22*DSFP{2}-B23*DSFP{3}-C21*DSCP{1}-C22*DSCP{2}-C23*DSCP{3}-E22*ECT{1})

*
*  Do initial regression. Copy initial values for regression parameters
*
LINREG  DSCP / R1
# CONSTANT DSCP{1} DSCP{2} DSCP{3} DSFP{1} DSFP{2} DSFP{3} ECT{1}
COMPUTE B10 = %BETA(1)
COMPUTE B11 = %BETA(2)
COMPUTE B12 = %BETA(3)
COMPUTE B13 = %BETA(4)
COMPUTE C11 = %BETA(5)
COMPUTE C12 = %BETA(6)
COMPUTE C13 = %BETA(7)
COMPUTE E11 = %BETA(8)
LINREG  DSFP / R2
# CONSTANT DSCP{1} DSCP{2} DSCP{3} DSFP{1} DSFP{2} DSFP{3} ECT{1}
COMPUTE B20 = %BETA(1)
COMPUTE B21 = %BETA(2)
COMPUTE B22 = %BETA(3)
COMPUTE B23 = %BETA(4)
COMPUTE C21 = %BETA(5)
COMPUTE C22 = %BETA(6)
COMPUTE C23 = %BETA(7)
COMPUTE E22 = %BETA(8)
*
*  Get the covariance matrix of the residuals.
*
VCV(MATRIX=RR,NOPRINT)
# R1 R2
DECLARE SERIES U1 U2
DECLARE SERIES H11 H12 H22
*
*  Initialize residuals
*
SET U1 = R1
SET U2 = R2

*
*  Seed the covariance matrix with values from it. This is important
*  for GARCH models: if the initial values of the H series are
*  unreasonable, the entire estimation is thrown off.
*
SET H11 = RR(1,1)
SET H22 = RR(2,2)
SET H12 = RR(1,2)

*
*  This is a standard log likelihood formula for any bivariate
*  ARCH, GARCH, ARCH-M,... The difference among these will be in
*  the definitions of H11F,H22F,H12F,RESID1 and RESID2
*
DECLARE SYMMETRIC H
DECLARE VECTOR    U
DECLARE FRML      H11F H12F H22F
*
FRML LOGL = $
	H11(T)=H11F(T),H12(T)=H12F(T),$
        H22(T)=H22F(T),$
	U1(T)=RESID1(T),U2(T)=RESID2(T),$
	H = ||H11(T)|H12(T),H22(T)||,$
	U = ||U1(T),U2(T)||,$
	%LOGDENSITY(H,U)

*
*  Positive definite parameterization (BEKK,EK)
*  This enforces a positive definite covariance matrix by writing the
*  covariance matrix evolution as
*
*   V(t) = C'C + B'u(t)u(t)'B + A'V(t-1)A
*
*  Note that the parameters are not globally identified: changing the signs
*  of all members of C,B or A will have no effect on the function value.
*  Using METHOD=SIMPLEX to begin is quite important with this setup, to
*  pull the estimates away from zero before starting the derivative-based
*  methods.
*

NLPAR(SUBITERATION=1000)
NONLIN(ADD) VC11 VC12 VC22 $
            VA11 VA12 VA21 VA22 $
            VB11 VB12 VB21 VB22 

*
FRML H11F = (VC=||VC11,VC12|0.0,VC22||),$
  (VA=||VA11,VA12|VA21,VA22||),$
  (VB=||VB11,VB12|VB21,VB22||),$
  (H=||H11{1}|H12{1},H22{1}||),$
  (UB=||U1{1},U2{1}||*VB),$
  (H=TR(VC)*VC+%MQFORM(H,VA)+TR(UB)*UB),$
  H(1,1)
FRML H12F = H(1,2)
FRML H22F = H(2,2)

*
*  Initialize c's from the decomp of the covariance matrix
*
COMPUTE CINIT = %DECOMP(RR)
COMPUTE VC11=CINIT(1,1) , VC12 = CINIT(1,2) , VC22=CINIT(2,2)
*
COMPUTE VA11=VA22=0.3 , VA12=VA21=0.1
COMPUTE VB11=VB22=0.2 , VB12=VB21=0.1
*
MAXIMIZE(vcv,METHOD=SIMPLEX,RECURSIVE,robusterrors,ITERS=50) LOGL GSTART GEND
MAXIMIZE(vcv,METHOD=BFGS,RECURSIVE,robusterrors,ITERS=1000) LOGL GSTART GEND
COMPUTE HN = %DECOMP(H)
COMPUTE HN11 = HN(1,1)
COMPUTE HN22 = HN(2,2)
SET NRED1 = U1(T)/((HN11)**0.5)
SET NRED2 = U2(T)/((HN22)**0.5)
SET ROU2 = H12/(H11*H22)**0.5
STATISTICS ROU2




TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Trivariate VAR-GARCH model

Unread post by TomDoan »

jessie wrote:Hi

I am using a trivariate VAR-MGARCH model to analyse volatility spillovers in my dissertation. I obtained the RATS codes from the author of a journal article that used a VECM-GARCH. However, I have never used RATS before and do not know how to alter the code from a bivariate VECM-GARCH model to a trivariate VAR-GARCH. Can anyone help me with the code please?

Cheers,

Jessie

Code: Select all

* Euro_UK 
*
calendar(daily) 1983 1 3
allocate 100 2002:12:31
* UK interbank (proxy A)  UK cd (proxy B) eurostirling
open data a:\uk.txt
data(format=free,org=obs) / h1 h2 h3 

* period 1, september 92 - erm crisis

smpl 1983:1:3 1992:7:31
SET DSCP =LOG(h1)-LOG(h1{1})
SET DSFP =LOG(h3)-LOG(h3{1})
SET ECT  =LOG(h3)-LOG(h1)

(snip rest)

That's really old code. I'm assuming that that's the bivariate VECM GARCH that someone gave you. From where I cut that off, this will do the VECM-BEKK estimation:

Code: Select all

*
* Define a three lag VAR with the gap being a cointegrating vector
*
system(define=vecmmodel)
variables dscp dsfp
lags 1 to 3
det constant ect{1}
end(system)
*
* Estimate by GARCH-BEKK
*
garch(model=vecmmodel,mv=bekk,hmatrices=h,rvector=r,$
  pmethod=simplex,piters=5,method=bfgs,iters=400)  1983:1:10 1992:7:31
*
* Compute correlations from GARCH model
*
set rou1 = h(t)(1,2)/sqrt(h(t)(1,1)*h(t)(2,2))
statistics rou1
*
* NRED1 and NRED2 are probably supposed to be standardized residuals, but they aren't
* being computed correctly. This computes standardized residuals into stdresids(1) and
* stdresids(2)
*
dec vect[series] stdresids(2)
do i=1,2
   set stdresids(i) = ([vector] stdr=inv(%decomp(h(t)))*r(t)),stdr(i)
end do i
The estimation for the second time period can be done by just adding:

Code: Select all

garch(model=vecmmodel,mv=bekk,hmatrices=h,rvector=r,$
  pmethod=simplex,piters=5,method=bfgs,iters=400)  1992:11:10 2002:12:31
and the diagnostic calculations following the GARCH as before.

To go to three variables, you would change the VARIABLES line to include the three variables. I'm assuming that you might need two "ect" variables, so create the other one, and include it in the "DET" line in the definition of the VAR. Everything else works pretty much the same, except that you have three empirical correlations, not just one, and three standarized residuals rather than two.
jessie
Posts: 6
Joined: Fri Aug 14, 2009 5:28 am

Re: Trivariate VAR-GARCH model

Unread post by jessie »

Thank you so much Tom.

Is it possible to change this code to do a VAR-GARCH? I don't need the error correction term. I tried changing all the instructions where it has vecm to var e.g. "system(define=vecmmodel)" to "system(model=varmodel)" and "garch(model=vecmmodel..." to "garch(model=varmodel..." but it still gives me the VECM in the mean. Could you please have a look at it for me one more time?

Cheers,

Jessie
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Trivariate VAR-GARCH model

Unread post by TomDoan »

This would be the definition of the VAR if you don't want the error correction term. (Delete the ect{1} from the DET instruction).

Code: Select all

system(model=vecmmodel)
variables dscp dsfp
lags 1 to 3
det constant
end(system)
jessie
Posts: 6
Joined: Fri Aug 14, 2009 5:28 am

Re: Trivariate VAR-GARCH model

Unread post by jessie »

Tom, thank you for your help again.

Regarding inserting a third variable into the code, I added it in the VARIABLES line as you suggested. Do I have to change the correlation computation and the residuals to account for the third variable too. I did the following. Could you please confirm whether it is correct?

Also, could you show me how to plot the values of the conditional variances and covariances over time?

Thanks a lot,

Jessie

Code: Select all

* Compute correlations from GARCH model
*
set rou1 = h(t)(1,3)/sqrt(h(t)(1,1)*h(t)(3,3))
statistics rou1
*
* NRED1 and NRED2 are probably supposed to be standardized residuals, but they aren't
* being computed correctly. This computes standardized residuals into stdresids(1) and
* stdresids(2)
*
dec vect[series] stdresids(3)
do i=1,2,3
   set stdresids(i) = ([vector] stdr=inv(%decomp(h(t)))*r(t)),stdr(i)
end do i

TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Trivariate VAR-GARCH model

Unread post by TomDoan »

You have the syntax on the

Code: Select all

DO
wrong. It should be:

Code: Select all

dec vect[series] stdresids(3)
do i=1,3
   set stdresids(i) = ([vector] stdr=inv(%decomp(h(t)))*r(t)),stdr(i)
end do i
the do i=1,2,3 goes from 1 to 2 by steps of 3, which is not what you want.
jessie
Posts: 6
Joined: Fri Aug 14, 2009 5:28 am

Re: Trivariate VAR-GARCH model

Unread post by jessie »

Thank you very much for the reply.

I have a few more questions regarding the VAR-GARCH(1,1).

1) When I got my results, it says 'NO CONVERGENCE IN 41 ITERATIONS'. I read in a RATS handbook that I need to use NLPAR when the convergence problem occurs. However, I am not sure where to add this in the codes;

2) I want to check the Ljung-Box Q-statistics for the residuals and squared residuals;

and 3) plot the values of the conditional variances and covariances over time.

Would you advise me how to do these?

Many thanks,

Jessie
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Trivariate VAR-GARCH model

Unread post by TomDoan »

jessie wrote:Thank you very much for the reply.

I have a few more questions regarding the VAR-GARCH(1,1).

1) When I got my results, it says 'NO CONVERGENCE IN 41 ITERATIONS'. I read in a RATS handbook that I need to use NLPAR when the convergence problem occurs. However, I am not sure where to add this in the codes;
With multivariate GARCH models, you can sometimes take care of that by simply multiplying your dependent variables by 100:

Code: Select all

SET DSCP =100.0*(LOG(h1)-LOG(h1{1}))
SET DSFP =100.0*(LOG(h3)-LOG(h3{1}))
With fractional return data, the constant parameters in the variance equations can be so small that it's hard to tell when they're converged. Multiplying the data by 100 changes those parameters by a factor of 10000 (putting them within a few orders of magnitude of 1), which can eliminate some of the numerical problems.
jessie wrote:2) I want to check the Ljung-Box Q-statistics for the residuals and squared residuals;

and 3) plot the values of the conditional variances and covariances over time.

Would you advise me how to do these?

Many thanks,

Jessie
Check out the Tsay textbook examples: tsayp449.prg and tsayp452.prg.
luxu1983
Posts: 61
Joined: Wed Aug 12, 2009 10:53 pm

Re: Trivariate VAR-GARCH model

Unread post by luxu1983 »

Code: Select all

dec vect[series] stdresids(3)
do i=1,3
   set stdresids(i) = ([vector] stdr=inv(%decomp(h(t)))*r(t)),stdr(i)
end do i
the do i=1,2,3 goes from 1 to 2 by steps of 3, which is not what you want.[/quote]

dear Tom
can this code computes standardized residuals of the dcc- mgarch model?
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Trivariate VAR-GARCH model

Unread post by TomDoan »

luxu1983 wrote:

Code: Select all

dec vect[series] stdresids(3)
do i=1,3
   set stdresids(i) = ([vector] stdr=inv(%decomp(h(t)))*r(t)),stdr(i)
end do i
dear Tom
can this code computes standardized residuals of the dcc- mgarch model?
Yes. Note that there are many ways to compute standardized residuals for a multivariate GARCH model. The code above gives a set of standardized residuals which are also uncorrelated with each other. (The covariance matrix of stdresids is the identity). If you just want to standardize each separately, you can do the simpler:

Code: Select all

dec vect[series] stdresids(3)
do i=1,3
   set stdresids(i) = r(t)/sqrt(h(t)(i,i))
end do i
jessie
Posts: 6
Joined: Fri Aug 14, 2009 5:28 am

Re: Trivariate VAR-GARCH model

Unread post by jessie »

Dear Tom,
Thank you for your recommendations. I followed the sample programme and obtained the graph.

However, when I did the Ljung-Box Q-stats, it gave me the following error messages: "## I2. Expected Instruction Here
>>>>@regcorrs(Ð<<<<
## CP18. REGCORRS is not the Name of a PROCEDURE. (Did you forget to SOURCE?)
>>>>@regcorrs(<<<<"

I changed the codes to

Code: Select all

@regcorrs(number=13) stdresids(1)
@regcorrs(number=13) stdresids(2)
@regcorrs(number=13) stdresids(3)

set stdresids(1)sq = stdresids(1)**2
set stdresids(2)sq = stdresids(2)**2
set stdresids(3)sq = stdresids(3)**2

@regcorrs(number=13) stdresids(1)**2
@regcorrs(number=13) stdresids(2)**2
@regcorrs(number=13) stdresids(3)**2
*
@mvqstat(lags=10)
# stdds stdidiff stddediff
@mvqstat(lags=10)
# stdresids(1) stdresids(2) stdresids(3)
Could you please tell me where I did things wrong?

Many thanks,

Jessie
moderator
Site Admin
Posts: 269
Joined: Thu Oct 19, 2006 4:33 pm

Re: Trivariate VAR-GARCH model

Unread post by moderator »

We'd probably need to see the entire program to identify the mistake. Why don't you send the code and data to

support@estima.com

Please include your full name and RATS serial number and we'll take a look.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Trivariate VAR-GARCH model

Unread post by TomDoan »

jessie wrote:Dear Tom,
Thank you for your recommendations. I followed the sample programme and obtained the graph.

However, when I did the Ljung-Box Q-stats, it gave me the following error messages: "## I2. Expected Instruction Here
>>>>@regcorrs(Ð<<<<
## CP18. REGCORRS is not the Name of a PROCEDURE. (Did you forget to SOURCE?)
>>>>@regcorrs(<<<<"
REGCORRS is one of the standard (external) RATS procedures. You may need to check with your IT department to see where those are loaded (if you're running off a network). It's on the file regcorrs.src. You can also get that from our web site, but you have to make sure that you get a copy that will work with your older version of RATS. Save the regcorrs.src that you find on this page:

http://www.estima.com/cgi-bin/testfile. ... erence=Any
jessie
Posts: 6
Joined: Fri Aug 14, 2009 5:28 am

Re: Trivariate VAR-GARCH model

Unread post by jessie »

Dear Tom

Thank you very much for your help. I now have my VAR-GARCH results.

I wonder whether I can do Granger causality tests to analyse the coefficients from the VAR system (mean equations)? Can you help me with the coding please?

Many thanks,

Jessie
prashantj
Posts: 88
Joined: Sun Apr 11, 2010 2:56 am

Re: Trivariate VAR-GARCH model

Unread post by prashantj »

Thanks Tom, When I test for standardized resiudals using the following syntax,
dec vect[series] stdresids(3)
do i=1,3
set stdresids(i) = ([vector] stdr=inv(%decomp(h(t)))*r(t)),stdr(i)
end do i

But I got the following message:
## MAT15. Subscripts Too Large or Non-Positive
Error was evaluating entry 1484
The Error Occurred At Location 0109 of loop/block
Line 2 of loop/block

I wish to get standardized residuals seperately for four series under consideration. So I replace 3 with 4 and run the syntax but the message is the same. Kindly help me out.
Post Reply