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

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

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

###Calculate the log-likelihood

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

fit2 &lt;- 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
</code></pre>

<p>Unfortunately I get the following error message and no results </p>

<blockquote>
  <p>There were 50 or more warnings (use warnings() to see the first 50)
  1: In sqrt(sigma.sqs[-1]) : NaNs produced </p>
  
  <p>2: In nlm(GarchLogl, p = rep(1, 3), hessian = FALSE, data &lt;- r,  ... :
  NA/Inf durch größte positive Zahl ersetzt</p>
</blockquote>

<p>Do you have any idea whats wrong with my code? Thanks a lot!</p>