所以我有一个这样的数据框:
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。 Vol(110%) - Imp。体积(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).
答案 0 :(得分:0)
所以,在这里,我使用data.table复制你的计算,按日期分组。作为一个例子,我正在使用你的头部尾部样本
# 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
鉴于您的更新,请查看这是否是您想要的
multi_match