首页 > 解决方案 > 如何在 dplyr 中使用基类

问题描述

我正在尝试使用RQuantLib包来使用函数 EuropeanImpliedVolatility 计算数据框中期权的隐含波动率。该函数返回一个基类并得到错误Expecting a single string value: [type=integer; extent=1]

在 dplyr 中使用基类的正确方法是什么?

library(tidyverse)
library(RQuantLib)

    book <- data.frame(
      type=c("call","call","call","put","put","put"),
      strike=c(27800,27600,27400,24800,24600,24400),
      last=c(240,280,320,310,270,230),
      underlying=rep(26425,6),
      dividendYield=rep(0,6),
      riskFreeRate=rep(0.015,6),
      maturity=rep(39/365,6)
    ) %>% 
      rowwise() %>% 
      mutate(
        impvol= EuropeanOptionImpliedVolatility(type, value=last, underlying,strike,dividendYield,riskFreeRate,maturity,volatility=0.4)
      )

标签: rtidyversebase-class

解决方案


有一些属性会导致问题。我们可以用as.numeric

library(dplyr)
library(RQuantLib)
book %>%
   rowwise %>%
    mutate(impvol = as.numeric(EuropeanOptionImpliedVolatility(type, 
        value=last, underlying, strike,
                dividendYield,riskFreeRate,maturity,volatility = 0.4)))

-输出

# A tibble: 6 x 8
# Rowwise: 
#  type  strike  last underlying dividendYield riskFreeRate maturity impvol
#  <chr>  <dbl> <dbl>      <dbl>         <dbl>        <dbl>    <dbl>  <dbl>
#1 call   27800   240      26425             0        0.015    0.107  0.204
#2 call   27600   280      26425             0        0.015    0.107  0.202
#3 call   27400   320      26425             0        0.015    0.107  0.197
#4 put    24800   310      26425             0        0.015    0.107  0.275
#5 put    24600   270      26425             0        0.015    0.107  0.277
#6 put    24400   230      26425             0        0.015    0.107  0.278

或者包裹在 a 中list,如果我们想保持这样的属性

book %>% 
   rowwise %>% 
   mutate(impvol = list(EuropeanOptionImpliedVolatility(type, 
    value=last, underlying,strike,dividendYield,
            riskFreeRate,maturity,volatility=0.4))) 

-输出

# A tibble: 6 x 8
# Rowwise: 
#  type  strike  last underlying dividendYield riskFreeRate maturity impvol        
#  <chr>  <dbl> <dbl>      <dbl>         <dbl>        <dbl>    <dbl> <list>        
#1 call   27800   240      26425             0        0.015    0.107 <ErpnOpIV [1]>
#2 call   27600   280      26425             0        0.015    0.107 <ErpnOpIV [1]>
#3 call   27400   320      26425             0        0.015    0.107 <ErpnOpIV [1]>
#4 put    24800   310      26425             0        0.015    0.107 <ErpnOpIV [1]>
#5 put    24600   270      26425             0        0.015    0.107 <ErpnOpIV [1]>
#6 put    24400   230      26425             0        0.015    0.107 <ErpnOpIV [1]>
 

或另一种选择pmap

library(purrr)
book %>%
    mutate(impvol = pmap_dbl(., ~ EuropeanOptionImpliedVolatility(..1, value = ..3,
           ..4, ..2, ..5, ..6, ..7, volatility = 0.4)))
#  type strike last underlying dividendYield riskFreeRate  maturity    impvol
#1 call  27800  240      26425             0        0.015 0.1068493 0.2044270
#2 call  27600  280      26425             0        0.015 0.1068493 0.2019167
#3 call  27400  320      26425             0        0.015 0.1068493 0.1973822
#4  put  24800  310      26425             0        0.015 0.1068493 0.2745614
#5  put  24600  270      26425             0        0.015 0.1068493 0.2773029
#6  put  24400  230      26425             0        0.015 0.1068493 0.2780447

数据

book <- data.frame(
      type=c("call","call","call","put","put","put"),
      strike=c(27800,27600,27400,24800,24600,24400),
      last=c(240,280,320,310,270,230),
      underlying=rep(26425,6),
      dividendYield=rep(0,6),
      riskFreeRate=rep(0.015,6),
      maturity=rep(39/365,6)
    ) 

推荐阅读