1

I'm trying to estimate a GARCH (1,1) model using maximum likelihood with simulated data. This is what I got:

library(fGarch)
set.seed(1)
garch11<-garchSpec(model = list())
x<-garchSim(garch11, n = 1000)
y <- t(x)
r <- y[1, ]

### Calculate Residuals
CalcResiduals <- function(theta, r)
{
  n <- length(r)
  omega<-theta[1]
  alpha11<-theta[2]
  beta11<-theta[3]
  sigma.sqs <- vector(length = n)
  sigma.sqs[1] <- 0.02
  for (i in 1:(n-1)){
    sigma.sqs[i+1] <- omega + alpha11*(r[i]^2) + beta11*sigma.sqs[i]
  }
  return(list(et=r, ht=sigma.sqs))
}

###Calculate the log-likelihood

GarchLogl <- function(theta, r){
  res <- CalcResiduals(theta,r)
  sigma.sqs <- res$ht
  r <- res$et
  return(-sum(dnorm(r[-1], mean = 0, sd = sqrt(sigma.sqs[-1]), log = TRUE)))
}

fit2 <- nlm(GarchLogl, # function call 
            p = rep(1,3), # initial values = 1 for all parameters
            hessian = FALSE, # also return the hessian matrix
            r = r , # data to be used
            iterlim = 500) # maximum iteration

Unfortunately I get the following error message and no results

There were 50 or more warnings (use warnings() to see the first 50) 1: In sqrt(sigma.sqs[-1]) : NaNs produced

2: In nlm(GarchLogl, p = rep(1, 3), hessian = FALSE, data <- r, ... : NA/Inf durch größte positive Zahl ersetzt

Do you have any idea whats wrong with my code? Thanks a lot!

B.Mae
  • 11
  • 3
  • You produce negative numbers in the command `sqrt(sigma.sqs[-1])`. That's what the warning tells you. – J_F Sep 06 '16 at 11:23
  • How can a square root be negative? Also here, sigma.sqs is always be positive du to: "sigma.sqs[i+1] <- omega + alpha11*(r[i]^2) + beta11*sigma.sqs[i]" – B.Mae Sep 06 '16 at 11:51
  • `alpha11` is getting negative. – J_F Sep 06 '16 at 12:02
  • Thanks J_F, but I tried `for (i in 1:(n-1)){ sigma.sqs[i+1] <- omega + alpha11^2*(r[i]^2) + beta11^2*sigma.sqs[i] } return(list(et=r, ht=sigma.sqs))` and still get the same warnings. – B.Mae Sep 06 '16 at 12:11
  • Not, when you use the right syntax for the `nlm`function: `fit2 <- nlm(GarchLogl, p = rep(1,3), theta = r , hessian = FALSE, iterlim = 500)` – J_F Sep 06 '16 at 12:14
  • BTW: `data <- r` as a assignment in a function is not right, use `= ' – J_F Sep 06 '16 at 12:15
  • With `fit2 <- nlm(GarchLogl, p = rep(1,3), theta = r , hessian = FALSE, iterlim = 500)` AND your updated `alpha11^2` it works. – J_F Sep 06 '16 at 12:29
  • shouldn't it be `fit2 <- nlm(GarchLogl, p = rep(1,3), r = r , hessian = FALSE, iterlim = 500)`? – B.Mae Sep 06 '16 at 12:32
  • Which still does not work. Sorry for bothering, I'm new to R and kind of lost. – B.Mae Sep 06 '16 at 12:33
  • see my last comment above. – J_F Sep 06 '16 at 12:35
  • I understand that `data = r` is wrong, but why should I write `theta = r`? `r` is my input data and `theta` the arguments – B.Mae Sep 06 '16 at 12:51

0 Answers0