Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NA/NaN function evaluation warning message in R (Rstudio) console for nlminb

I want to obtain an estimate for the Value-at-Risk at level 99% using the Analytic_t method (based on Students-t distribution).

I have the code:

load("stocks.Rdata")
plot(S,main="stock prices")
loss.returns <- -diff(log(S)) ## loss returns
plot(loss.returns,main="loss returns")

Analytic_t <- function(L,RM,p){
  input_check(RM,p)
  ## estimation of model parameters
  require(fGarch)
  std_par <- stdFit(L)$par
  nu <- std_par[3]
  mu <- std_par[1]
  s <- std_par[2] * sqrt((nu-2)/nu)
  ## calculation of risk measure 
  q <- qt(p,df=nu)
  if (RM=="VaR") return(mu + s * q)
  if (RM=="ES") return(mu + s * dt(q,df=nu) * (nu+q^2)/(nu-1)/(1-p)) 
}

if (T){
  set.seed(123)
  i <- sample(1:5,1)  
  L <- loss.returns[,i]
}
plot(L)

VaR_t <- Analytic_t(L, "VaR",0.99)

However, when I run

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

VaR_t <- Analytic_t(L, "VaR",0.99)

I get the following warning:

Warning messages:
1: In nlminb(start = start, objective = loglik, lower = c(-Inf, 0, :
NA/NaN function evaluation
2: In nlminb(start = start, objective = loglik, lower = c(-Inf, 0, :
NA/NaN function evaluation

I can still obtain a value if I type VaR_t into the console, however I would like to get rid of the warning. There is no missing data in L, so what could be the reason for this warning and how could I fix it?
Thank you!

>Solution :

Something in your code (probably in stdFit()) calls the nlminb() function, which does numerical optimization. It is common for optimizers to propose parameter values that don’t make sense, and then the function they are optimizing returns NA or NaN. In your case, it’s just a warning, so the functions mainly are working. You’d get an error if it always returned those bad values.

The best fix for this is to track it down and make sure your function never returns those values. That’s often difficult, and in your case even more so, because it appears you didn’t write the function being minimized, it was written by the author of stdFit().

You can suppress warning messages by making the call as

VaR_t <- suppressWarnings(Analytic_t(L, "VaR",0.99))

but that is often a bad idea, because sometimes warnings tell you something useful. My recommendation would be to allow the warning to show, but ignore it unless it says something other than the nlminb() message. (It is possible to suppress just that one warning, but R doesn’t make it easy, so I wouldn’t bother trying.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading