-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCRR_option_pricing.py
231 lines (196 loc) · 6.3 KB
/
CRR_option_pricing.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
'''
CRR model for option pricing
JIMWWWJIM
collected from https://github.com/ququcai/option/blob/master/option_CRR.ipynb
'''
# packages
import math
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from scipy.integrate import quad
mpl.rcParams['font.family'] = 'serif'
#parameters
S0 = 3475.0 # index level
K = 3475.0 # option strike
T = 1.0 # maturity date
r = 0.02 # risk-less short rate
sigma = 0.22 # volatility
#BSM模型定价
def dN(x):
''' Probability density function of standard normal random variable x. '''
return math.exp(-0.5 * x ** 2) / math.sqrt(2 * math.pi)
def N(d):
''' Cumulative density function of standard normal random variable x. '''
return quad(lambda x: dN(x), -20, d, limit=50)[0]
def d1f(St, K, t, T, r, sigma):
''' Black-Scholes-Merton d1 function.
Parameters see e.g. BSM_call_value function. '''
d1 = (math.log(St / K) + (r + 0.5 * sigma ** 2)
* (T - t)) / (sigma * math.sqrt(T - t))
return d1
#
# Valuation Functions
#
def BSM_call_value(St, K, t, T, r, sigma):
''' Calculates Black-Scholes-Merton European call option value.
Parameters
==========
St : float
stock/index level at time t
K : float
strike price
t : float
valuation date
T : float
date of maturity/time-to-maturity if t = 0; T > t
r : float
constant, risk-less short rate
sigma : float
volatility
Returns
=======
call_value : float
European call present value at t
'''
d1 = d1f(St, K, t, T, r, sigma)
d2 = d1 - sigma * math.sqrt(T - t)
call_value = St * N(d1) - math.exp(-r * (T - t)) * K * N(d2)
return call_value
def BSM_put_value(St, K, t, T, r, sigma):
''' Calculates Black-Scholes-Merton European put option value.
Parameters
==========
St : float
stock/index level at time t
K : float
strike price
t : float
valuation date
T : float
date of maturity/time-to-maturity if t = 0; T > t
r : float
constant, risk-less short rate
sigma : float
volatility
Returns
=======
put_value : float
European put present value at t
'''
put_value = BSM_call_value(St, K, t, T, r, sigma) \
- St + math.exp(-r * (T - t)) * K
return put_value
# CRR欧式期权
def CRR_european_option_value(S0, K, T, r, sigma, otype, M=4):
''' Cox-Ross-Rubinstein European option valuation.
Parameters
==========
S0 : float
stock/index level at time 0
K : float
strike price
T : float
date of maturity
r : float
constant, risk-less short rate
sigma : float
volatility
otype : string
either 'call' or 'put'
M : int
number of time intervals
'''
# 生成二叉树
dt = T / M # length of time interval
df = math.exp(-r * dt) # discount per interval
# 计算udp
u = math.exp(sigma * math.sqrt(dt)) # up movement
d = 1 / u # down movement
q = (math.exp(r * dt) - d) / (u - d) # martingale branch probability
# 初始化幂矩阵
mu = np.arange(M + 1)
mu = np.resize(mu, (M + 1, M + 1))
#print(mu)
md = np.transpose(mu)
#print(md)
#print(mu - md)
mu = u ** (mu - md)
md = d ** md
#print(mu)
#print(md)
#得到各节点的股票价格
S = S0 * mu * md
# 得到叶子结点的期权价值
if otype == 'call':
V = np.maximum(S - K, 0) # inner values for European call option
else:
V = np.maximum(K - S, 0) # inner values for European put option
#逐步向前加权平均并折现,得到期初期权价值
for z in range(0, M): # backwards iteration
#逐列更新期权价值,相当于二叉树中的逐层向前折算
V[0:M - z, M - z - 1] = (q * V[0:M - z, M - z] +
(1 - q) * V[1:M - z + 1, M - z]) * df
return V[0, 0]
# CRR美式期权
def CRR_american_option_value(S0, K, T, r, sigma, otype, M=4):
# 一.生成二叉树
dt = T / M # length of time interval
df = math.exp(-r * dt) # discount per interval
inf = math.exp(r * dt) # discount per interval
# 计算udp
u = math.exp(sigma * math.sqrt(dt)) # up movement
d = 1 / u # down movement
q = (math.exp(r * dt) - d) / (u - d) # martingale branch probability
# 初始化幂矩阵
mu = np.arange(M + 1)
mu = np.resize(mu, (M + 1, M + 1))
md = np.transpose(mu)
# 计算个节点单向变动时的股票价格
mus = u ** (mu - md)
mds = d ** md
# 得到各节点的股票价格
S = S0 * mus * mds
# 二.计算每个节点股票的预期价格
mes = S0 * inf ** mu
# 三.得到叶子结点的期权价值
if otype == 'call':
V = np.maximum(S - K, 0)
#计算每个节点提前行权的收益
oreturn = mes - K
else:
V = np.maximum(K - S, 0)
#计算每个节点提前行权的收益
oreturn = K - mes
# 四.逐步向前加权平均折现和提前行权的收益比较,得到期初期权价值
for z in range(0, M): # backwards iteration
#计算后期折现的后期价格
ovalue = (q * V[0:M - z, M - z] +
(1 - q) * V[1:M - z + 1, M - z]) * df
#逐列更新期权价值,相当于二叉树中的逐层向前折算
#期权价格取后期折现和提前行权获得收益的最大值
V[0:M - z, M - z - 1] = np.maximum(ovalue, oreturn[0:M - z, M - z - 1])
return V[0, 0]
#result printing
mmin=2
mmax=200
step_size=1
print('CRR model, calculating american option')
print(CRR_american_option_value(S0, K, T, r, sigma, 'call', 100))
print('CRR model, calculating european option')
print(CRR_european_option_value(S0, K, T, r, sigma, 'call', 100))
print('BS_put')
print(BSM_put_value(S0, K, 0, T, r, sigma))
#result visulization
BSM_benchmark = BSM_call_value(S0, K, 0, T, r, sigma)
m = range(mmin, mmax, step_size)
CRR_values = [CRR_european_option_value(S0, K, T, r, sigma, 'call', M) for M in m]
plt.figure(figsize=(9, 5))
plt.plot(m, CRR_values, label='CRR')
plt.axhline(BSM_benchmark, color='r', ls='dashed', lw=1.5,
label='BSM')
plt.xlabel('Steps')
plt.ylabel('European call option value')
plt.legend(loc=4)
plt.xlim(0, mmax)
plt.show()