Need help with my Code for complex GARCH (GJR-GARCH)

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Need help with my Code for complex GARCH (GJR-GARCH)

Lin23
Hello,

i want to estimate a complex GARCH-model (see below).

W stands for the Day of the Week Dummies. r stands for returns of stock market indices. I stands for the GJR-term.
 I need some help with three problems:
1.) implementation of the GJR-term in the variance equation
2.) compute robust covariance matrix (Bollerslev/Wooldbridge,1992) for robust standard errors
3.) extract the residuals amd volatility of my estimation

First of all my GARCH-Code:

garch2<-function(par,x,Di,Mi,Do,Fr,y,z,d){
   x<<-ts(x)
    y<<-ts(y)
    z<<-ts(z)
    Di <<-ts(Di)
    Mi<<-ts(Mi)
    Do<<-ts(Do)
    Fr<<-ts(Fr)

    n<-length(x)
   
    a <-par[1]
    di <- par[2]
    mi <- par[3]
    do <- par[4]
    fr <- par[5]
    b1 <- par[6]
   b2 <- par[7]
    b3 <- par[8]
    b4 <- par[9]
    dum <- par[10]
    alpha0<-par[11]
    alpha<-par[12]
    beta<-par[13]

    res<-array(length(x))
    hh<-array(length(x))
    ll <-numeric(length(x))

    res[1] <- x[1]-a
    for (i in 2:n){
    res[i]<-x[i]-a-di*Di[i]-mi*Mi[i]-do*Do[i]-fr*Fr[i]-b1*y[i]-b2*z[i-1]-b3*x[i-1]-b4*d[i]*x[i-1]   #MEan Equation
    }
    res<-ts(res)
    hh[1]<-var(res)
    for (i in 2:n){
    hh[i]<-(alpha0+alpha*res[i-1]^2+beta*(hh[i-1]-alpha0))*(1+dum*d[i])     #Variance Equation
     ll[i] <- -1/2*log(2*pi*hh[i]) - 1/2*res[i]^2/hh[i]                                   # LogLikelihood
    }
    hh<-ts(hh)
    h<-sqrt(abs(hh))
    ll <- sum(ll[i])
   
     }
 
x <- dat2$r_csi
   
 Mean = mean(x); Var = var(x); S = 1e-6
    param = c(reg$coef, dum = 0, alpha0 = 0.1*Var,alpha = 0.1, beta = 0.8)    # start values
    lowerB = c(a = -10*abs(Mean),di = S-1, mi = S-1, do = S-1, fr = S-1, b1 = S-1, b2 = S-1, b3= S-1, b4= S-1, dum = S-1, alpha0 = S^2, alpha = S, beta = S)
    upperB = c(a = 10*abs(Mean), di = 1-S, mi = 1-S, do = 1-S, fr =1-S, b1 = 1-S, b2 = 1-S,b3 = 1-S, b4 = 1-S, dum = 1-S, alpha0 = 100*Var, alpha = 1-S, beta = 1-S)
 
     fitt<-maxLik(start=param, logLik=garch2,method="BHHH", x=dat2$r_csi,Di=dat2$Di,Mi=dat2$Mi,Do=dat2$Do,Fr=dat2$Fr,y=dat2$r_t,z=dat2$r_sp,d=dat2$f)


Note that optim always breaks down: finite-infinite error message
nlminb and the BFGS and BHHH algorithmus from the maxLik-package work fine. The estimated coefficients are similiar to those of the EVIEWS Estimation. So I guess, they are correct.
Is my Implementation of the  Dummy-Variabel in the VAriance-Equation correct?

I failed to incorporate the GJR-term in the VAriance Equation. I tried to modify the Variance Equation:
     I[1]=0
    for(i in 2:n) {
     I[i] <- if (res[i-1]<0){I[i] = 1}else{if(res[i-1]>=0){I[i] = 0}}
      }
      I<-ts(I)
     
      hh[1]<-alpha0
     for (i in 2:n){
     hh[i]<-alpha0+alpha*res[i-1]^2+beta*(hh[i-1]-alpha0)+gjr*I[i]*res[i-1]^2

The estimation results are different from those that EVIEWS suggested. So I think I did something wrong here.

2.) Compute robust covariance (Bollerslev/Wooldbridge,1992). I need robust standard errors, because the real innovations in my data are not normally distributed. Is there a way to control for this aspect other than the robust covariance from Bollerslev/Wooldbridge?

V = H^(-1) G' G H^(-1),

where V denotes the variance-covariance matrix, H stands for the Hessian and G represents the matrix of contributions to the gradient, the elements of which are defined as

G_{t,i} = derivative of l_{t} w.r.t. zeta_{i},

where l_{t} is the log likelihood of the t-th observation and zeta_{i} is the i-th estimated parameter.

Thats a way to compute the robust covariance matrix. But how to I do this i R??? Only the maxLik-package reports the Hessian-matrix, but not the gradient.
When using nlminb for optimization I dont know how to extract the gradient and hessian.

3.) How can I extract the residuals of my GARCH-model and the volatility(hh)? So that I can plot them or do a Box-test.


I hope someone can help me. That would be awesome. Thanks in advance.
Lin23
Reply | Threaded
Open this post in threaded view
|

Re: Need help with my Code for complex GARCH (GJR-GARCH)

braverock
On Sun, 2011-11-27 at 04:43 -0800, Lin23 wrote:
> i want to estimate a complex GARCH-model (see below).
> http://r.789695.n4.nabble.com/file/n4112387/GJR_Garch.png 

Is there a reason that the GJR GARCH functionality in the fGarch or
rgarch packages doesn't fit your requirements?

Don't you think it might be faster to start with the code in one of
those packages and extend it, rather than starting from scratch?

Regards,

   - Brian

--
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

Re: Need help with my Code for complex GARCH (GJR-GARCH)

braverock
<on-list so others can benefit>

On Sun, 2011-11-27 at 16:02 +0100, linus holtermann wrote:
> Hello,
>
> thats a good idea.

> how can I extract the R source code of GJR in the fGarch or rugarch-package?

Why do you feel that you should start with the source rather than the
package documentation and examples. fGarch supports many GARCH
methodologies, including GJR Garch, and rgarch has extensive
documentation and examples for all of its methods.  Please avail
yourself first of the documentation provided by the package authors.
 
As with any R function, the source code is of course available.
 
You can simply type the name of the function without parameters to see
the parsed function source.  This will omit any comments the author may
have in the original sources.

Both rgarch and fGarch are hosted on R-Forge, so you could also browse
the source code there.

> Do you know how to compute the robust covariance matrix?
> Thanks

The R package 'sandwich contains multiple methods for computing robust
covariance matrices, and there are other packages that contain various
robust covariance methodologies as well.  You'd need to be more specific
about which method you hope to employ, and perhaps search a bit before
asking for help.


If you post a follow up query to the list, please take the time to use a
publicly available data set and make your example code completely
reproducible.  This will aid others who may be inclined to help you
resolve your issue.

Regards,

  - Brian

> -------- Original-Nachricht --------
> > Von: "Brian G. Peterson" <[hidden email]>
> > An: Lin23 <[hidden email]>
> > CC: [hidden email]
> > Betreff: Re: [R-SIG-Finance] Need help with my Code for complex GARCH (GJR-GARCH)
>
> > On Sun, 2011-11-27 at 04:43 -0800, Lin23 wrote:
> > > i want to estimate a complex GARCH-model (see below).
> > > http://r.789695.n4.nabble.com/file/n4112387/GJR_Garch.png 
> >
> > Is there a reason that the GJR GARCH functionality in the fGarch or
> > rgarch packages doesn't fit your requirements?
> >
> > Don't you think it might be faster to start with the code in one of
> > those packages and extend it, rather than starting from scratch?
> >
> > Regards,
> >
> >    - Brian
> >
> > --
> > Brian G. Peterson
> > http://braverock.com/brian/
> > Ph: 773-459-4973
> > IM: bgpbraverock
> >
>

--
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.