【发布时间】:2017-04-08 00:31:12
【问题描述】:
我编写了一个函数bs.pp,用于计算看跌期权的 Black Scholes 值。
bs.pp <- function (Price, Strike, sigma, texp, int) {
d1=(1 / (sigma*sqrt(texp)))*(log(Price/Strike)+(int+(sigma^2)/2)*texp)
d2=d1-sigma*sqrt(texp)
Strike*pnorm(-d2)*exp(-int*texp)-Price*pnorm(-d1)}
这似乎运作良好
> bs.pp(1000,1000,.2,1,.02)
[1] 69.35905
> bs.pp(1000,900,.25,1,.02)
[1] 46.15609
当我们知道 (Price, Strike, texp, int) 和函数的结果但不是sigma.
我尝试这样做
gg <- function(Price, Strike, sigma, texp, int, PutPrice){(bs.pp(Price, Strike, sigma, texp, int) - PutPrice)^2}
xmin <- optimize(gg, c(0,1), tol = 0.0001, Price = 1000, Strike = 1000, texp = 1, int = 0.02, PutPrice = 69.4)
xmin$minimum
返回错误
Error in optimize(gg, c(0, 1), tol = 1e-04, Price = 1000, Strike = 1000, :
'xmin' not less than 'xmax'
有趣的是,如果我运行以下命令(注意“int = 0.02”在优化行中仅替换为“0.02”)
gg <- function(Price, Strike, sigma, texp, int, PutPrice){(bs.pp(Price, Strike, sigma, texp, int) - PutPrice)^2}
xmin <- optimize(gg, c(0,1), tol = 0.0001, Price = 1000, Strike = 900, texp = 1, 0.02, PutPrice = 46.2)
xmin$minimum
我答对了
[1] 0.2501474
为了证明这不仅仅是侥幸
gg <- function(Price, Strike, sigma, texp, int, PutPrice){(bs.pp(Price, Strike, sigma, texp, int) - PutPrice)^2}
xmin <- optimize(gg, c(0,1), tol = 0.0001, Price = 1000, Strike = 1000, texp = 1, 0.02, PutPrice = 69.4)
xmin$minimum
也返回正确答案
[1] 0.2001055
有什么想法吗?
我已经尝试移动函数的参数,以便 sigma 是第一个,但这似乎没有任何区别。
仅供参考,我的最终函数将如下所示,我确信可以更优雅地编写它
bs.piv <- function(Price, Strike, texp, intr, PutPrice){
optfunc <- function(P, S, sigma, t, i, PP){(bs.pp(Price, Strike, sigma, texp, intr) - PutPrice)^2}
xmin <- optimize(optfunc, c(0,1), tol = 0.0001, P=Price, S=Strike, t=texp, i=intr, PP=PutPrice)
xmin$minimum}
【问题讨论】:
-
@Alex A 你是怎么做到的?
-
@Alex A 编辑文本,例如 bs.pp 不再显示为“普通文本”
-
用反引号把它括起来`like this`。它是内联代码格式。请参阅here 了解更多信息。
-
@Alex A 谢谢。我实际上看过格式选项,但一定错过了。
标签: r optimization