[R] nlm return wrong function value - garch fitting
Scotty Nelson
poorboy44 at hotmail.com
Tue Oct 14 21:48:53 CEST 2008
I am using nlm to maximize a likelihood function. When I call the likelihood
function (garchLLH) via nlm however, nlm returns the wrong value of the
function.
When I test the likelihood function manually I get the correct answer. I'm
probably doing something really stupid, maybe someone can point it out for
me.
###############this is the function i am trying to minimize ############
garchLLH <- function(y,xdata, parm) {
len<-length(parm)
yhat<-as.vector(t(parm[1:(len-3)]%*%t(xdata)))
###compute likelihood
omega<-parm[len-2]; alpha<-parm[len-1]; beta<-parm[len]
z<-(y-yhat)^2; Mean=mean(z); #### z is the squared error
h<-vector(length=length(y) )
logLL<-vector(length=length(y) )
h[1] <- omega + alpha*Mean + beta*Mean;
logLL[1]<- 0.5 * ( log (2*pi) + log (h[1] ) + z[1] / h[1] ) #negative of the
LL
for (i in (2:length(y)) ) {
h[i] <- omega + alpha*z[i-1] + beta*h[i-1]
#logLL[i]<- -0.5 * ( log (2*pi) + log (h[i] ) + z[i] / h[i] )
logLL[i]<- -0.5 * ( log (h[i] ) + z[i] / h[i] )
}
llh=-sum(logLL);
llh
}
##########ok if i call this function with my initial parameters, i get the
right likelihood value ####
> parm
[1] -0.01642679 0.17234848 0.22053851 0.00000000
> garchLLH(y, xdata, parm)
[1] -596.1819
#### now when i call nlm and look at the first iteration, it is telling me
that the value of the function is 0.7370451 ########
> fit=nlm(f=garchLLH, y,xdata, p=parm, print.level=2)
iteration = 0
Step:
[1] 0 0 0 0
Parameter:
[1] -0.01642679 0.17234848 0.22053851 0.00000000
Function Value
[1] 0.7370451 <------------- what's up with that???? it should be -596.1819
Gradient:
[1] -0.03006041 0.14911503 0.10740682 -0.04930265
########################################
[[elided Hotmail spam]]
Thanks,
Scotty
--
View this message in context: http://www.nabble.com/nlm-return-wrong-function-value---garch-fitting-tp19980876p19980876.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list