so I have a dataframe like this:
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
I want to calculate a measure of skew i.e. Imp. Vol(110%) - Imp. Vol(90%)
Suppose that IV110 is 0.9431225, that is the first value in the data above. IV90 is 0.7980267, the third value. Once I have these values, I want to compute 0.192926 - 0.190826 , that is Impl_volatility[IV110] - Impl_volatility[IV.90] This is the outcome I want in the new column.
In order to do so created subsets of the data given one unique date (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)
Then I did the following to compute the measure of skew:
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]
However, I would like to extend this formula to the whole dataframe without working on subsets. In other words, I want to make the same calculation for the skew in the whole dataset so that I get the same result for each date (but different results throughout the different dates)
Is there a way to do so?
Thanks!
Update: Running the code I get this
> 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).