-
Notifications
You must be signed in to change notification settings - Fork 3
/
binomialTreeModel.py
206 lines (183 loc) · 5.52 KB
/
binomialTreeModel.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
from numpy import exp, sqrt, arange
class BinomialTree:
def __init__(
self,
steps: int,
time_to_maturity: float,
strike: float,
current_price: float,
volatility: float,
interest_rate: float,
dividend_yield: float,
american_or_european: str,
):
self.steps = steps
self.delta_time = time_to_maturity / steps
self.strike = strike
self.current_price = current_price
self.volatility = volatility
self.interest_rate = interest_rate
self.american_or_european = american_or_european
self.up, self.down = self.up_down(
volatility,
self.delta_time,
)
self.risk_neutral_probability = self.risk_neutral_probability(
interest_rate,
dividend_yield,
self.delta_time,
self.up,
self.down,
volatility,
)
def up_down(
self,
volatility,
delta_time,
):
up = exp(volatility * sqrt(delta_time))
down = exp(-volatility * sqrt(delta_time))
return up, down
def risk_neutral_probability(
self,
interest_rate,
dividend_yield,
delta_time,
up,
down,
volatility
):
q = (exp((interest_rate - dividend_yield) * delta_time) - down) / (
(up - down)
)
# if delta_time > (volatility ** 2) / (interest_rate - q) ** 2:
# raise Exception("Time condition not met")
return q
def calculate_all_nodes_for_puts(
self,
strike,
current_price,
up,
down,
risk_neutral_probability,
steps,
delta_time,
interest_rate,
):
q = risk_neutral_probability
# S is the price
S = {}
# S[i, j] is defined as S[time,(step - amount of ups)]
S[0, 0] = current_price
for i in range(1, steps+1):
for j in range(0, i+1):
if j > 0:
S[i, j] = S[i-1, j-1] * up
else:
S[i, 0] = S[i-1, 0] * down
# P-alive vs P-exercise
P = {}
exercise = {}
for i in arange(steps, -1, -1):
for j in range(0, i+1):
if i == steps:
P[i, j] = max(strike - S[i, j], 0)
else:
P[i, j] = exp(-interest_rate * delta_time) * (
q * P[i+1, j+1] + (1 - q) * P[i+1, j]
)
P[i, j] = max(P[i, j], 0)
if P[i, j] > (strike - S[i, j]):
exercise[i, j] = 'no'
else:
exercise[i, j] = 'yes'
if self.american_or_european == 'A':
P[i, j] = max(P[i, j], (strike - S[i, j]))
return S, P, exercise
def calculate_all_nodes_for_calls(
self,
strike,
current_price,
up,
down,
risk_neutral_probability,
steps,
delta_time,
interest_rate,
):
q = risk_neutral_probability
# S is the price
S = {}
# S[i, j] is defined as S[time,(step - amount of ups)]
S[0, 0] = current_price
for i in range(1, steps+1):
for j in range(0, i+1):
if j > 0:
S[i, j] = S[i-1, j-1] * up
else:
S[i, 0] = S[i-1, 0] * down
# P-alive vs P-exercise
P = {}
exercise = {}
for i in arange(steps, -1, -1):
for j in range(0, i+1):
if i == steps:
P[i, j] = max(S[i, j] - strike, 0)
else:
P[i, j] = exp(-interest_rate * delta_time) * (
q * P[i+1, j+1] + (1 - q) * P[i+1, j]
)
P[i, j] = max(P[i, j], 0)
if P[i, j] > (S[i, j] - strike):
exercise[i, j] = 'no'
else:
exercise[i, j] = 'yes'
if self.american_or_european == 'A':
P[i, j] = max(P[i, j], (S[i, j] - strike))
return S, P, exercise
def run(self):
self.put_S, self.put_P, self.put_exercise = (
self.calculate_all_nodes_for_puts(
self.strike,
self.current_price,
self.up,
self.down,
self.risk_neutral_probability,
self.steps,
self.delta_time,
self.interest_rate,
)
)
self.call_S, self.call_P, self.call_exercise = (
self.calculate_all_nodes_for_calls(
self.strike,
self.current_price,
self.up,
self.down,
self.risk_neutral_probability,
self.steps,
self.delta_time,
self.interest_rate,
)
)
if __name__ == "__main__":
steps = 4
time_to_maturity = 2
strike = 90
current_price = 100
volatility = 0.2
interest_rate = 0.05
dividend_yield = 0.0
american_or_european = 'E'
# Binomial Tree
BT = BinomialTree(
steps=steps,
time_to_maturity=time_to_maturity,
strike=strike,
current_price=current_price,
volatility=volatility,
interest_rate=interest_rate,
dividend_yield=dividend_yield,
american_or_european=american_or_european,
)
BT.run()