在不同的数据帧块中进行相同的计算

Make same calculation in different chunks of dataframe

所以我有一个这样的数据框:

    head(TNX)
         date strike_price impl_volatility moneyness
1  1996-09-03        65000        0.192926 0.9431225
4  1996-09-03        65000        0.184757 0.9431225
6  1996-09-03        55000        0.190826 0.7980267
7  1996-09-03        60000        0.187024 0.8705746
9  1996-09-03        62500        0.189573 0.9068485
10 1996-09-03        72500        0.209731 1.0519443



tail(TNX)
             date strike_price impl_volatility moneyness
424834 2009-10-30        27500        0.646013 0.8107311
424835 2009-10-30        20000        1.261644 0.5896226
424836 2009-10-30        25000        0.835957 0.7370283
424837 2009-10-30        30000        0.462221 0.8844340
424844 2009-10-30        17500        1.512000 0.5159198
424845 2009-10-30        22500        1.038973 0.6633255

我想计算偏差的度量,即 Imp。体积 (110%) - 进出口。体积(90%)

假设IV110为0.9431225,也就是上面数据中的第一个值。 IV90 为 0.7980267,第三个值。获得这些值后,我想计算 0.192926 - 0.190826 ,即 Impl_volatility[IV110] - Impl_volatility[IV.90] 这是我希望在新列中得到的结果。

为此创建了给定一个唯一日期 (anchor.date) 的数据子集:

#plotting the volatility surface
anchor.date <- TNX[164522,1]


#keeping a dataset with a specific date so that I can plot the Volatility Smile and Surface
TNX.surface <- subset(TNX, date == anchor.date)

然后我执行了以下操作来计算偏斜度:

IV.110 <- which(abs(TNX.surface$moneyness - 1.1) == min(abs(TNX.surface$moneyness - 1.1)))
IV.90 <- which(abs(TNX.surface$moneyness - 0.9) == min(abs(TNX.surface$moneyness - 0.9)))
skew <- TNX.surface[IV.110, 3] - TNX.surface[IV.90, 3]

但是,我想在不处理子集的情况下将此公式扩展到整个数据框。换句话说,我想对整个数据集中的偏差进行相同的计算,以便每个日期得到相同的结果(但不同日期的结果不同)

有办法吗?

谢谢!

更新:运行我得到的代码

> TNX <- setDT(TNX)

> View(TNX)
> TNX[, id110 := abs(moneyness - 1.1) == min(abs(moneyness - 1.1)), by = date]
> TNX[, id90  := abs(moneyness - 0.9) == min(abs(moneyness - 0.9)), by = date]
> TNX[, skew := impl_volatility[id110] - impl_volatility[id90], by = date][]
              date strike_price impl_volatility moneyness id110  id90      skew
     1: 1996-09-03        65000        0.192926 0.9431225 FALSE FALSE  0.005509
     2: 1996-09-03        65000        0.184757 0.9431225 FALSE FALSE  0.021010
     3: 1996-09-03        55000        0.190826 0.7980267 FALSE FALSE  0.020730
     4: 1996-09-03        60000        0.187024 0.8705746 FALSE FALSE  0.017199
     5: 1996-09-03        62500        0.189573 0.9068485 FALSE  TRUE  0.015333
    ---                                                                        
209806: 2009-10-30        20000        1.261644 0.5896226 FALSE FALSE -0.062087
209807: 2009-10-30        25000        0.835957 0.7370283 FALSE FALSE  0.019549
209808: 2009-10-30        30000        0.462221 0.8844340 FALSE  TRUE  0.191924
209809: 2009-10-30        17500        1.512000 0.5159198 FALSE FALSE        NA
209810: 2009-10-30        22500        1.038973 0.6633255 FALSE FALSE        NA



> warnings()
Warning messages:
1: In impl_volatility[id110] - impl_volatility[id90] :
  longer object length is not a multiple of shorter object length
2: In `[.data.table`(TNX, , `:=`(skew, impl_volatility[id110] -  ... :
  Supplied 6 items to be assigned to group 1 of size 49 in column 'skew' (recycled leaving remainder of 1 items).
3: In impl_volatility[id110] - impl_volatility[id90] :
  longer object length is not a multiple of shorter object length
4: In `[.data.table`(TNX, , `:=`(skew, impl_volatility[id110] -  ... :
  Supplied 6 items to be assigned to group 2 of size 50 in column 'skew' (recycled leaving remainder of 2 items).
5: In `[.data.table`(TNX, , `:=`(skew, impl_volatility[id110] -  ... :
  Supplied 4 items to be assigned to group 3 of size 49 in column 'skew' (recycled leaving remainder of 1 items).

所以,我在这里使用 data.table 复制您的计算,按日期分组。作为例子,我使用你的头部和尾部样本

library(data.table    
hh = setDT(read.table(text = "
date strike_price impl_volatility moneyness
  1996-09-03        65000        0.192926 0.9431225
  1996-09-03        65000        0.184757 0.9431225
  1996-09-03        55000        0.190826 0.7980267
  1996-09-03        60000        0.187024 0.8705746
  1996-09-03        62500        0.189573 0.9068485
 1996-09-03        72500        0.209731 1.0519443
 2009-10-30        27500        0.646013 0.8107311
 2009-10-30        20000        1.261644 0.5896226
 2009-10-30        25000        0.835957 0.7370283
 2009-10-30        30000        0.462221 0.8844340
 2009-10-30        17500        1.512000 0.5159198
 2009-10-30        22500        1.038973 0.6633255", header = T))

鉴于你的更新,看看这是否是你想要的

# create your 110 indices to find the values
hh[, id110 := abs(moneyness - 1.1) == min(abs(moneyness - 1.1)), by = date]
hh[, id90  := abs(moneyness - 0.9) == min(abs(moneyness - 0.9)), by = date]
# calculate the skew by date
hh[, skew := impl_volatility[id110] - impl_volatility[id90], by = date][]

          date strike_price impl_volatility moneyness id110  id90     skew
 1: 1996-09-03        65000        0.192926 0.9431225 FALSE FALSE 0.020158
 2: 1996-09-03        65000        0.184757 0.9431225 FALSE FALSE 0.020158
 3: 1996-09-03        55000        0.190826 0.7980267 FALSE FALSE 0.020158
 4: 1996-09-03        60000        0.187024 0.8705746 FALSE FALSE 0.020158
 5: 1996-09-03        62500        0.189573 0.9068485 FALSE  TRUE 0.020158
 6: 1996-09-03        72500        0.209731 1.0519443  TRUE FALSE 0.020158
 7: 2009-10-30        27500        0.646013 0.8107311 FALSE FALSE 0.000000
 8: 2009-10-30        20000        1.261644 0.5896226 FALSE FALSE 0.000000
 9: 2009-10-30        25000        0.835957 0.7370283 FALSE FALSE 0.000000
10: 2009-10-30        30000        0.462221 0.8844340  TRUE  TRUE 0.000000
11: 2009-10-30        17500        1.512000 0.5159198 FALSE FALSE 0.000000
12: 2009-10-30        22500        1.038973 0.6633255 FALSE FALSE 0.000000