Sunday, 7 July 2013

Variance Swap Replication in R.

As I was studying volatility derivatives I made some charts that represent some key features of replication. Say variance swap has a payoff function \(f=(\sigma - K_{VOL}) \), which means that \(K_{VOL}\) will most likely be the forward volatility close to implied. To replicate this theory goes deep into maths and log-contracts that are not even traded on the market, however the idea is simple, buy a portfolio of options with equally distributed strike prices and weight them by reciprocal of squared strikes, i.e.\( 1 \big/ K^2 \). Go for liquid ones, i.e. out of the money puts and out of the money calls. Then volatility or in this case variance is dependent on VEGA sensitivity of portfolio. The following graph gives an idea of how it is done. The code is included below:




X-Y - spot/time to maturity, Z - Vega \( \left(\frac{\partial C}{\partial \sigma}\right) \).

Code:

vega <- function(S0=100, K=100, r=0.05, d=0.05, tau=1, vol=0.1){
    d_1  <- (log(S0/K)+((r-d)+vol^2/2)*tau)/(vol*sqrt(tau));
    S0*sqrt(tau)*(1/sqrt(2*pi)*exp(-d_1^2/2))*exp(-d*tau)}

vegaK <- function(K, t){
    sapply(t, function(x){vega(S0=50:200, K=K, tau=x)/K^2}) }

my3d_plot <- function(mat, x, y, col, bgcol="slatergray", material="lines", add=FALSE, ...){
    require(rgl);
    if( missing(x) ){ x <- 1:nrow(mat) }; if( missing(y) ){ y <- 1:ncol(mat) }
    if( missing(col) ){ col <- rev(rep(heat.colors(ncol(mat),alpha=0),each=nrow(mat))) }
    if( !add ){ open3d(); material3d(color=bgcol) }
    persp3d(x, y, mat, col = col, alpha=1, front=material, back=material, add=add, ...)}

mat <- lapply(seq(60,180,15), vegaK, t=seq(1,0,length=20))
mat$sum <- Reduce("+", mat)

my3d_plot(mat$sum, bgcol="white", col="grey", aspect=c(4, 3, 2))
sapply(mat[1:9], my3d_plot, col="green", add=TRUE)