将 GBSVolatility 应用于每一行
我有一个相当简单的问题,但不幸的是无法得到结果: 我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果数据框的一列或多列是字符,则在行上申请数据框会将数字转换为字符。所以简单的解决方法是在 vol 中再次转换:
这并不优雅。
我现在不记得更优雅的方式,可能使用 d*ply 或其他东西......
也许这更优雅:
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:
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:
您不应该使用
$
因为 apply 给出的输入被命名为向量而不是 data.frames。所以这应该有效:You shouldn't use
$
since the inputs given by apply are named vectors not data.frames. So this should work:这是我能做的最好的了。它有点“神奇”,但我认为迄今为止最具可读性的选项?
This is the best I can do. It's a little more "magic" but also I think the most readable option so far?