nlsLM给出错误:尝试使用零长度变量名称 - Black Scholes Model Fit

时间:2014-12-13 23:04:17

标签: nls levenberg-marquardt

我正在研究我的建模技巧,并且正在参加Black Scholes。我们的想法是使用当前期权价格(x)来估算波动率(b)。

当我运行nlsLM时,我收到此错误:

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

我无法看到错误的位置,我已从公式中删除了所有变量,并将其替换为此特定情况的数值。 x和b仍然是变量。我在没有d1和d2的情况下运行它,换句话说,在每个pnorm()中放置每个长公式,但也没有运气。

x是执行价格,y是2014年12月12日到期12月20日到期的GOOG通话价格。

以下是重现错误的代码。

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)))

谢谢。

1 个答案:

答案 0 :(得分:0)

找到答案...... nlsLM函数中有一行:

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

问题是我公式中提供的开头没有名字,b。我想通过使用list命令来解决问题。

我已将其更正如下:

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