将 GBSVolatility 应用于每一行

发布于 2024-10-07 10:32:58 字数 735 浏览 4 评论 0原文

我有一个相当简单的问题,但不幸的是无法得到结果: 我想将 GBSVolatility 函数应用于 data.frame 的每一行。

我做了以下操作:

> vol <- function(x) GBSVolatility(x$Price, "c", S = 1000, x$Strike, Time = 1/4, 
r = 0.01, b = 0.02, maxiter = 500)
> foo$iv <- apply(foo, 1, vol)

但这不起作用。有人能告诉我为什么吗?

非常感谢

丹尼

更新: 谢谢您的建议。我的数据框称为 foo ,看起来像这样,

Date Price Strike Name
1.1  100   1200    X
1.1  120   1500    P
etc.

我想创建一个包含隐含波动率的新列。我尝试申请,

vol <- function(x) GBSVolatility(x["Price"], "c", S = 1000, x["Strike"], 
     Time = 1/4,  r = 0.01, b = 0.02, maxiter = 500)
foo$iv <- apply(foo, 1, vol)

但效果并不好。

您还有其他建议吗?谢谢

I have a rather simple question but unfortunately just cannot get to a result:
I would like to apply the GBSVolatility function to each row of my data.frame.

I did the following:

> vol <- function(x) GBSVolatility(x$Price, "c", S = 1000, x$Strike, Time = 1/4, 
r = 0.01, b = 0.02, maxiter = 500)
> foo$iv <- apply(foo, 1, vol)

But this does not work. Can someone tell me why?

Thanks alot

Dani

Update:
Thank you for your suggestion. My data frame is called foo and looks like this

Date Price Strike Name
1.1  100   1200    X
1.1  120   1500    P
etc.

I would like to make a new column with the implied volatility. I tried to apply,

vol <- function(x) GBSVolatility(x["Price"], "c", S = 1000, x["Strike"], 
     Time = 1/4,  r = 0.01, b = 0.02, maxiter = 500)
foo$iv <- apply(foo, 1, vol)

but it didn't work as well.

Do you have any other suggestions? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终难遇 2024-10-14 10:32:58

如果数据框的一列或多列是字符,则在行上申请数据框会将数字转换为字符。所以简单的解决方法是在 vol 中再次转换:

vol <- function(x) GBSVolatility(as.numeric(x["Price"]), "c", S = 1000,    
  as.numeric(x["Strike"]), Time = 1/4, r = 0.01, b = 0.02, maxiter = 500)
apply(foo, 1, vol)

这并不优雅。
我现在不记得更优雅的方式,可能使用 d*ply 或其他东西......

也许这更优雅:

library(plyr)
vol <- function(x) GBSVolatility(x$Price, "c", S = 1000, x$Strike, 
  Time = 1/4, r = 0.01, b = 0.02, maxiter = 500)
foo$iv <- adply(foo, 1, vol)$V1

applying for data frame on row converts numeric to character if one or more column of the data frame is character. So easy workaround is to convert again in vol:

vol <- function(x) GBSVolatility(as.numeric(x["Price"]), "c", S = 1000,    
  as.numeric(x["Strike"]), Time = 1/4, r = 0.01, b = 0.02, maxiter = 500)
apply(foo, 1, vol)

This is not elegant.
I can not recall just now the more elegant way, probably using d*ply or something...

Probably this is more elegant:

library(plyr)
vol <- function(x) GBSVolatility(x$Price, "c", S = 1000, x$Strike, 
  Time = 1/4, r = 0.01, b = 0.02, maxiter = 500)
foo$iv <- adply(foo, 1, vol)$V1
行至春深 2024-10-14 10:32:58

您不应该使用 $ 因为 apply 给出的输入被命名为向量而不是 data.frames。所以这应该有效:

vol <- function(x) GBSVolatility(x["Price"], "c", S = 1000, x["Strike"], 
         Time = 1/4,  r = 0.01, b = 0.02, maxiter = 500)
foo$iv <- apply(foo, 1, vol)

You shouldn't use $ since the inputs given by apply are named vectors not data.frames. So this should work:

vol <- function(x) GBSVolatility(x["Price"], "c", S = 1000, x["Strike"], 
         Time = 1/4,  r = 0.01, b = 0.02, maxiter = 500)
foo$iv <- apply(foo, 1, vol)
韬韬不绝 2024-10-14 10:32:58

这是我能做的最好的了。它有点“神奇”,但我认为迄今为止最具可读性的选项?

foo <- read.table(textConnection("Date Price Strike Name
1.1  100   1200    X
1.1  120   1500    P"),header=TRUE)

foo$iv <- with(d,mapply(GBSVolatility,
              Price,Strike,
              MoreArgs=list(TypeFlag="c",S=1000,
                Time=1/4,r=0.01,b=0.02,maxiter=500)))

This is the best I can do. It's a little more "magic" but also I think the most readable option so far?

foo <- read.table(textConnection("Date Price Strike Name
1.1  100   1200    X
1.1  120   1500    P"),header=TRUE)

foo$iv <- with(d,mapply(GBSVolatility,
              Price,Strike,
              MoreArgs=list(TypeFlag="c",S=1000,
                Time=1/4,r=0.01,b=0.02,maxiter=500)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文