Page 1 of 1

Variations on BEKK

Posted: Wed Dec 02, 2009 2:53 pm
by TomDoan
The code below provides modernized calculations for standard, diagonal and triangular BEKK models. The standard BEKK, can, of course, be done with GARCH directly. The code using MAXIMIZE can be used as the base if you want some additional features that aren't currently included. This uses the same data set as the garchmv.prg example.


Note that MV=TBEKK (for triangular BEKK) and MV=DBEKK (for diagonal BEKK) were added to the GARCH instruction with RATS 8.3. Also, the SIGNS option was added to allow for different sign effects on asymmetry.

Code: Select all

*
* Variations on BEKK models
*
all 6237
open data g10xrate.xls
data(format=xls,org=columns) / usxjpn usxfra usxsui
*
set xjpn = 100.0*log(usxjpn/usxjpn{1})
set xfra = 100.0*log(usxfra/usxfra{1})
set xsui = 100.0*log(usxsui/usxsui{1})
*
* Standard BEKK with GARCH instruction
*
compute gstart=3,gend=6237
garch(p=1,q=1,mv=bekk,pmethod=simplex,piters=5) gstart gend xjpn xfra xsui
*
* General setup for GARCH with MAXIMIZE
*
compute n=3
*
dec series[vect] yv
dec frml[vect]   residv
dec vect[series] u(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] h uu
*
* ux is used when extracting a u vector
*
declare symm hx(n,n)
declare vect ux(n)
declare frml[symm] hf
*
* Mean parameters
*
dec vect mu(n)
nonlin(parmset=meanparms) mu
*
* Define residuals
*
frml residv = yv-mu
*
* Define vector of dependent variables
*
gset yv = ||xjpn,xfra,xsui||
*
* Run preliminary regression to get estimates of residuals and means.
*
nlsystem(frmlvector=residv,parmset=meanparms,resids=u) gstart gend
compute rr=%sigma
*
* These are used to initialize pre-sample variances.
*
gset h  * gend = rr
gset uu * gend = rr
*
frml logl = $
    hx    = h(t) = hf(t) , $
    ux    = residv       , $
    uu(t) = %outerxx(ux) , $
    %pt(u,t,ux)          , $
    %logdensity(hx,ux)
******************************************************************
*
* Standard BEKK. Should be the same as above (just much slower)
*
dec rect 	var(n,n) vbr(n,n)
dec packed	vct(n,n)
dec symm    vcs
*
compute var=sqrt(.05)*%identity(n),vbr=sqrt(.45)*%identity(n)
compute vct=%decomp(rr)
nonlin(parmset=bekkparms) vct var vbr
FUNCTION %%BEKKInit
compute vcs=%ltouterxx(vct)
END
frml hf = vcs+%mqform(h{1},vbr)+%mqform(uu{1},var)
maximize(title="Standard BEKK Model",start=%%BEKKInit(),parmset=meanparms+bekkparms,$
  method=bfgs,iters=400,pmethod=simplex,piters=5) logl gstart gend
******************************************************************
*
* Diagonal BEKK
*
dec vect   vav(n) vbv(n)
dec packed vct(n,n)
dec symm   vcs vbs vas
*
compute vav=%fill(n,1,sqrt(.05)),vbv=%fill(n,1,sqrt(.45))
compute vct=%decomp(rr)
nonlin(parmset=dbekkparms) vct vav vbv
FUNCTION %%DBEKKInit
compute vcs=%ltouterxx(vct)
compute vbs=%outerxx(vbv)
compute vas=%outerxx(vav)
END
*
frml hf = vcs+vbs.*h{1}+vas.*uu{1}
maximize(title="Diagonal BEKK Model",start=%%DBEKKInit(),parmset=meanparms+dbekkparms,$
 method=bfgs,iters=400,pmethod=simplex,piters=5) logl gstart gend
******************************************************************
*
* Triangular BEKK. This is written assuming the "causality" flows from
* the first to the last series, which means that the A' matrix in A'HA
* needs to be lower triangular. Because of the convention used in
* defining "packed" matrices, the output will show A and B as lower
* triangular, so this is actually doing AHA' rather than A'HA.
*
dec packed vat(n,n) vbt(n,n)
dec packed vct(n,n)
dec rect   var(n,n) vbr(n,n)
compute vat=sqrt(.05)*%identity(n),vbt=sqrt(.45)*%identity(n)
compute vct=%decomp(rr)
nonlin(parmset=tbekkparms) vct vat vbt
FUNCTION %%TBEKKInit
local integer i j
compute vcs=%ltouterxx(vct)
ewise vbr(i,j)=%if(j>=i,vbt(i,j),0.0)
ewise var(i,j)=%if(j>=i,vat(i,j),0.0)
END
frml hf = vcs+%mqform(h{1},vbr)+%mqform(uu{1},var)
maximize(title="Triangular BEKK",start=%%TBEKKInit(),parmset=meanparms+tbekkparms,$
  method=bfgs,iters=400,pmethod=simplex,piters=5) logl gstart gend

Re: Variations on BEKK

Posted: Fri Apr 01, 2011 4:16 pm
by Alepruz
Dear Tom,
Is it possible to extend the BEKK Triangular model to include assymetry?
Doing either one with the code you provided is easy, but together I haven't been able to figure it out.

Re: Variations on BEKK

Posted: Sat Apr 02, 2011 8:26 am
by TomDoan
Andres wrote:Dear Tom,
Is it possible to extend the BEKK Triangular model to include assymetry?
Doing either one with the code you provided is easy, but together I haven't been able to figure it out.
Yes, but, because of the forced p.s.d. nature of the calculation, the asymmetry in a BEKK gives you a (fundamentally) different model depending upon which sign you choose to use in the calculation. This is unlike the situation with most other asymmetric GARCH models where the coefficient sign can be negative to counteract a different choice for + or - shocks. This uses a function called VTSign to pick a particular signed version of the residual vector. The SIGNS vector for this is the set of + or -1's which will be the type of residual which is assumed to increase variance; in this case, all positive.

This can be done more directly with RATS 8.3 or later using GARCH with the MV=TBEKK and SIGNS options

Code: Select all

*
* Asymmetric triangular BEKK. This is written assuming the "causality"
* flows from the first to the last series, which means that the A'
* matrix in A'HA needs to be lower triangular. Because of the convention
* used in defining "packed" matrices, the output will show A and B as
* lower triangular, so this is actually doing AHA' rather than A'HA.
*
function VTSign v s
type vector v s VTSign
*
local integer i
*
dim VTSign(%rows(v))
ewise VTSign(i)=%if(%sign(v(i))==s(i),v(i),0.0)
end
*
compute signs=||1.0,1.0,1.0||
declare series[symm] uus
gset uus * gend = %zeros(n,n)
*
frml alogl = $
    hx    = h(t) = hf(t) , $
    ux    = residv       , $
    uu(t) = %outerxx(ux) , $
    uus(t)= %outerxx(VTSign(ux,signs)), $
    %pt(u,t,ux)          , $
    %logdensity(hx,ux)
dec packed vat(n,n) vbt(n,n) vdt(n,n)
dec packed vct(n,n)
dec rect   var(n,n) vbr(n,n) vdr(n,n)
compute vat=sqrt(.05)*%identity(n),vbt=sqrt(.85)*%identity(n),vdt=%zeros(n,n)
compute vct=%decomp(rr)
nonlin(parmset=atbekkparms) vct vat vbt vdt
FUNCTION %%ATBEKKInit
local integer i j
compute vcs=%ltouterxx(vct)
ewise vbr(i,j)=%if(j>=i,vbt(i,j),0.0)
ewise var(i,j)=%if(j>=i,vat(i,j),0.0)
ewise vdr(i,j)=%if(j>=i,vdt(i,j),0.0)
END
frml hf = vcs+%mqform(h{1},vbr)+%mqform(uu{1},var)+%mqform(uus{1},vdr)
maximize(title="Asymmetric Triangular BEKK",start=%%ATBEKKInit(),parmset=meanparms+atbekkparms,$
  method=bfgs,iters=400,trace,pmethod=simplex,piters=5) alogl gstart gend

Re: Variations on BEKK

Posted: Mon Apr 04, 2011 1:28 am
by Alepruz
Dear Tom,

Thanks for your quick reply.

I have two further questions.
When we add the asymmetric option to the garch function for the full bekk. i.e.
garch(p=1,q=1,mv=bekk,asymmetric,...,pmethod=simplex,piters=5,...) / xjpn, xfra (or any other variable)

Then which version are we getting the positive or the negative sign?

In finance, generally bear markets tend to exhibit more return volatility. In your previous code how may I include the negative sign?

Thank you very much

Andres

Re: Variations on BEKK

Posted: Mon Apr 04, 2011 11:42 am
by TomDoan
Andres wrote:Dear Tom,

Thanks for your quick reply.

I have two further questions.
When we add the asymmetric option to the garch function for the full bekk. i.e.
garch(p=1,q=1,mv=bekk,asymmetric,...,pmethod=simplex,piters=5,...) / xjpn, xfra (or any other variable)

Then which version are we getting the positive or the negative sign?

In finance, generally bear markets tend to exhibit more return volatility. In your previous code how may I include the negative sign?

Thank you very much

Andres
I believe that's explained in the comments in that last post. If negative shocks are the ones which are assumed to increase the variance, you would want:

compute signs=||-1.0,-1.0,-1.0||

rather than

compute signs=||1.0,1.0,1.0||

Re: Variations on BEKK

Posted: Tue Feb 18, 2014 10:01 am
by edysbe76
TomDoan wrote:
Andres wrote:Dear Tom,
Is it possible to extend the BEKK Triangular model to include assymetry?
Doing either one with the code you provided is easy, but together I haven't been able to figure it out.
Yes, but, because of the forced p.s.d. nature of the calculation, the asymmetry in a BEKK gives you a (fundamentally) different model depending upon which sign you choose to use in the calculation. This is unlike the situation with most other asymmetric GARCH models where the coefficient sign can be negative to counteract a different choice for + or - shocks. This uses a function called VTSign to pick a particular signed version of the residual vector. The SIGNS vector for this is the set of + or -1's which will be the type of residual which is assumed to increase variance; in this case, all positive.

Code: Select all

*
* Asymmetric triangular BEKK. This is written assuming the "causality"
* flows from the first to the last series, which means that the A'
* matrix in A'HA needs to be lower triangular. Because of the convention
* used in defining "packed" matrices, the output will show A and B as
* lower triangular, so this is actually doing AHA' rather than A'HA.
*
function VTSign v s
type vector v s VTSign
*
local integer i
*
dim VTSign(%rows(v))
ewise VTSign(i)=%if(%sign(v(i))==s(i),v(i),0.0)
end
*
compute signs=||1.0,1.0,1.0||
declare series[symm] uus
gset uus * gend = %zeros(n,n)
*
frml alogl = $
    hx    = h(t) = hf(t) , $
    ux    = residv       , $
    uu(t) = %outerxx(ux) , $
    uus(t)= %outerxx(VTSign(ux,signs)), $
    %pt(u,t,ux)          , $
    %logdensity(hx,ux)
dec packed vat(n,n) vbt(n,n) vdt(n,n)
dec packed vct(n,n)
dec rect   var(n,n) vbr(n,n) vdr(n,n)
compute vat=sqrt(.05)*%identity(n),vbt=sqrt(.85)*%identity(n),vdt=%zeros(n,n)
compute vct=%decomp(rr)
nonlin(parmset=atbekkparms) vct vat vbt vdt
FUNCTION %%ATBEKKInit
local integer i j
compute vcs=%ltouterxx(vct)
ewise vbr(i,j)=%if(j>=i,vbt(i,j),0.0)
ewise var(i,j)=%if(j>=i,vat(i,j),0.0)
ewise vdr(i,j)=%if(j>=i,vdt(i,j),0.0)
END
frml hf = vcs+%mqform(h{1},vbr)+%mqform(uu{1},var)+%mqform(uus{1},vdr)
maximize(title="Asymmetric Triangular BEKK",start=%%ATBEKKInit(),parmset=meanparms+atbekkparms,$
  method=bfgs,iters=400,trace,pmethod=simplex,piters=5) alogl gstart gend

Dear Tom,

I'm new in this forum and much interested in Multivariate GARCH analysis. I currently doing my research thesis. I saw your post (above) regarding the asymmetric BEKK for the case of three variables. I have two following questions:

1. Is there any differences of the computed sign structure (e.g. compute signs=||-1.0,-1.0,-1.0||) between three and four (or more) variables? E.g. ||-1.0,-1.0,-1.0,-1.0|| for the case four variables (if it is true)?

2. Regarding your post on Asymmetric Triangular BEKK (above), how to write the code for diagnostic testing? E.g. Multivariat Q (LB) and Multivariate Q Square (LB Square or McLeod-Li)? I have been trying to figure out how to write the code but still failed. I also refer to Tsay code (for the bi-variate case) but still not clear since the way you write the code prior to diagnostic testing is a little bit different from Tsay's code. Could you please show me how to write the code for diagnostic testing in the case of four variables? Thank you in advance.

Re: Variations on BEKK

Posted: Tue Feb 18, 2014 11:51 am
by TomDoan
edysbe76 wrote:Dear Tom,
I'm new in this forum and much interested in Multivariate GARCH analysis. I currently doing my research thesis. I saw your post (above) regarding the asymmetric BEKK for the case of three variables. I have two following questions:

1. Is there any differences of the computed sign structure (e.g. compute signs=||-1.0,-1.0,-1.0||) between three and four (or more) variables? E.g. ||-1.0,-1.0,-1.0,-1.0|| for the case four variables (if it is true)?
No. You just need to match it to the number of variables.

Note that the original thread was four years old, and there have been some changes to the GARCH instruction since then to implement directly the models handled above. I just edited the old responses, so you might want to re-read them.
edysbe76 wrote: 2. Regarding your post on Asymmetric Triangular BEKK (above), how to write the code for diagnostic testing? E.g. Multivariat Q (LB) and Multivariate Q Square (LB Square or McLeod-Li)? I have been trying to figure out how to write the code but still failed. I also refer to Tsay code (for the bi-variate case) but still not clear since the way you write the code prior to diagnostic testing is a little bit different from Tsay's code. Could you please show me how to write the code for diagnostic testing in the case of four variables? Thank you in advance.
I'm not sure Tsay ever does diagnostics based upon a full standardization of the residuals---instead he does a set of univariate diagnostics. The multivariate diagnostics as described on page UG-303 of the RATS v8 User's Guide get computed exactly the same way regardless of the size of the model.

Re: Variations on BEKK

Posted: Tue Feb 18, 2014 8:10 pm
by edysbe76
Dear Tom,

Thank you for quick reply. Unfortunately I don't have RATS version 8.3. Yes you are right. Tsay does a set of univariate diagnostics and also multivariate diagnostics for the case of time-varying correlation models (tsayp524.rpf) but not full BEKK. Anyway, I refer to RATS v8 User's Guide for multivariate diagnostic testing as following:

dec vect[series] zu(%nvar)
do time=%regstart(),%regend()
compute %pt(zu,time,%solve(%decomp(hh(time)),rd(time)))
end do time
@mvqstat(lags=40)
# zu

My questions:

1. Refer to your original code for Asymmetric Triangular BEKK, what code should I use to replace the hh(time) and rd(time) since both are not recognizable by previous code? The error is as below:

dec vect[series] zu(%nvar)
do time=%regstart(),%regend()
compute %pt(zu,time,%solve(%decomp(hh(time)),rd(time)))

## SX11. Identifier HH is Not Recognizable. Incorrect Option Field or Parameter Order?
>>>>,%solve(%decomp(hh(<<<<

end do time
@mvqstat(lags=40)
# zu

2. Is it necessary to test for univariate diagnostic?

Thank you in advance.

Re: Variations on BEKK

Posted: Wed Feb 19, 2014 8:55 am
by TomDoan
edysbe76 wrote:Dear Tom,

Thank you for quick reply. Unfortunately I don't have RATS version 8.3. Yes you are right. Tsay does a set of univariate diagnostics and also multivariate diagnostics for the case of time-varying correlation models (tsayp524.rpf) but not full BEKK. Anyway, I refer to RATS v8 User's Guide for multivariate diagnostic testing as following:

dec vect[series] zu(%nvar)
do time=%regstart(),%regend()
compute %pt(zu,time,%solve(%decomp(hh(time)),rd(time)))
end do time
@mvqstat(lags=40)
# zu

My questions:

1. Refer to your original code for Asymmetric Triangular BEKK, what code should I use to replace the hh(time) and rd(time) since both are not recognizable by previous code? The error is as below:

dec vect[series] zu(%nvar)
do time=%regstart(),%regend()
compute %pt(zu,time,%solve(%decomp(hh(time)),rd(time)))

## SX11. Identifier HH is Not Recognizable. Incorrect Option Field or Parameter Order?
>>>>,%solve(%decomp(hh(<<<<

end do time
@mvqstat(lags=40)
# zu
For the way this is coded, you would use

compute %pt(zu,time,%solve(%decomp(h(time)),%xt(u,time)))
edysbe76 wrote: 2. Is it necessary to test for univariate diagnostic?
Not really, but it's common to do both. Better to do them and delete them if unnecessary, than have to go back if a referee insists.

Re: Variations on BEKK

Posted: Wed Feb 19, 2014 10:14 am
by edysbe76
Dear Tom,

Sorry to bother you again. Please help me with the following problem regarding the multivariate diagnostic test: ##MAT5

dec vect[series] zu(%nvar)
do time=%regstart(),%regend()
compute %pt(zu,time,%solve(%decomp(h(time)),%xt(u,time)))
end do time

## MAT5. Needed Matrix with Dimensions 0 x 1, Got 4 x 1 Instead
The Error Occurred At Location 36, Line 2 of loop/block

@mvqstat(lags=12)
# zu


And for univariate diagnostic (I got it from the RATS v8 User's Guide), could you give the correct code for the following test:

set resid1 = rd(t)(1) /sqrt(hh(t)(1,1))
set resid2 = rd(t)(2) /sqrt(hh(t)(2,2))
set resid3 = rd(t)(3) /sqrt(hh(t)(3,3))
set resid4 = rd(t)(4) /sqrt(hh(t)(4,4))

@bdindtests(number=12) resid1
@bdindtests(number=12) resid2
@bdindtests(number=12) resid3
@bdindtests(number=12) resid4



For your reference, below is the code that I used to estimate the asymmetric triangular BEKK with four variables:

Code: Select all

compute gstart=1995:05:04,gend=2012:12:06

compute n=4

dec series[vect] yv
dec frml[vect]   residv
dec vect[series] u(n)
declare series[symm] h uu
declare symm hx(n,n)
declare vect ux(n)
declare frml[symm] hf
dec vect mu(n)

nonlin(parmset=meanparms) mu

frml residv = yv-mu

gset yv = ||rmyr,rbrd,rcbr,rvtd||

nlsystem(frmlvector=residv,parmset=meanparms,resids=u) gstart gend

compute rr=%sigma
gset h  * gend = rr
gset uu * gend = rr

function VTSign v s
type vector v s VTSign

local integer i

dim VTSign(%rows(v))
ewise VTSign(i)=%if(%sign(v(i))==s(i),v(i),0.0)
end

compute signs=||-1.0,-1.0,-1.0,-1.0||
declare series[symm] uus
gset uus * gend = %zeros(n,n)

frml alogl = $
    hx    = h(t) = hf(t) , $
    ux    = residv       , $
    uu(t) = %outerxx(ux) , $
    uus(t)= %outerxx(VTSign(ux,signs)), $
    %pt(u,t,ux)          , $
    %logdensity(hx,ux)
dec packed vat(n,n) vbt(n,n) vdt(n,n)
dec packed vct(n,n)
dec rect   var(n,n) vbr(n,n) vdr(n,n)
compute vat=sqrt(.05)*%identity(n),vbt=sqrt(.85)*%identity(n),vdt=%zeros(n,n)
compute vct=%decomp(rr)
nonlin(parmset=atbekkparms) vct vat vbt vdt
FUNCTION %%ATBEKKInit
local integer i j
compute vcs=%ltouterxx(vct)
ewise vbr(i,j)=%if(j>=i,vbt(i,j),0.0)
ewise var(i,j)=%if(j>=i,vat(i,j),0.0)
ewise vdr(i,j)=%if(j>=i,vdt(i,j),0.0)
END
frml hf = vcs+%mqform(h{1},vbr)+%mqform(uu{1},var)+%mqform(uus{1},vdr)

maximize(title="Asymmetric Triangular BEKK",start=%%ATBEKKInit(),parmset=meanparms+atbekkparms,$
  method=bfgs,robust,iters=400,trace,pmethod=simplex,piters=5) alogl gstart gend
Thanks again.

Re: Variations on BEKK

Posted: Wed Feb 19, 2014 3:18 pm
by TomDoan
Since you're not using GARCH, the %NVAR variable (which dimensions the ZU VECTOR) is never set. Just add compute %nvar=4 before you start doing the diagnostics.

Re: Variations on BEKK

Posted: Wed Feb 19, 2014 8:00 pm
by edysbe76
TomDoan wrote:Since you're not using GARCH, the %NVAR variable (which dimensions the ZU VECTOR) is never set. Just add compute %nvar=4 before you start doing the diagnostics.

Dear Tom,

Thank you very much. I also try to estimate the Asymmetric Standard BEKK (Full BEKK) using GARCH instruction and also MAXIMIZE instead of ATBEKK but I experienced with convergence problem. I really need you help here. Below is the code that I slightly modified from your Standard BEKK's code (If it is ture) using MAXIMIZE and GARCH instruction.

Code: Select all

compute gstart=1995:05:04,gend=2012:12:06

compute n=4

dec series[vect] yv
dec frml[vect]   residv
dec vect[series] u(n)
declare series[symm] h uu
declare symm hx(n,n)
declare vect ux(n)
declare frml[symm] hf
dec vect mu(n)

nonlin(parmset=meanparms) mu

frml residv = yv-mu

gset yv = ||rmyr,rbrd,rcbr,rvtd||

nlsystem(frmlvector=residv,parmset=meanparms,resids=u) gstart gend

compute rr=%sigma
gset h  * gend = rr
gset uu * gend = rr

function VTSign v s
type vector v s VTSign

local integer i

dim VTSign(%rows(v))
ewise VTSign(i)=%if(%sign(v(i))==s(i),v(i),0.0)
end

compute signs=||-1.0,-1.0,-1.0,-1.0||
declare series[symm] uus
gset uus * gend = %zeros(n,n)

frml alogl = $
    hx    = h(t) = hf(t) , $
    ux    = residv       , $
    uu(t) = %outerxx(ux) , $
    uus(t)= %outerxx(VTSign(ux,signs)), $
    %pt(u,t,ux)          , $
    %logdensity(hx,ux)
dec packed vct(n,n)
dec rect   var(n,n) vbr(n,n) vdr(n,n)
dec symm vcs
compute var=sqrt(.05)*%identity(n),vbr=sqrt(.85)*%identity(n),vdr=%zeros(n,n)
compute vct=%decomp(rr)
nonlin(parmset=abekkparms) vct var vbr vdr
FUNCTION %%ABEKKInit
compute vcs=%ltouterxx(vct)
END
frml hf = vcs+%mqform(h{1},vbr)+%mqform(uu{1},var)+%mqform(uus{1},vdr)


maximize(title="Asymmetric BEKK",start=%%ABEKKInit(),parmset=meanparms+abekkparms,$
  method=bfgs,iters=500,robust,pmethod=simplex,piters=5) alogl gstart gend
and using GARCH instruction:

Code: Select all

system(model=var2)
variables rmyr rbrd rcbr rvtd
lags 2
det constant
end(system)

GARCH(P=1,Q=1,model=var2,mv=bekk,robust,asymmetric,iters=500,pmethod=simplex,piters=10,$
      hmatrices=hh,rvectors=rv) / rmyr rbrd rcbr rvtd
Thank you in advance.

Re: Variations on BEKK

Posted: Thu Feb 20, 2014 10:43 am
by TomDoan
I can't diagnose a convergence problem without the data and full program. Did you successfully fit a TBEKK without asymmetry?

Re: Variations on BEKK

Posted: Thu Feb 20, 2014 12:31 pm
by edysbe76
TomDoan wrote:I can't diagnose a convergence problem without the data and full program. Did you successfully fit a TBEKK without asymmetry?

Dear Tom,

I got it! The TBEKK with/without asymmetry both are successful. Thank you very much for your help. :D