forked from jerryxyx/MonteCarlo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binomial.py
131 lines (117 loc) · 4.3 KB
/
binomial.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
import numpy as np
import math
def call(S, K, sigma, r, t, div=0, n=100, am=False):
"""
Price a call option using the Binomial Options Pricing model
S: initial spot price of stock
K: strick price of option
sigma: volatility
r: risk-free interest rate
t: time to maturity (in years)
div: dividend yield (continuous compounding)
n: binomial steps
am: True for American option, False for European option
%timeit results: 0 loops, best of 3: 49.6 ms per loop
"""
return option(S, K, sigma, r, t, div, 1, n, am)
def put(S, K, sigma, r, t, div=0, n=100, am=False):
"""
Price a put option using the Binomial Options Pricing model
S: initial spot price of stock
K: strick price of option
sigma: volatility
r: risk-free interest rate
t: time to maturity (in years)
div: dividend yield (continuous compounding)
n: binomial steps
am: True for American option, False for European option
%timeit results: 10 loops, best of 3: 50.5 ms per loop
"""
return option(S, K, sigma, r, t, div, -1, n, am)
def option(S, K, sigma, r, t, div=0, call=1, n=100, am=False):
"""
Price an option using the Binomial Options Pricing model
S: initial spot price of stock
K: strick price of option
sigma: volatility
r: risk-free interest rate
t: time to maturity (in years)
div: dividend yield (continuous compounding)
call: 1 if call option, -1 if put
n: binomial steps
am: True for American option, False for European option
"""
delta = float(t) / n
u = math.exp(sigma * math.sqrt(delta))
d = float(1) / u
q = float((math.exp((r - div) * delta) - d)) / (u - d) # Prob. of up step
stock_val = np.zeros((n + 1, n + 1))
opt_val = np.zeros((n + 1, n + 1))
# Calculate stock value at maturity
stock_val[0, 0] = S
for i in range(1, n + 1):
stock_val[i, 0] = stock_val[i - 1, 0] * u
for j in range(1, i + 1):
stock_val[i, j] = stock_val[i - 1, j - 1] * d
# Recursion for option price
for j in range(n + 1):
opt_val[n, j] = max(0, call*(stock_val[n, j] - K))
for i in range(n - 1, -1, -1):
for j in range(i + 1):
opt_val[i, j] = \
(q * opt_val[i + 1, j] + (1 - q) * opt_val[i + 1, j + 1]) \
/ math.exp(r * delta)
if am:
opt_val[i, j] = max(opt_val[i, j], call*(stock_val[i, j] - K))
return opt_val[0, 0]
def call2(S, K, sigma, r, t, div=0, n=100, am=False):
"""
Price a call option using the Binomial Options Pricing model
S: initial spot price of stock
K: strick price of option
sigma: volatility
r: risk-free interest rate
t: time to maturity (in years)
div: dividend yield (continuous compounding)
n: binomial steps
%timeit results: 10000 loops, best of 3: 139 us per loop
"""
return option2(S, K, sigma, r, t, div, 1, n, am)
def put2(S, K, sigma, r, t, div=0, n=100, am=False):
"""
Price a put option using the Binomial Options Pricing model
S: initial spot price of stock
K: strick price of option
sigma: volatility
r: risk-free interest rate
t: time to maturity (in years)
div: dividend yield (continuous compounding)
n: binomial steps
%timeit results: 10000 loops, best of 3: 136 us per loop
"""
return option2(S, K, sigma, r, t, div, -1, n, am)
def option2(S, K, sigma, r, t, div=0, call=1, n=100, am=False):
"""
Price an option using the Binomial Options Pricing model
S: initial spot price of stock
K: strick price of option
sigma: volatility
r: risk-free interest rate
t: time to maturity (in years)
div: dividend yield (continuous compounding)
n: binomial steps
"""
delta = float(t) / n
u = math.exp(sigma * math.sqrt(delta))
d = float(1) / u
pu = float((math.exp((r - div) * delta) - d)) / (u - d) # Prob. of up step
pd = 1 - pu # Prob. of down step
u_squared = u * u
S = S * pow(d, n) # stock price at bottom node at last date
prob = pow(pd, n) # prob. of bottom node at last date
opt_val = prob * max(0, call*(S - K))
for i in range(1, n):
S = S * u_squared
prob = prob * (float(pu) / pd) * (n - i + 1) / i
opt_val = opt_val + prob * max(call*(S - K), 0)
return math.exp(-r * t) * opt_val