[R] Using nlminb for maximum likelihood estimation
Ben Bolker
bbolker at gmail.com
Tue Dec 7 04:45:40 CET 2010
Joonas - as_trix85 <ast_rix85 <at> hotmail.com> writes:
>
>
> I'm trying to estimate the parameters for GARCH(1,1) process.
> Here's my code:
>
> loglikelihood <-function(theta) {
>
> h=((r[1]-theta[1])^2)
> p=0
>
> for (t in 2:length(r)) {
> h=c(h,theta[2]+theta[3]*((r[t-1]-theta[1])^2)+theta[4]*h[t-1])
> }
It will be more efficient to set aside space for the full
h and p vectors (h <- p <- numeric(length(r))) rather than
building them up one step at a time. Also, I think you can
compute p outside the loop:
-sum(dnorm(r,theta[1],sqrt(h),log=TRUE))
> }
>
>
> Then I use nlminb to minimize the function loglikelihood:
>
> nlminb( c(0.001,0.001,0.001,0.001), loglikelihood, lower=c(0.000001,0,0,0))
>
> This does not produce a good result: the iteration limit
> is reached and increasing it won't help. I have used
> "garchFit" from fGarch-package to estimate the parameters.
> I set garchFit to use nlminb as well, and it
> produces a result within just a few seconds.
> However, for modification purposes, I need to get my own code working.
I don't see a question here, nor a reproducible example ...
can you look to see in more detail what garchFit does? Does it
have a way to generate better starting parameter estimates?
Have you tried turning on tracing to see where your
approach is getting stuck?
More information about the R-help
mailing list