简体   繁体   中英

Creating a new column in dataframe using row values as variables in a function in R

I have a data frame:

df <- structure(list(LAST = c(3.4, 2.52, 1.82, 1.16, 0.69, 0.36, 4, 
3.21, 2.54, 1.93), CURRENTPRICE = c(464.16, 464.16, 464.16, 464.16, 
464.16, 464.16, 464.16, 464.16, 464.16, 464.16), STRIKEPRICE = c(461, 
462, 463, 464, 465, 466, 461, 462, 463, 464), YEARSTOEXPIRATION = c(0.00273972602739726, 
0.00273972602739726, 0.00273972602739726, 0.00273972602739726, 
0.00273972602739726, 0.00273972602739726, 0.010958904109589, 
0.010958904109589, 0.010958904109589, 0.010958904109589)), row.names = c(NA, 
-10L), class = c("data.table", "data.frame"))

I am trying to create a new column calculating implied volatility for each row using data from each row and using the AmericanOptionImpliedVolatility function from the RQuantLib package:

df$IMPLIEDVOLATILITY <- AmericanOptionImpliedVolatility(type="call", value=df$LAST, underlying=df$CURRENTPRICE, strike=df$STRIKEPRICE, dividendYield=0.00, riskFreeRate=.03, maturity=df$YEARSTOEXPIRATION, volatility=0.2)

I know that the code is wrong and I accordingly get an error message:

"Error in americanOptionImpliedVolatilityEngine(type, value, underlying, : Expecting a single value: [extent=10]."

How do I correctly use the "AmericanOptionImpliedVolatility" function with the row values as variables to create a new column with that resulting value?

df %>%
  rowwise() %>%
  mutate(IMPLIEDVOLATILITY = 
           RQuantLib::AmericanOptionImpliedVolatility(type="call", 
                  value = LAST, 
                  underlying = CURRENTPRICE, 
                  strike = STRIKEPRICE, 
                  dividendYield = 0.00, 
                  riskFreeRate =.03,
                  maturity = YEARSTOEXPIRATION,
                  volatility=0.2))



LAST CURRENTPRICE STRIKEPRICE YEARSTOEXPIRATI~ IMPLIEDVOLATILI~
   <dbl>        <dbl>       <dbl>            <dbl> <AmrcnOIV>      
 1  3.4          464.         461          0.00274 0.12058321      
 2  2.52         464.         462          0.00274 0.11218994      
 3  1.82         464.         463          0.00274 0.11577334      
 4  1.16         464.         464          0.00274 0.10918985      
 5  0.69         464.         465          0.00274 0.10744424      
 6  0.36         464.         466          0.00274 0.10472401      
 7  4            464.         461          0.0110  0.09853768      
 8  3.21         464.         462          0.0110  0.09443249      
 9  2.54         464.         463          0.0110  0.09343687      
10  1.93         464.         464          0.0110  0.09130677  

With base R, you could do:

transform(df, IMPLIEDVOLATILITY = 
           Vectorize(RQuantLib::AmericanOptionImpliedVolatility)(type="call", 
                  value = LAST, 
                  underlying = CURRENTPRICE, 
                  strike = STRIKEPRICE, 
                  dividendYield = 0.00, 
                  riskFreeRate =.03,
                  maturity = YEARSTOEXPIRATION,
                  volatility=0.2))

You can use the following solution. You have to apply AmericanOptionImpliedVolatility function in a rowwise operation since the function is not a vectorize function. pmap function applies a function on a variety of arguments in parallel and if you notice I used ..1 , ..2 and ... to refer to the respective values of each variable in a row containing 4 variables in your data set:

library(RQuantLib)
library(purrr)
library(dplyr)

df %>%
  mutate(IMPLIEDVOLATILITY = pmap(cur_data(), 
                                  ~ AmericanOptionImpliedVolatility(type = "call", 
                                                                    value = ..1, 
                                                                    underlying = ..2,
                                                                    strike = ..3,
                                                                    dividendYield = 0.00,
                                                                    riskFreeRate = 0.03,
                                                                    maturity = ..4, 
                                                                    volatility = 0.2)))

    LAST CURRENTPRICE STRIKEPRICE YEARSTOEXPIRATION IMPLIEDVOLATILITY
 1: 3.40       464.16         461       0.002739726         0.1205832
 2: 2.52       464.16         462       0.002739726         0.1121899
 3: 1.82       464.16         463       0.002739726         0.1157733
 4: 1.16       464.16         464       0.002739726         0.1091899
 5: 0.69       464.16         465       0.002739726         0.1074442
 6: 0.36       464.16         466       0.002739726          0.104724
 7: 4.00       464.16         461       0.010958904        0.09853768
 8: 3.21       464.16         462       0.010958904        0.09443249
 9: 2.54       464.16         463       0.010958904        0.09343687
10: 1.93       464.16         464       0.010958904        0.09130677

You can simply use apply and use the column number to assign the values row-wise:

df$IMPLIEDVOLATILITY <- apply( df, 1, function(x) 
  AmericanOptionImpliedVolatility( type="call", value=x[1], underlying=x[2],
  strike=x[3], dividendYield=0.00, riskFreeRate=.03, maturity=x[4],
  volatility=0.2 ) )

df
   LAST CURRENTPRICE STRIKEPRICE YEARSTOEXPIRATION IMPLIEDVOLATILITY
1  3.40       464.16         461       0.002739726            467.56
2  2.52       464.16         462       0.002739726            466.68
3  1.82       464.16         463       0.002739726            465.98
4  1.16       464.16         464       0.002739726            465.32
5  0.69       464.16         465       0.002739726            464.85
6  0.36       464.16         466       0.002739726            464.52
7  4.00       464.16         461       0.010958904            468.16
8  3.21       464.16         462       0.010958904            467.37
9  2.54       464.16         463       0.010958904            466.70
10 1.93       464.16         464       0.010958904            466.09

Keep in mind that I made up a dummy function so the values are obviously wrong in my example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM