"""
Created on Sun Nov 6 16:26:56 2016
@author: Stan Wang
Assignment_1 Delta hedging of an option
"""
import scipy as sp
import math
import numpy as np
import scipy.stats as ss
def d1(S
0, K, r, sigma, T):
return (np.
log(S
0/K) + (r + sigma
**2 /
2) * T)/(sigma * np.
sqrt(T))
def d2(S
0, K, r, sigma, T):
return (np.
log(S
0/K) + (r - sigma
**2 /
2) * T)/(sigma * np.
sqrt(T))
def BS_Call(S
0, K, r, sigma, T):
return S
0 * ss.norm.cdf(d1(S
0, K, r, sigma, T)) - K * np.
exp(-r * T) * ss.norm.cdf(d2(S
0, K, r, sigma, T))
def MC_deltaHedging_Call(S
,X
,T
,sigma
,expectedReturn
,r
,numMC
,numsim
):
dt = T/float(numsim)
drift=(r-
0.
5*sigma*sigma)
*dt
sigmasqrtdt = sigma * math.
sqrt(dt)
portfolio = sp.zeros([numMC],dtype=float)
interest = np.
exp(r
*dt)
for j in range(
0,numMC):
stockPrice = S
callValue01 = BS_Call(stockPrice,X,r,sigma,T)
delta01 = ss.norm.cdf(d1(stockPrice,X,r,sigma,T))
moneyAccount = delta01 * stockPrice - callValue01
print(
"the stock price is {}".
format(stockPrice))
print(
"to hedge one call option, we sell {} shares of stock".
format(delta01))
for i in range(
1,numsim):
print(
"in the {} day: ".
format(i+
1))
e = sp.random.normal(
0,
1)
stockPrice
*= np.
exp(drift + sigmasqrtdt * e)
print(
"the stock price is {}".
format(stockPrice) )
moneyAccount
*= interest
callValue02 = BS_Call(stockPrice,X,r,sigma,T-i
*dt)
delta02 = ss.norm.cdf(d1(stockPrice,X,r,sigma,T-i
*dt))
PnL_call = callValue02 - callValue01
print(
"the P&L of the option is {}".
format(PnL_call))
print(
"the new delta is {}".
format(delta01))
print(
"the amount of stock we changed is {}".
format(delta01-delta02))
moneyAccount +=(delta02-delta01)
*stockPrice
delta01 = delta02
callValue01=callValue02
e = sp.random.normal(
0,
1);
stockPrice
*= math.
exp(drift+sigmasqrtdt * e)
print(
"the stock price at maturity is {}".
format(stockPrice))
callValue = max(stockPrice-X,
0)
print(
"the call value at maturity is {}".
format(callValue))
moneyAccount
*= interest
portfolio[j] = moneyAccount+callValue-delta01
*stockPrice
print(
"the portfolio at maturity is {}".
format(portfolio[j]))
portfolioValue =sp.mean(portfolio)
return portfolioValue
print(
"the result from Monte Carlo for delta hedging strategy is {}".
format(MC_deltaHedging_Call(
100,
100,
1,
0.
1,
0.
1,
0.
05,
1,
360)))
转载请注明原文地址: https://ju.6miu.com/read-658440.html