Page 1 of 1

regime-switch VAR-GARCH-M

Posted: Tue Mar 29, 2011 4:46 am
by huanpipt
I'm trying to combine the VAR-GARCH-M with regime-switching.
But I met some problems.
The code below is estimate the VAR-GARCH-M in 3 regimes depending on dSP{1}
But when running the code

Code: Select all

*
* This does a simple GARCH(1,1) model for the variance.
*
dec symm VA1(n,n) VA2(n,n) VA3(n,n) VB1(n,n) VB2(n,n) VB3(n,n) VC1(n,n) VC2(n,n) VC3(n,n)

compute VA1=WWW1,VA2=WWW2,VA3=WWW3,VB1=%const(0.50),VB2=%const(0.50),VB3=%const(0.50),VC1=%const(0.05),VC2=%const(0.05),VC3=%const(0.05)


nonlin(parmset=garchparms1) VA1 VB1 VC1 
frml Gf1 = VA1+VC1.*G1{1}+VB1.*uu1{1}
nonlin(parmset=garchparms2) VA2 VB2 VC2 
frml Gf2 = VA2+VC2.*G2{1}+VB2.*uu2{1}
nonlin(parmset=garchparms3) VA3 VB3 VC3
frml Gf3 = VA3+VC3.*G3{1}+VB3.*uu3{1}

set LOGLL = logl1 + logl2 + logl3

maximize(trace,parmset=meanparms+garchparms1+garchparms2+garchparms3,pmethod=simplex,piters=10,method=bfgs,iters=400) LOGLL * gend
I got error message
""## MAT15. Subscripts Too Large or Non-Positive
Error was evaluating entry 1""

I tried to divide the sample in advance, but the error message
""## MAT15. Subscripts Too Large or Non-Positive"" is still appeared.

Could anyone help me with this?

my whole code is shown below.
The attachment are the data and the whole code.

Code: Select all

*open the file (m)代表月 data後面接起始日與結束日
open data TD5_final.xls
CALENDAR(irregular)
data(format=xls,org=columns) / sp f1m
set dsp = sp - sp{1}
set df1m = f1m - f1m{1}
*graph SP(畫好多張圖) vfields = 幾張 footer = 標題名稱
print / dsp

spgraph(vfields=2,$
     footer="Figure 2. the changes")
graph(vlabel="spot freight rate")
# dSP
graph(vlabel="1-month forward freight rate")
# dF1M

spgraph(done)







******************************************************************************
***********************************VAR-GARCH-M Model**************************

******************************************************************************
set dSP1 = dSP{1}

set Lam1 2 1578 = (dsp1>-22.and.dsp1<4.)
set Lam2 2 1578 = (dsp1>4.)
print / Lam1


dec series[vect] La1
dec series[vect] La2
gset La1 = ||Lam1||
gset La2 = ||Lam2||


compute n=2
compute gstart=2,gend=1578
*
dec series[vect] Y
dec frml[vect] R1 R2 R3
dec vect[series] U1(n) U2(n) U3(n)
*
*
* The paths of the covariance matrices and uu' are saved in the
* SERIES[SYMM] named H and UU. UX and HX are used for the current values
* of the residual vector and H matrices
*
declare series[symm] G1 G2 G3 uu1 uu2 uu3
*
* ux is used when extracting a u vector
*
declare symm Gx1(n,n) Gx2(n,n) Gx3(n,n)
declare vect ux1(n) ux2(n) ux3(n)
*
* These will be the parameters for the mean equations. These are adjusted to add
* variance or covariance terms as needed.
*
dec vect A1(n) A2(n) A3(n)
dec rect B1(n,n) B2(n,n) B3(n,n) C1(n,n) C2(n,n) C3(n,n)
nonlin(parmset=meanparms) A B C
*
* Mean model = VAR(1) with sqrt(G) "M" term
*
frml R = Y-A1-A2*La1-A3*La2-B1*Y{1}-B2*La1*Y{1}-B3*La2*Y{1}-C1*%xdiag(G1)-C2*La1*%xdiag(G2)-C3*La2*%xdiag(G3)
*
gset Y = ||dsp,df1m||
*
* Run preliminary VAR(4) to get estimates of residuals
*
*
linreg(smpl=dsp1<-22) dsp / u1(1)
# constant dsp{1 to 4} df1m{1 to 4}
linreg(smpl=dsp1>-22.and.dsp1<4.) dsp / u2(1)
# constant dsp{1 to 4} df1m{1 to 4}
linreg(smpl=dsp1>4) dsp / u3(1)
# constant dsp{1 to 4} df1m{1 to 4}
linreg(smpl=dsp1<-22) df1m / u1(2)
# constant dsp{1 to 4} df1m{1 to 4}
linreg(smpl=-22<dsp1<4) df1m / u2(2)
# constant dsp{1 to 4} df1m{1 to 4}
linreg(smpl=dsp1>4) df1m / u3(2)
# constant dsp{1 to 4} df1m{1 to 4}
*
linreg(smpl=dsp1<-22) dsp
# constant dsp{1} df1m{1}  u2(1){1} u2(2){1}
compute A1(1)=%beta(1),B1(1,1)=%beta(2),B1(1,2)=%beta(3),$
                      C1(1,1)=%beta(4),C1(1,2)=%beta(5)
linreg(smpl=dsp1>-22.and.dsp1<4.) dsp
# constant dsp{1} df1m{1}  u1(1){1} u1(2){1}
compute A2(1)=%beta(1),B2(1,1)=%beta(2),B2(1,2)=%beta(3),$
                      C2(1,1)=%beta(4),C2(1,2)=%beta(5)
linreg(smpl=dsp1>4) dsp
# constant dsp{1} df1m{1}  u3(1){1} u3(2){1}
compute A3(1)=%beta(1),B3(1,1)=%beta(2),B3(1,2)=%beta(3),$
                      C3(1,1)=%beta(4),C3(1,2)=%beta(5)
linreg(smpl=dsp1<-22) df1m
# constant dsp{1} df1m{1}  u2(1){1} u2(2){1}
compute A1(1)=%beta(1),B1(1,1)=%beta(2),B1(1,2)=%beta(3),$
                      C1(1,1)=%beta(4),C1(1,2)=%beta(5)
linreg(smpl=dsp1>-22.and.dsp1<4.) df1m
# constant dsp{1} df1m{1}  u1(1){1} u1(2){1}
compute A2(1)=%beta(1),B2(1,1)=%beta(2),B2(1,2)=%beta(3),$
                      C2(1,1)=%beta(4),C2(1,2)=%beta(5)
linreg(smpl=dsp1>4) df1m
# constant dsp{1} df1m{1}  u3(1){1} u3(2){1}
compute A3(1)=%beta(1),B3(1,1)=%beta(2),B3(1,2)=%beta(3),$
                      C3(1,1)=%beta(4),C3(1,2)=%beta(5)


vcv(matrix=WWW1)
# u1
vcv(matrix=WWW2)
# u2
vcv(matrix=WWW3)
# u3

*
* These are used to initialize pre-sample variances.
*
gset G1  * gend = WWW1
gset G2  * gend = WWW2
gset G3  * gend = WWW3
gset uu1 * gend = WWW1
gset uu2 * gend = WWW2
gset uu3 * gend = WWW3
set u1(1) = 0.0
set u2(1) = 0.0
set u3(1) = 0.0
set u1(2) = 0.0
set u2(2) = 0.0
set u3(2) = 0.0
*
declare frml[symm] Gf1 Gf2 Gf3
*
frml logl1 = $
    Gx1    = Gf1(t) , $
    G1(t)  = Gx1, $
    ux1    = R1 , $
    uu1(t) = %outerxx(ux1), $
    %pt(u1,t,ux1),$
    %logdensity(Gx1,ux1)
frml logl2 = $
    Gx2    = Gf2(t) , $
    G2(t)  = Gx2, $
    ux2    = R2 , $
    uu2(t) = %outerxx(ux2), $
    %pt(u2,t,ux2),$
    %logdensity(Gx2,ux2)
frml logl3 = $
    Gx3    = Gf3(t) , $
    G3(t)  = Gx3, $
    ux3    = R3 , $
    uu3(t) = %outerxx(ux3), $
    %pt(u3,t,ux3),$
    %logdensity(Gx3,ux3)

*
* This does a simple GARCH(1,1) model for the variance.
*
dec symm VA1(n,n) VA2(n,n) VA3(n,n) VB1(n,n) VB2(n,n) VB3(n,n) VC1(n,n) VC2(n,n) VC3(n,n)

compute VA1=WWW1,VA2=WWW2,VA3=WWW3,VB1=%const(0.50),VB2=%const(0.50),VB3=%const(0.50),VC1=%const(0.05),VC2=%const(0.05),VC3=%const(0.05)


nonlin(parmset=garchparms1) VA1 VB1 VC1 
frml Gf1 = VA1+VC1.*G1{1}+VB1.*uu1{1}
nonlin(parmset=garchparms2) VA2 VB2 VC2 
frml Gf2 = VA2+VC2.*G2{1}+VB2.*uu2{1}
nonlin(parmset=garchparms3) VA3 VB3 VC3
frml Gf3 = VA3+VC3.*G3{1}+VB3.*uu3{1}

set LOGLL = logl1 + logl2 + logl3

maximize(trace,parmset=meanparms+garchparms1+garchparms2+garchparms3,pmethod=simplex,piters=10,method=bfgs,iters=400) LOGLL * gend
*
* This does a BEKK model for the variance
*
dec rect VBB1(n,n) VBB2(n,n) VBB3(n,n) VCC1(n,n) VCC2(n,n) VCC3(n,n)
compute VBB1=.05*%identity(n),VBB2=.05*%identity(n),VBB3=.05*%identity(n),VCC1=.50*%identity(n),VCC2=.50*%identity(n),VCC3=.50*%identity(n)


nonlin(parmset=garchparms1) VA1 VBB1 VCC1  
nonlin(parmset=garchparms2) VA2 VBB2 VCC2
nonlin(parmset=garchparms3) VA3 VBB3 VCC3


compute VA1=%decomp(WWW1),VA2=%decomp(WWW2),VA3=%decomp(WWW3)
frml Gf1 = VA1*tr(VA1)+VCC1*G1{1}*tr(VCC1)+VBB1*uu1{1}*tr(VBB1)
frml Gf2 = VA2*tr(VA2)+VCC2*G2{1}*tr(VCC2)+VBB2*uu2{1}*tr(VBB2)
frml Gf3 = VA3*tr(VA3)+VCC3*G3{1}*tr(VCC3)+VBB3*uu3{1}*tr(VBB3)
set LOGLL = logl1 + logl2 + logl3


maximize(trace,parmset=meanparms+garchparms,pmethod=simplex,piters=10,method=bfgs,iters=400) LOGLL * gend

TD5_final.xls
(379 KiB) Downloaded 1452 times
TD5_switch.txt
(5.84 KiB) Downloaded 1792 times

Re: regime-switch VAR-GARCH-M

Posted: Wed Mar 30, 2011 11:12 am
by TomDoan
I think you want this to be defining a FRML, not a SERIES:

set LOGLL = logl1 + logl2 + logl3

that is, you want

frml LOGLL = logl1 + logl2 + logl3

You also can't do * on the MAXIMIZE:

maximize(trace,parmset=meanparms+garchparms1+garchparms2+garchparms3,pmethod=simplex,piters=10,method=bfgs,iters=400) LOGLL * gend

You have to figure out the first computable entry yourself since the formulas are recursive.

Re: regime-switch VAR-GARCH-M

Posted: Sun Apr 17, 2011 3:08 pm
by huanpipt
Thanks Tom.
I have another question.
After I run these code, how could I get the estimated number of conditional variance?
thanks

Re: regime-switch VAR-GARCH-M

Posted: Mon Apr 18, 2011 5:15 pm
by TomDoan
I'm not sure I'm seeing where your model is regime-switching. You're doing guess values based upon sample splits, but there isn't anything in the formulas for the variances which will cause different calculations under different conditions.

Re: regime-switch VAR-GARCH-M

Posted: Mon Apr 18, 2011 7:59 pm
by huanpipt
Sorry, I don't express clearly.

I want to obtain the conditional variance of the GARCH estimator, in simply Ht.

And I think the regime-switch is equivalent to threshold model ?

Re: regime-switch VAR-GARCH-M

Posted: Tue Apr 19, 2011 7:40 pm
by TomDoan
huanpipt wrote:Sorry, I don't express clearly.

I want to obtain the conditional variance of the GARCH estimator, in simply Ht.

And I think the regime-switch is equivalent to threshold model ?
The threshold model is one form of regime-switching, but there's nothing in your formula which causes the variances to switch. What is it that you're trying to do?

Re: regime-switch VAR-GARCH-M

Posted: Tue Apr 19, 2011 9:15 pm
by huanpipt
Thanks for your reminding.

I revise the last part of my code as following

Code: Select all

*
* These are used to initialize pre-sample variances.
*
gset G1  * gend = WWW1
gset G2  * gend = WWW2
gset G3  * gend = WWW3
gset uu1 * gend = WWW1
gset uu2 * gend = WWW2
gset uu3 * gend = WWW3
set u1(1) = 0.0
set u2(1) = 0.0
set u3(1) = 0.0
set u1(2) = 0.0
set u2(2) = 0.0
set u3(2) = 0.0
*
declare frml[symm] Gf1
*
frml logl1 = $
    Gx1    = Gf1(t) , $
    G1(t)  = Gx1, $
    ux1    = R1 , $
    uu1(t) = %outerxx(ux1), $
    %pt(u1,t,ux1),$
    %logdensity(Gx1,ux1)

*
* This does a simple GARCH(1,1) model for the variance.
*
dec symm VA1(n,n) VA2(n,n) VA3(n,n) VB1(n,n) VB2(n,n) VB3(n,n) VC1(n,n) VC2(n,n) VC3(n,n)

compute VA1=WWW1,VA2=WWW2,VA3=WWW3,VB1=%const(0.50),VB2=%const(0.50),$
VB3=%const(0.50),VC1=%const(0.05),VC2=%const(0.05),VC3=%const(0.05)


nonlin(parmset=garchparms) VA1 VB1 VC1 VA2 VB2 VC2 VA3 VB3 VC3

frml Gf1 = VA1+VC1.*G1{1}+VB1.*uu1{1}+VA2*La1+VC2*La1*G2{1}+VB2*La1*uu2{1}+VA3*La2+VC3*La2*G3{1}+VB3*La2*uu3{1}


maximize(trace,parmset=meanparms+garchparms,pmethod=simplex,$
piters=10,method=bhhh,iters=400) logl1 gstart+2 gend
*
* This does a BEKK model for the variance
*
dec rect VBB1(n,n) VBB2(n,n) VBB3(n,n) VCC1(n,n) VCC2(n,n) VCC3(n,n)
compute VBB1=.05*%identity(n),VBB2=.05*%identity(n),VBB3=.05*%identity(n),$
VCC1=.50*%identity(n),VCC2=.50*%identity(n),VCC3=.50*%identity(n)


nonlin(parmset=garchparms) VA1 VBB1 VCC1 VA2 VBB2 VCC2 VA3 VBB3 VCC3


compute VA1=%decomp(WWW1),VA2=%decomp(WWW2),VA3=%decomp(WWW3)
frml Gf1 = VA1*tr(VA1)+VCC1*G1{1}*tr(VCC1)+VBB1*uu1{1}*tr(VBB1)+VA2*La1*tr(VA2)+VCC2*La1*G2{1}*tr(VCC2)+VBB2*La1*uu2{1}*tr(VBB2)+VA3*La2*tr(VA3)+VCC3*La2*G3{1}*tr(VCC3)+VBB3*La2*uu3{1}*tr(VBB3)

maximize(trace,parmset=meanparms+garchparms,pmethod=simplex,piters=10,$
method=bhhh,iters=400) logl1 gstart+2 gend

But there is error "## MAT2. Matrices with Dimensions 2 x 2 and 1 x 1 Involved in * Operation"

And I trying to get the real number of G from time 0 to time t for forecasting the future estimated value of dependent variable.

Re: regime-switch VAR-GARCH-M

Posted: Tue Apr 26, 2011 12:23 pm
by huanpipt
Sorry, I have a problem again.

I want to get the R-square value of this model.

How could I obtain it?

Thanks