2
$\begingroup$

I am trying to compute a hedge for a variance swap, in a simulation. Fo that I am using the following equation:\begin{align*} E^Q\bigg(\sum_{i=1}^n \bigg(\frac{S_{t_{i}}-S_{t_{i-1}}}{S_{t_{i-1}}}\bigg)^2\bigg) &\approx -2E^Q\bigg(\ln\frac{S_T}{S_0} \bigg)\\ &=2E^Q\bigg[\int_{S_0}^{\infty} \frac{(S_T-K)^+}{K^2} dK + \int_0^{S_0} \frac{(K-S_T)^+}{K^2} dK\bigg], \end{align*}

I have a piece of code that instead of using the formula above uses:

$$ \sum_{j=-(m-1)}^{-1}(K_j-K_l)^+-\sum_{j=-(m-1)}^{-1}(K_j-K_0)^+=2\ln(S_0/K_l) $$

That code is:

S0<-100
dK<-0.05*S0
nK<-3

K<-S0+dK*c(((-(nK-1)):(nK-1))) # strikes used for static portfolio
                               # puts for K<=S0, calls for K >= S0

x<-S0+dK*c((-nK):(-1),1:nK)    # S(T)-values at which the option portfolio
                               # matches 2ln(S(0)/S(T)) exacty

RHS<-2*log(S0/x)

A<-matrix(0,nrow=2*nK,ncol=(2*nK))

for (i in 1:nK){
  A[i,1:i]<-dK*i:1; A[2*nK-(i-1),(2*nK-(i-1)):(2*nK)]<-rev(A[i,1:i])
}
print(A)

print(rev(A))
w<-solve(t(A),RHS)

y<-50:150

capT<-1/12
n=20
dt=capT/n; dt05<-sqrt(dt)
time<-seq(0,capT,by=dt)

S<-rep(S0,(n+1))
sigma<-0.15

price<-0

largeLHS<-largeRHS<-2*log(S0/y)

for (i in 1:length(y)){
  largeLHS[i]<-sum(w[1:nK]*pmax(K[1:nK]-y[i],0))+sum(w[(nK+1):(2*nK)]*pmax(y[i]-K[nK:(2*nK-1)],0))
}

plot(y,largeRHS,type='l',xlab='S(T)',ylab='Payoff @ T', main="Variance swap hedging: \n Matching the log-contract")
text(55,1.25,"2*ln(S(0)/S(T))",adj=0)
text(140,-0.75,"Option portfolio",adj=1,col="blue")
text(145,1.25,paste("dK =",dK),adj=1)
text(145,1.05,paste("#calls = #puts =",nK),adj=1)
points(x,RHS,col='blue')
points(y,largeLHS,col='blue',type='l')



Since I am a beginner at programming, I am trying to adapt the code to the formula above.

I tried to alter the second for loop in the following way:

for (i in 1:length(y)){
  largeLHS[i]<-sum((2/(K[1:nK])^2)*pmax(K[1:nK]-y[i],0))+sum((2/(K[nK:(2*nK-1)])^2)*pmax(y[i]-K[nK:(2*nK-1)],0))
}


But the plot is a mess, the two lines are not meeting but crossing each other.

Question:

Can someone help me solve this problem? And give me some insight into the matrix part of the code I provided?

Thanks in advance!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.