nlsLM giving error: attempt to use zero-length variable name - Black Scholes Model Fit

1.5k views Asked by At

I am working on my modeling skills and am having a go at Black Scholes. The idea is to use the current option prices (x) to get an estimate for volatility (b).

When I run nlsLM, I get this error:

Error in assign(i, data[[i]], envir = env) : 
  attempt to use zero-length variable name

I cannot see where the error is, I have removed all of the variables from the formula and replaced them with their numerical values for this specific case. x and b remain as variables. I have run this without d1 and d2, in other words, placing the long formulas for each inside the respective pnorm() but no luck there either.

x is the strike price, y is the price of the GOOG call expiring 12/20/14 as of 12/12/14.

Here is the code to reproduce the error.

df <- structure(list(x = c(505, 510, 512.5, 515, 517.5, 520, 522.5, 
                           525, 527.5, 530, 532.5), y = c(17.7, 12.5, 12, 9.2, 7.82, 6.3, 
                           5.1, 4.1, 3.21, 2.45, 1.9)), .Names = c("x", "y"), 
                row.names = c(NA, -11L), class = "data.frame")

f <- function(x, b){
  d1 = (1/(b*sqrt(0.021918)))*(log(518.66/x) + (0.0025+b^2/2) * 0.021918)
  d2 = (1/(b*sqrt(0.021918)))*(log(518.66/x) - (0.0025+b^2/2) * 0.021918)
  return (pnorm(d1)*518.66 - pnorm(d2)*x*exp(-0.0025*0.021918))
}

library(minpack.lm)
test <- nlsLM(y~f(x, b), data=df, start=list((b=0.50)))

Thank you.

1

There are 1 answers

0
mks212 On BEST ANSWER

Found the answer...There is a line in the nlsLM function:

 for (i in names(start)) assign(i, start[[i]], envir = startEnv)

The problem was the start provided in my formula did not have a name, b. I thought by using the list command that would resolve the issue.

I have corrected it as follows:

test <- nlsLM(y~f(x, b), data=df, start=c(b=0.50))