-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathbinomial_option_model.py
More file actions
36 lines (30 loc) · 975 Bytes
/
binomial_option_model.py
File metadata and controls
36 lines (30 loc) · 975 Bytes
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
import numpy as np
def binomial_model(N, S0, u, r, K):
"""
N = number of binomial iterations
S0 = initial stock price
u = factor change of upstate
r = risk free interest rate per annum
K = strike price
"""
d = 1 / u
p = (1 + r - d) / (u - d)
q = 1 - p
# make stock price tree
stock = np.zeros([N + 1, N + 1])
for i in range(N + 1):
for j in range(i + 1):
stock[j, i] = S0 * (u ** (i - j)) * (d ** j)
# Generate option prices recursively
option = np.zeros([N + 1, N + 1])
option[:, N] = np.maximum(np.zeros(N + 1), (stock[:, N] - K))
for i in range(N - 1, -1, -1):
for j in range(0, i + 1):
option[j, i] = (
1 / (1 + r) * (p * option[j, i + 1] + q * option[j + 1, i + 1])
)
return option
if __name__ == "__main__":
print("Calculating example option price:")
op_price = binomial_model(5, 4, 2, 0.25, 8)
print(op_price)