[R-SIG-Finance] simple GARCH model

Chris Urlaub Uniraf at gmail.com
Mon Apr 21 12:18:37 CEST 2014


Hi,

I'm still trying to implement the GARCH MIDAS model of Engle et al (2013) in
R, but I've still many problems. Therefore I wanted to start with
programming a simple GARCH (1,1) model and refine it step by step.
Surprisingly, the simple model already does not run and gives warnings and
I've no idea what's wrong. Maybe someone knows why the code doesn't work and
warnings are produced.
The code is taken from the "We think before we R" blog and only differs from
the original code in the data used. Here it is:

pseudoreturns= rnorm(3000, 0.0004, 0.016) 
data=list(Component1 = pseudoreturns)

# Specifying functions:

CalcResiduals <- function(th, data) {

  th[1] -> mean
  th[2] -> alpha.0
  th[3] -> alpha.1
  th[4] -> beta.1

  
  e <- data$Component1
  
  
  n <- length(y)
  sigma.sqs <- vector(length=n)
  sigma.sqs[1] <- 1
  for(ii in c(1:(n-1))) { ## This loop is where the h_t are calculated
    sigma.sqs[ii + 1] <- (
      alpha.0 +
        alpha.1 * (e[ii] - mean) ^ 2 +
        beta.1 * sigma.sqs[ii])
  }
  
  return(list(et = e, ht = sigma.sqs)) # Returns the list of e_t and h_t
}




GarchLogL <- function(th, data) {
  
  res <- CalcResiduals(th, data) ## Recall our earlier function
CalcResiduals()
  sigma.sqs <- res$ht
  e <- res$et
  
  # Assuming normal density of the errors dnorm() gives the density of
normal dist.
  # this will return the negative of the log likelihood.
  return (-sum(dnorm(e[-1], mean=th[1] , sd=sqrt(sigma.sqs[-1]), log=TRUE)))
}


fit2 <- nlm(GarchLogL, # function call
            p = c(0.1,0.1,0.1,0.8), 
            hessian = TRUE,
            data <- data , 
            iterlim = 500) 



Any help would be appreciated!

Thanks,

Chris



--
View this message in context: http://r.789695.n4.nabble.com/simple-GARCH-model-tp4689179.html
Sent from the Rmetrics mailing list archive at Nabble.com.



More information about the R-SIG-Finance mailing list