# [R] NaN in nlminb

Alexandr sherbaa at gmail.com
Wed Feb 29 21:54:43 CET 2012

```Good day everybody!

I use the following:

The function that present weighted maximum likelihood:
garchLLH<-
function(parm) {
mu = parm[1]; omega = parm[2]; alpha = parm[3]; beta = parm[4]
z = (x-mu); Mean = mean(z^2)
# Use Filter Representation:
e = omega + alpha * c(Mean, z[-length(x)]^2)
h = filter(e, beta, "r", init = Mean)
hh = sqrt(abs(h))
llh = -sum(y*log(garchDist(z, hh)))
llh }

Where,
y<-rep(0,length(x))
len<-length(x)
for (i in 1:len) {
y[i]<-0.5^(len-i+1)}}
Mean = mean(x); Var = var(x); S = 1e-6
params = c(mu = Mean, omega = 0.1*Var, alpha = 0.1, beta = 0.8)
lowerBounds = c(mu = -10*abs(Mean), omega = S^2, alpha = S, beta = S)
upperBounds = c(mu = 10*abs(Mean), omega = 100*Var, alpha = 1-2*S, beta =
1-2*S)
garchDist = function(z, hh) { dnorm(x = z/hh)/hh }

Also I use scaling in optimization:
parscale = rep(1, length = length(params))
parscale[2] = var(x)
parscale[1] = abs(mean(x))

And finally optoimization function:
fit = nlminb(start = params, objective = garchLLH, lower = lowerBounds,
upper = upperBounds,
scale = 1/parscale, control = list(eval.max = 2000, iter.max = 1500, rel.tol
= 1e-14, x.tol = 1e-14))

For calculating as x I use the lorarithmic yield of the share "Gazprom"
quotes.
The code for yield is following:
logyield<-
function(range1) {
len<-length(range1)
log(range1[2:len]/range1[1:(len-1)],base=exp(1))}

Running the nlminb results the NaN at the 37th step of iteration.