有关使用fOptions的初学者问题:GBSVolatility

时间:2019-10-06 09:43:04

标签: r

很抱歉给你们带来麻烦,我是编程领域的新手,所以我很难在线上了解大多数发布的解决方案。我尝试搜索问题的答案,但似乎找不到任何答案。

我有一个选项详细信息的数据框。它叫做gcp5。

> str(gcp5)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   90 obs. of  10 variables:
 $ Expiry Date  : POSIXct, format: "2019-12-20" "2019-12-20" ...
 $ Strike       : num  1210 1215 1220 1225 1230 ...
 $ Open Interest: num  34 87 50 52 115 17 99 62 907 36 ...
 $ Underlying   : num  1209 1209 1209 1209 1209 ...
 $ CallOrPut    : chr  "c" "c" "c" "c" ...
 $ Bid          : num  43.2 44.1 41.9 40 34.8 36.1 31.4 29.6 27.9 28.4 ...
 $ Ask          : num  44.1 44.7 42.7 40.6 35.7 36.7 32.2 30.4 28.6 29 ...
 $ value        : num  1484 3863 2115 2096 4054 ...
 $ time         : num  0.205 0.205 0.205 0.205 0.205 ...
 $ price        : num  43.7 44.4 42.3 40.3 35.2 ...

我想添加一个名为vol的新列。我想使用GBSVolatility函数(在库fOptions下)填充此新列。我按照以下方式使用了命令,并得到了一些奇怪的错误消息。我不确定出什么问题。

gcp6 <- mutate(gcp5, vol = GBSVolatility(gcp5$price, gcp5$CallOrPut, 
                                          gcp5$Underlying, gcp5$Strike, gcp5$time, 0.03, 0))

#Error in uniroot(.fGBSVolatility, interval = c(-10, 10), price = price,  : 
#f() values at end points not of opposite sign
#In addition: Warning messages:
# 1: In if (is.na(f.lower)) stop("f.lower = f(lower) is NA") :
# the condition has length > 1 and only the first element will be used
#2: In if (is.na(f.upper)) stop("f.upper = f(upper) is NA") :
#  the condition has length > 1 and only the first element will be used

我了解每个人在生活中都有自己的责任,需要时间来履行它们。谢谢所有花时间尝试帮助回答我的查询的人。非常感谢您的帮助。

另外(有关前5个数据行和后5个数据行的详细信息):

> dput(head(gcp5))                                          
structure(list(`Expiry Date` = structure(c(1576800000, 1576800000, 
1576800000, 1576800000, 1576800000, 1576800000), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), Strike = c(1210, 1215, 1220, 1225, 
1230, 1235), `Open Interest` = c(34, 87, 50, 52, 115, 17), Underlying = c(1209, 
1209, 1209, 1209, 1209, 1209), CallOrPut = c("c", "c", "c", "c", 
"c", "c"), Bid = c(43.2, 44.1, 41.9, 40, 34.8, 36.1), Ask = c(44.1, 
44.7, 42.7, 40.6, 35.7, 36.7), value = c(1484.1, 3862.8, 2115, 
2095.6, 4053.75, 618.8), time = c(0.205479452054795, 0.205479452054795, 
0.205479452054795, 0.205479452054795, 0.205479452054795, 0.205479452054795
), price = c(43.65, 44.4, 42.3, 40.3, 35.25, 36.4)), row.names = c(NA, 
6L), class = c("tbl_df", "tbl", "data.frame"))

> dput(tail(gcp5))                                          
structure(list(`Expiry Date` = structure(c(1576800000, 1576800000, 
1576800000, 1576800000, 1576800000, 1576800000), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), Strike = c(1180, 1185, 1190, 1195, 
1200, 1205), `Open Interest` = c(48, 12, 119, 9, 0, 26), Underlying = c(1209, 
1209, 1209, 1209, 1209, 1209), CallOrPut = c("p", "p", "p", "p", 
"p", "p"), Bid = c(47.9, 59.4, 52.7, 64.3, 72, 69.4), Ask = c(52.4, 
60.1, 56.3, 65.2, 73.5, 70.2), value = c(2407.2, 717, 6485.5, 
582.75, 0, 1814.8), time = c(0.205479452054795, 0.205479452054795, 
0.205479452054795, 0.205479452054795, 0.205479452054795, 0.205479452054795
), price = c(50.15, 59.75, 54.5, 64.75, 72.75, 69.8)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -6L))

2 个答案:

答案 0 :(得分:1)

我使用了另一种方法来获得想要的东西。但是我仍然想知道为什么我的原始方法错误以及如何使用我的原始方法而不是我的新方法。谢谢大家。

新方法如下所示,但不是我的首选方法,因为命名约定非常复杂。

vol <- function(x) GBSVolatility(as.numeric(x["price"]), as.character(x["CallOrPut"]), as.numeric(x["Underlying"]),    
                                 as.numeric(x["Strike"]), as.numeric(x["time"]), r = 0.03, b = 0.00)
gcp6 <- mutate(gcp5, vol = apply(gcp5, 1, vol))

答案 1 :(得分:1)

问题是GBSVolatility希望一次计算一个期权价格的波动率,但是在尝试中,我们将完整的列传递给它无法处理的函数。有多种解决方法。

其中之一是使用rowwise,它将自动以行方式传递参数

library(dplyr)
library(fOptions)
library(purrr)

gcp5 %>%
  rowwise() %>%
  mutate(vol = GBSVolatility(price, CallOrPut, Underlying, Strike, time, 0.03, 0))

但是,逐行有点过时了,我们也可以使用pmap_dbl中的purrr

gcp5 %>%
   mutate(vol = pmap_dbl(list(price, CallOrPut, Underlying, Strike, time), 
                 GBSVolatility, r = 0.03, b = 0))

或者在基数R中,使用mapply

with(gcp5,mapply(GBSVolatility, price, CallOrPut, Underlying, Strike,time, 0.03, 0))
#[1] 0.2031476937 0.2173287575 0.2180045839 0.2187683891 0.2050072991 0.2196630834