-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_BS.py
153 lines (97 loc) · 2.67 KB
/
examples_BS.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# @author: quantomas
# original code: The Python Quants
## Black-Scholes-Merton Option Pricing
# Libs
import numpy as np
np.set_printoptions(precision=5)
import math
from pylab import mpl, plt
S0 = 100
r = 0.05
sigma = 0.2
T = 1.0
# I = 10000
I = 1000000
# np.random.seed(100)
ST = S0 * np.exp((r - sigma ** 2 / 2) * T +
sigma * math.sqrt(T) * np.random.standard_normal(I))
ST[:8].round(1)
ST.mean()
S0 * math.exp(r * T)
## Plot
# Im Intervall der Abweichung ± σ \pm \sigma vom Erwartungswert sind 68,27 % aller Messwerte zu finden
plt.style.use('seaborn')
mpl.rcParams['savefig.dpi'] = 300
mpl.rcParams['font.family'] = 'serif'
plt.figure(figsize=(10, 6))
plt.hist(ST, bins=1000, label='frequency');
# plt.hist(ST, bins=30, label='frequency');
plt.axvline(ST.mean(), color='r', label='mean')
plt.axvline(ST.mean() + ST.std(), color='y', label='sd up')
plt.axvline(ST.mean() - ST.std(), color='y', label='sd down')
# plt.title("Im Intervall der Abweichung ± \sigma vom Erwartungswert sind 68,27 % aller Messwerte zu finden")
plt.title(r'Im Intervall der Abweichung ± $\sigma$ vom Erwartungswert sind 68,27 % aller Messwerte zu finden')
plt.legend(loc=0);
plt.show()
# Call-Option, basierend auf simuliertem ST
K = 105
CT = np.maximum(ST - K, 0)
CT[:8].round(1)
C0 = math.exp(-r * T) * CT.mean()
C0
## Monte Carlo Simulation
np.set_printoptions(formatter= {'float_kind': lambda x: '%7.3f' % x})
# Parameter - Bsp. 1
S0 = 36.
r = 0.06
T = 1.0
sigma = 0.2
# Parameter - Bsp. 2
S0 = 1.
r = 0.8
T = 1.0
sigma = 0.4
# Simulating the Stock Price Process
M = 100
I = 50000
dt = T / M
dt
np.random.seed(100)
rn = np.random.standard_normal((M + 1, I))
rn.shape
rn.round(2)
S = np.zeros_like(rn)
S[0] = S0
S
for t in range(1, M + 1):
S[t] = S[t - 1] * np.exp((r - sigma ** 2 / 2) * dt +
sigma * math.sqrt(dt) * rn[t])
S
# Plot
# t = np.arange(0, M + 1)
def f(x):
return S0 * np.exp(r * x / M * T)
m_grid = np.arange(0, M + 1)
t_grid = T * m_grid / M
exp_val = S0 * np.exp(r * t_grid)
plt.style.use('seaborn')
mpl.rcParams['font.family'] = 'serif'
mpl.rcParams['savefig.dpi'] = 300
plt.figure(figsize=(10, 6))
plt.plot(S[:, :100]);
# plt.plot(S[:, 10:20]);
# plt.plot(t, f(t), 'r-', linewidth=3)
plt.plot(m_grid, exp_val, 'r-', linewidth=3)
plt.show()
#
ST = S[-1]
plt.figure(figsize=(10, 6))
# plt.hist(ST, bins=35, color='b', label='frequency');
plt.hist(ST, bins=1000, color='b', label='frequency');
plt.axvline(ST.mean(), color='r', label='mean')
plt.axvline(ST.mean() + ST.std(), color='y', label='sd up')
plt.axvline(ST.mean() - ST.std(), color='y', label='sd down')
plt.legend(loc=0);
plt.show()
S0 * math.exp(r * T)
S[-1].mean()