-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathDELTA.py
More file actions
87 lines (63 loc) · 1.97 KB
/
DELTA.py
File metadata and controls
87 lines (63 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Monte Carlo Valuation of a European Option in a Black-Scholes World
# With implementation of Delta-based control variate method
# by Michal Lyskawinski
# 10/31/2016
from math import *
import numpy as np
import random
from scipy.stats import norm
def CBS(S, K, T, r, sigma,t, option):
t2t = T-t # time to maturity
# Calculations for the solution to BSM equation
dplus = (1 / (sigma * sqrt(t2t))) * ((log(S / K)) + (r + ((sigma ** 2) / 2)) * t2t)
dminus = (1 / (sigma * sqrt(t2t))) * ((log(S / K)) + (r - (sigma ** 2) / 2) * t2t)
# Calculating price of Call and Put
if option == 'Call':
return S * norm.cdf(dplus) - K * exp(-r * t2t) * norm.cdf(dminus)
elif option == 'Put':
return K * exp(-r * t2t) * norm.cdf(-dminus) - S * norm.cdf(-dplus)
# Initialize parameters
S = 100
r = 0.06
sig = 0.2
T = 1
K = 100
N = 10
M = 100
div = 0.03 # In percentage
option = 'Call'
# Precompute constants
dt = T/N
nu = r - div - 0.5*(sig**2)
nudt = nu*dt
sigsdt = sig*sqrt(dt)
erddt = exp((r-div)*dt)
beta1 = -1
sum_CT = 0
sum_CT2 = 0
for j in range(1,M): # For each simulation
St = S
cv = 0
for i in range(1,N): # For each time step
t = (i-1)*dt
delta = CBS(St,K,T,r,sig,t,option)
eps = np.random.normal(0, 1)
Stn = St*exp(nudt+sigsdt*eps)
cv1 = cv + delta*(Stn-St*erddt)
St = Stn
if option == 'Call':
CT = max(0, St - K) + beta1*cv1
sum_CT = sum_CT + CT
sum_CT2 = sum_CT2 + CT*CT
elif option == 'Put':
CT = max(0, K - St) + beta1*cv1
sum_CT = sum_CT + CT
sum_CT2 = sum_CT2 + CT * CT
else:
break
Value = sum_CT/M*exp(-r*T)
SD = sqrt((sum_CT2 - sum_CT*sum_CT/M)*exp(-2*r*T)/(M-1))
SE = SD/sqrt(M)
print('The Value of European',option,'Option is',Value)
print('The Standard Deviation of this Option is',SD)
print('The Standard Error in this case is',SE)