0

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

    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).
4

1 回答 1

0

因此,在这里我使用 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
于 2018-02-07T16:16:29.847 回答