Page 1 of 1

VAR-Multivariate GARCH Programming Help Needed

Posted: Wed Oct 26, 2011 4:33 am
by hdhosky
Hi all,

I am a beginner user of the winRATS and have some problems when I try to implement the VAR-multivariate GARCH where I have 2 asset return variables. I chose the constant correlation model for the multivariate GARCH and below is my code.

Code: Select all

OPEN DATA C:\FOLDER\FTSESP500.XLS
CALENDAR(M) 1984:4
ALLOCATE 2011:7
DATA(FORMAT=XLS,ORG=COLUMNS) 1984:4 2011:7 FTSE SP500
COM GSTART=1984:9, GEND=2011:7
*
* Set continuously compounded returns
SET Y1 = 100*LOG(SP500/SP500{1})
SET Y2 = 100*LOG(FTSE/FTSE{1})
*
* Parameters for the regression function
NONLIN B11 B12 B13 B21 B22 B23
FRML RESID1 = (Y1-B11-B12*Y1{1}-B13*Y2{1})
FRML RESID2 = (Y2-B21-B22*Y1{1}-B23*Y2{1})
*
* DO INITIAL REGRESSION. COPY INITIAL VALUES FOR REGRESSION PARAMETERS
LINREG Y1 / R1
# CONSTANT Y1{1} Y2{1}
COM B11 = %BETA(1), B12 = %BETA(2), B13 = %BETA(3)
LINREG Y2 / R2
# CONSTANT Y1{1} Y2{1}
COM B21 = %BETA(1), B22 = %BETA(2), B23 = %BETA(3)
*
*GET THE COVARIANCE MATRIX OF THE RESIDUALS.
VCV(MATRIX=RR,NOPRINT)
# R1 R2
*
* U1 AND U2 ARE THE RESIDUAL SERIES
* H11, H12, AND H22 ARE THE VARIANCES AND COVARIANCE
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
SET H11 = RR(1,1)
SET H22 = RR(2,2)
SET H12 = RR(1,2)
*
*STANDARD LOG LIKELIHOOD FORMULA
DECLARE SYMMETRIC H
DECLARE VECTOR U
DECLARE FRML H11F H22F H12F LOGL
*
FRML LOGL = $
H11(T)= H11F(T) , H22(T)=H22F(T) ,H12(T)=H12F(T) , $
U1(T) = RESID1(T) , U2(T) = RESID2(T),$
H = ||H11(T)|H12(T),H22(T)|| , U = ||U1(T),U2(T)||, $
%LOGDENSITY(H,U)
*
NLPAR(SUBITS=50)
*
* CONSTANT CORRELATION MODEL
NONLIN(ADD) VC11 VC12 VC22 VB11 VB22 VA11 VA22
FRML H11F = VC11 + VA11 * H11{1} + VB11 * U1{1}**2
FRML H22F = VC22 + VA22 * H22{1} + VB22 * U2{1}**2
FRML H12F = VC12*SQRT(H11(T)*H22(T))
COMPUTE VC11 = RR(1,1) , VC22 = RR(2,2) , VC12 = RR(1,2)/SQRT(VC11*VC22)
COMPUTE VB11 = VB22 = VA11 = VA22 = 0.05
MAXIMIZE(METHOD=BFGS,PMETHOD=SIMPLEX,PITERS=5,ROBUST,RECURSIVE,ITERS=100)
LOGL GSTART GEND
*
* RESIDUAL DIAGNOSTICS
SET U11 = U1(T)/SQRT(H11(T))
SET UU1 = (U1(T)*U1(T))/H11(T)
STATISTICS U11
STATISTICS UU1
CORR(NUMBER=10,QSTATS) U11 6 GEND
CORR(NUMBER=10,QSTATS) UU1 6 GEND

SET U22 = U2(T)/SQRT(H22(T))
SET UU2 = (U2(T)*U2(T))/H22(T)
STATISTICS U22
STATISTICS UU2
CORR(NUMBER=10,QSTATS) U22 6 GEND
CORR(NUMBER=10,QSTATS) UU2 6 GEND
*
SPGRAPH(VFIELDS=3,HFIELDS=1,HEADER="CONSTANT CORRELATION MODEL")
GRAPH(HEADER="CONDITIONAL VARIANCE", SUBHEADER="SP500") 1
# H11 GSTART GEND
GRAPH(HEADER="CONDITIONAL VARIANCE", SUBHEADER="FTSE") 1
# H22 GSTART GEND
GRAPH(HEADER="CONDITIONAL COVARIANCE",SUBHEADER="SP500 & FTSE") 1
# H12 GSTART GEND
SPGRAPH(DONE)
*
As seen from above, I end up with nice little conditional variances and covariances of the return variables.
But what I want to do but cannot is that I want to get values for each of H11, H12 and H12 at each time period. In other words, I want to get the conditional variance covariance matrix for each time period from this VAR-constant correlation multivariate GARCH model.

Could any one help me with this? I will really appreciate your response in advance.

Have a great day.

Cheers.

Re: VAR-Multivariate GARCH Programming Help Needed

Posted: Wed Oct 26, 2011 11:21 am
by moderator
The variables H11, H12, and H22 are all time series containing the variances (covariances for H12) over time, so those should have the information you need.

If you really need this information in the form of arrays (one array for each time period), there are several ways to go about that. The choice of which to use would depend on what you want to do with the arrays. One option would be to use DECLARE and GSET to create a symmetric array of series. You could also define a formula of symmetric arrays. If you can tell me more about what you want to do with the arrays, I can offer a specific suggestion.

Also, is there a particular reason you aren't using the built-in GARCH instruction for this? Unless I'm missing something about the setup that GARCH can't handle, using the built-in command would allow you to reduce the entire program to about 10 lines or so.

Regards,
Tom Maycock
Estima

Re: VAR-Multivariate GARCH Programming Help Needed

Posted: Thu Oct 27, 2011 12:44 am
by hdhosky
Hi Tom,

Firstly, thank you very much for such a prompt reply.

I need to get the conditional variance/ covariance matrixs throughout time and it seems like what you said ' ... you really need this information in the form of arrays (one array for each time period) ' is what I want.
The reason why I need this information is because I want to compute the variance of cumulative returns, within this VAR(1)-CCORR Multivariate Garch model freamwork.

For your convenience, I attach the paper in which I am interested. The authors develop a simple VAR(1) model where they extract the first two moments of the cumulative returns. Since they do not have a model for volatility, they assume the conditional variance/covariance matrix of the residuals in the VAR(1) which is constant throughout time. This particular matrix is used to compute the cumulative returns' variances. Please refer to the pages 30 - 32 (under the Appendix 8.1.1.) of the attachment.

What I am trying to do is to repeat the exercise done by the authors by using different data set and return dynamics model, namely VAR(1)-CCORR Multivariate GARCH model. I want to see the effect the CCORR model has when computing the variance of the cumulative returns since I will have different conditional variance/covariance matrices throughout time.

To explain the particular reason I am not using the built-in GARCH instruction for this is because I do not know how to :( I got the code off the lecture notes and was just playing around by myself. As much as I find it interesting to learn stuff about winRATS, I am having troubles like now and for that reason, I would really like to appreciate your tremendous help.

Cheers.

Ryan

Re: VAR-Multivariate GARCH Programming Help Needed

Posted: Thu Oct 27, 2011 10:29 am
by moderator
First, with respect to using the GARCH instruction, as I noted earlier, it's vastly simpler than writing out the entire model with NONLIN and FRML statements, manually initializing variables with COMPUTE and SET, and estimating with MAXIMIZE, because you can do all of that with a single instruction. In terms of learning how to do it, just read the relevant section of the User's Guide and Reference Manual--that's what they are there for. See also the GARCHMV.RPF example program included with RATS (it's called GARCHMV.PRG in older versions). For example, here's an implementation of a multivariate VAR-GARCH model from the most recent version of that example program:

*
* VAR(1) model for the mean, BEKK for the variance
*
system(model=var1)
variables xjpn xfra xsui
lags 1
det constant
end(system)
*
garch(p=1,q=1,model=var1,mv=bekk,pmethod=simplex,piters=10)

With respect to the arrays over time, the best way to set up what you want depends on exactly how you want to use the result--i.e. specifically what sort of computation(s) you want to do with these arrays. If you want a SERIES of SYMMETRICS from H11, H12, and H22, you could do

dec series[symm] hmat
gset hmat = ||h11|h12,h22||

Each entry of the series HMAT will contain a symmetric array. For example, to see the values in the "gstart" element, you could do

display hmat(gstart)

With respect to using the built-in GARCH command, you can create such a series of arrays containing the covariances simply by adding the HMATRICES option. For example:

garch(p=1,q=1,model=var1,mv=bekk,pmethod=simplex,piters=10,hmatrices=hmat)

Regards,
Tom Maycock

Re: VAR-Multivariate GARCH Programming Help Needed

Posted: Sat Oct 29, 2011 4:01 am
by hdhosky
Hi Tom,

Thank you very much for your reply. I see how simple the things can be done now ! Thanks also for referring me to the User's Guide haha

Just a couple of more things though

First, If I was to implement a VAR(1)-constant correlation multivariate GARCH model, then what would the commands be?

I tried the following but it did not work.

*
* VAR(1) model for the mean, BEKK for the variance
*
system(model=var1)
variables xjpn xfra xsui
lags 1
det constant
end(system)
estimate
*
garch(p=1,q=1,model=var1,mv=ccorr,pmethod=simplex,piters=10)


Also, if I was to implement such model with 8 variables, collected using monthly data over 15year's time (so 180 observations for each variable), do you reckon the sample is too small that there would be convergence problems (e.g. No convergence / convergence to local maxima etc.)?


Thanks again in advance.

Ryan

Re: VAR-Multivariate GARCH Programming Help Needed

Posted: Sat Oct 29, 2011 9:53 am
by TomDoan
The MV option should be MV=CC not MV=CCORR:

garch(p=1,q=1,model=var1,mv=cc,pmethod=simplex,piters=10)

A CC model on a VAR(1) on 8 variable probably would converge fine even with only 150 data points. A more more heavily parameterized model like a DVEC or BEKK probably wouldn't work.

Re: VAR-Multivariate GARCH Programming Help Needed

Posted: Fri Nov 04, 2011 2:04 am
by hdhosky
Hi Tom, it is me again.

So I tried to run a VAR(1)-CC MGARCH model with 8 variables (4 return variables and 4 predictor variables) using the simple command you have taught me.

*
* VAR(1) model for the mean, BEKK for the variance
*
system(model=var1)
variables rcr bxr sxr gxr ncr lys ldp lcs
lags 1
det constant
end(system)
estimate
garch(p=1,q=1,model=var1,mv=cc,pmethod=simplex,iters=300,piters=10,hmatrices=hmat)
display hmat(gstart)
*

All the parameters have been estimated, however, I got an error message "## MAT1. Matrix HMAT(1) Has Not Been Dimensioned" and I did not get a HMAT matrix.

Do you find any possible explanations for this problem and maybe solutions?

Cheers.

Ryan

Re: VAR-Multivariate GARCH Programming Help Needed

Posted: Fri Nov 04, 2011 10:33 am
by moderator
I don't see anywhere in that segment of code where you are setting GSTART? The most likely explanation for the error you are getting would be that the HMAT variable isn't defined for the time period in question, which would suggest that GSTART is not set properly.

A simple test would be to use the %REGSTART() function, which returns the starting period of the most recent estimation. For example, if this works:

dis hmat(%regstart())

then the problem is with how GSTART is set. You could compare it to the estimation range by doing something like

display gstart %regstart() %regend()

Use the %datelabel() function if you want to see those in terms of dates:

display %datelabel(gstart) %datelabel(%regstart()) etc.

Regards,
Tom Maycock

Re: VAR-Multivariate GARCH Programming Help Needed

Posted: Sat Nov 05, 2011 10:48 pm
by hdhosky
Hey Tom,

THanks to you, I got the model to work.

Now I would to extract the conditional variances/covariances as a set of individual series and graph them as well. I know that you told me how to extract it by a Hmatrix at a given point in time, however, I found that I have more than 800 observations and in order to get a set of time series data of the conditional variances/covariances, it is rather cumbersome to do it that way.

I have the following code for the VAR(1)-Constant Correlation MGARCH model for 8 variables.

Also, anyway that I could save/declare/extract the residual and residual squared series for each variables I get from this model? I need them to perform the residual diagnostics.

Code: Select all

*
* Program for VAR(1)-CCORR Multivariate GARCH for 8 variables
*
open data e:\d\test\test(2).xls
calendar(w) 1994:12:30
allocate 2011:07:01
data(format=xls,org=columns) 1994:12:30 2011:07:01 rcr bxr sxr gxr ncr lys ldp lcs
com gstart=1995:01:06, gend=2011:07:01
* VAR(1) model for the mean, BEKK for the variance
*
system(model=var1)
variables rcr bxr sxr gxr ncr lys ldp lcs
lags 1
det constant
end(system)
estimate
garch(p=1,q=1,model=var1,mv=cc,pmethod=simplex,piters=10,hmatrices=hmat)
*
SPGRAPH(VFIELDS=2,HFIELDS=4,HEADER="CONSTANT CORRELATION MODEL",SUBHEADER="CONDITIONAL VARIANCES")
GRAPH(HEADER="CONDITIONAL VARIANCE", SUBHEADER="RCR") 1
# H11 GSTART GEND
GRAPH(HEADER="CONDITIONAL VARIANCE", SUBHEADER="BXR") 1
# H22 GSTART GEND
GRAPH(HEADER="CONDITIONAL VARIANCE", SUBHEADER="SXR") 1
# H33 GSTART GEND
GRAPH(HEADER="CONDITIONAL VARIANCE", SUBHEADER="GXR") 1
# H44 GSTART GEND
GRAPH(HEADER="CONDITIONAL VARIANCE", SUBHEADER="NCR") 1
# H55 GSTART GEND
GRAPH(HEADER="CONDITIONAL VARIANCE", SUBHEADER="LYS") 1
# H66 GSTART GEND
GRAPH(HEADER="CONDITIONAL VARIANCE", SUBHEADER="LDP") 1
# H77 GSTART GEND
GRAPH(HEADER="CONDITIONAL VARIANCE", SUBHEADER="LCS") 1
# H88 GSTART GEND
SPGRAPH(DONE)
*
*RESIDUAL DIAGNOSTICS [SEE BOLLERSLEV(1987)]
*
SET U11 = U1(T)/SQRT(H11(T))
SET UU1 = (U1(T)*U1(T))/H11(T)
STATISTICS U11
STATISTICS UU1*
Could you help me here? :)

I appreciate your help in advance.

Re: VAR-Multivariate GARCH Programming Help Needed

Posted: Mon Nov 07, 2011 9:18 am
by moderator
The GARCH section of the Reference Manual should tell you what you need to know.

Note in particular the MVHSERIES option, which stores the varaiances/covariances in an array of series, rather than series of an arrays (which is what HMAT does). Use the former option if you want to work with this data as series rather than as arrays.

Similarly, the Reference Manual documents the RVECTORS option, which stores the estimated residuals. Like HMAT, it uses the "series of arrays" format, so you'd need to use SET instructions to extract the desired information.

See the GARCHMV.RPF example program included with RATS. Also:

http://www.estima.com/forum/viewtopic.php?f=11&t=1081

Regards,
Tom Maycock