Skip to content

Instantly share code, notes, and snippets.

@yhilpisch
Last active March 23, 2024 05:36
Show Gist options
  • Save yhilpisch/e2af84bc2ff1cab88d6be019459f5672 to your computer and use it in GitHub Desktop.
Save yhilpisch/e2af84bc2ff1cab88d6be019459f5672 to your computer and use it in GitHub Desktop.

Market-Based Valuation of Equity Options

CQF Lecture, 19. September 2017, London

Dr. Yves J. Hilpisch, The Python Quants GmbH

General resources:

Abstract

This lecture covers numerical methods for the market-based valuation of equity options. The lecture is mainly based on the book Derivatives Analytics with Python (http://dawp.tpq.io).

Slides

You find the slides under http://tpq.io/p/cqf_lecture_sept_2017.html

Python

This Gist contains the files needed to replicate all the results shown during the lecture. The code base has been updated to Python 3.6.

Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#
# Valuation of European Call Options in BSM Model
# and Numerical Derivation of Implied Volatility
# 03_stf/BSM_imp_vol.py
#
# (c) Dr. Yves J. Hilpisch
# from Hilpisch, Yves (2014): Python for Finance, O'Reilly.
#
from math import log, sqrt, exp
from scipy import stats
from scipy.optimize import fsolve
class call_option(object):
''' Class for European call options in BSM Model.
Attributes
==========
S0 : float
initial stock/index level
K : float
strike price
t : datetime/Timestamp object
pricing date
M : datetime/Timestamp object
maturity date
r : float
constant risk-free short rate
sigma : float
volatility factor in diffusion term
Methods
=======
value : float
return present value of call option
vega : float
return vega of call option
imp_vol : float
return implied volatility given option quote
'''
def __init__(self, S0, K, t, M, r, sigma):
self.S0 = float(S0)
self.K = K
self.t = t
self.M = M
self.r = r
self.sigma = sigma
def update_ttm(self):
''' Updates time-to-maturity self.T. '''
if self.t > self.M:
raise ValueError("Pricing date later than maturity.")
self.T = (self.M - self.t).days / 365.
def d1(self):
''' Helper function. '''
d1 = ((log(self.S0 / self.K)
+ (self.r + 0.5 * self.sigma ** 2) * self.T)
/ (self.sigma * sqrt(self.T)))
return d1
def value(self):
''' Return option value. '''
self.update_ttm()
d1 = self.d1()
d2 = ((log(self.S0 / self.K)
+ (self.r - 0.5 * self.sigma ** 2) * self.T)
/ (self.sigma * sqrt(self.T)))
value = (self.S0 * stats.norm.cdf(d1, 0.0, 1.0)
- self.K * exp(-self.r * self.T) * stats.norm.cdf(d2, 0.0, 1.0))
return value
def vega(self):
''' Return Vega of option. '''
self.update_ttm()
d1 = self.d1()
vega = self.S0 * stats.norm.pdf(d1, 0.0, 1.0) * sqrt(self.T)
return vega
def imp_vol(self, C0, sigma_est=0.2):
''' Return implied volatility given option price. '''
option = call_option(self.S0, self.K, self.t, self.M,
self.r, sigma_est)
option.update_ttm()
def difference(sigma):
option.sigma = sigma
return option.value() - C0
iv = fsolve(difference, sigma_est)[0]
return iv
#
# Black-Scholes-Merton Implied Volatilities of
# Call Options on the EURO STOXX 50
# Option Quotes from 30. September 2014
# Source: www.eurexchange.com, www.stoxx.com
# 03_stf/ES50_imp_vol.py
#
# (c) Dr. Yves J. Hilpisch
# Derivatives Analytics with Python
#
import numpy as np
import pandas as pd
from BSM_imp_vol import call_option
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['font.family'] = 'serif'
# Pricing Data
pdate = pd.Timestamp('30-09-2014')
#
# EURO STOXX 50 index data
#
# URL of data file
es_url = 'http://www.stoxx.com/download/historical_values/hbrbcpe.txt'
# column names to be used
cols = ['Date', 'SX5P', 'SX5E', 'SXXP', 'SXXE',
'SXXF', 'SXXA', 'DK5F', 'DKXF', 'DEL']
# reading the data with pandas
es = pd.read_csv(es_url, # filename
header=None, # ignore column names
index_col=0, # index column (dates)
parse_dates=True, # parse these dates
dayfirst=True, # format of dates
skiprows=4, # ignore these rows
sep=';', # data separator
names=cols) # use these column names
# deleting the helper column
del es['DEL']
S0 = es['SX5E']['30-09-2014']
r = -0.05
#
# Option Data
#
data = pd.read_csv('es50_option_data.csv', index_col=0)
data['Date'] = data['Date'].apply(lambda x: pd.Timestamp(x))
data['Maturity'] = data['Maturity'].apply(lambda x: pd.Timestamp(x))
#
# BSM Implied Volatilities
#
def calculate_imp_vols(data):
''' Calculate all implied volatilities for the European call options
given the tolerance level for moneyness of the option.'''
data['Imp_Vol'] = 0.0
tol = 0.30 # tolerance for moneyness
for row in data.index:
t = data['Date'][row]
T = data['Maturity'][row]
ttm = (T - t).days / 365.
forward = np.exp(r * ttm) * S0
if (abs(data['Strike'][row] - forward) / forward) < tol:
call = call_option(S0, data['Strike'][row], t, T, r, 0.2)
data['Imp_Vol'][row] = call.imp_vol(data['Call'][row])
return data
#
# Graphical Output
#
markers = ['.', 'o', '^', 'v', 'x', 'D', 'd', '>', '<']
def plot_imp_vols(data):
''' Plot the implied volatilites. '''
maturities = sorted(set(data['Maturity']))
plt.figure(figsize=(10, 6))
for i, mat in enumerate(maturities):
dat = data[(data['Maturity'] == mat) & (data['Imp_Vol'] > 0)]
plt.plot(dat['Strike'].values, dat['Imp_Vol'].values,
'b%s' % markers[i], label=str(mat)[:10])
plt.grid(True)
plt.legend()
plt.xlabel('strike')
plt.ylabel('implied volatility')
plt.show()
Date Strike Call Maturity Put
0 2014-09-30 1850.0 1373.6 2014-12-19 0.5
1 2014-09-30 1900.0 1323.7 2014-12-19 0.6
2 2014-09-30 1950.0 1273.8 2014-12-19 0.8
3 2014-09-30 2000.0 1223.9 2014-12-19 0.9
4 2014-09-30 2050.0 1174.1 2014-12-19 1.1
5 2014-09-30 2100.0 1124.3 2014-12-19 1.3
6 2014-09-30 2150.0 1074.5 2014-12-19 1.5
7 2014-09-30 2200.0 1024.8 2014-12-19 1.7
8 2014-09-30 2250.0 975.1 2014-12-19 2.1
9 2014-09-30 2300.0 925.4 2014-12-19 2.4
10 2014-09-30 2350.0 875.9 2014-12-19 2.9
11 2014-09-30 2400.0 826.4 2014-12-19 3.4
12 2014-09-30 2450.0 777.0 2014-12-19 4.0
13 2014-09-30 2500.0 727.8 2014-12-19 4.8
14 2014-09-30 2550.0 678.7 2014-12-19 5.7
15 2014-09-30 2600.0 629.9 2014-12-19 6.9
16 2014-09-30 2625.0 605.6 2014-12-19 7.6
17 2014-09-30 2650.0 581.3 2014-12-19 8.3
18 2014-09-30 2675.0 557.2 2014-12-19 9.2
19 2014-09-30 2700.0 533.2 2014-12-19 10.1
20 2014-09-30 2725.0 509.2 2014-12-19 11.2
21 2014-09-30 2750.0 485.4 2014-12-19 12.4
22 2014-09-30 2775.0 461.8 2014-12-19 13.8
23 2014-09-30 2800.0 438.3 2014-12-19 15.3
24 2014-09-30 2825.0 415.0 2014-12-19 17.0
25 2014-09-30 2850.0 391.9 2014-12-19 18.9
26 2014-09-30 2875.0 369.0 2014-12-19 21.0
27 2014-09-30 2900.0 346.4 2014-12-19 23.4
28 2014-09-30 2925.0 324.1 2014-12-19 26.0
29 2014-09-30 2950.0 302.0 2014-12-19 29.0
30 2014-09-30 2975.0 280.4 2014-12-19 32.4
31 2014-09-30 3000.0 259.2 2014-12-19 36.2
32 2014-09-30 3025.0 238.4 2014-12-19 40.4
33 2014-09-30 3050.0 218.1 2014-12-19 45.1
34 2014-09-30 3075.0 198.5 2014-12-19 50.5
35 2014-09-30 3100.0 179.4 2014-12-19 56.4
36 2014-09-30 3125.0 161.1 2014-12-19 63.1
37 2014-09-30 3150.0 143.5 2014-12-19 70.5
38 2014-09-30 3175.0 126.8 2014-12-19 78.8
39 2014-09-30 3200.0 110.9 2014-12-19 87.9
40 2014-09-30 3225.0 96.1 2014-12-19 98.1
41 2014-09-30 3250.0 82.3 2014-12-19 109.3
42 2014-09-30 3275.0 69.6 2014-12-19 121.6
43 2014-09-30 3300.0 58.1 2014-12-19 135.1
44 2014-09-30 3325.0 47.8 2014-12-19 149.8
45 2014-09-30 3350.0 38.8 2014-12-19 165.8
46 2014-09-30 3375.0 31.0 2014-12-19 183.0
47 2014-09-30 3400.0 24.4 2014-12-19 201.4
48 2014-09-30 3425.0 18.9 2014-12-19 221.0
49 2014-09-30 3450.0 14.5 2014-12-19 241.5
50 2014-09-30 3475.0 10.9 2014-12-19 262.9
51 2014-09-30 3500.0 8.0 2014-12-19 285.1
52 2014-09-30 3525.0 5.9 2014-12-19 307.9
53 2014-09-30 3550.0 4.3 2014-12-19 331.3
54 2014-09-30 3575.0 3.1 2014-12-19 355.1
55 2014-09-30 3600.0 2.2 2014-12-19 379.2
56 2014-09-30 3625.0 1.6 2014-12-19 403.6
57 2014-09-30 3650.0 1.1 2014-12-19 428.1
58 2014-09-30 3700.0 0.6 2014-12-19 477.6
59 2014-09-30 1000.0 2128.6 2015-12-18 0.7
60 2014-09-30 1100.0 2029.1 2015-12-18 1.1
61 2014-09-30 1200.0 1929.6 2015-12-18 1.7
62 2014-09-30 1300.0 1830.4 2015-12-18 2.5
63 2014-09-30 1400.0 1731.4 2015-12-18 3.4
64 2014-09-30 1500.0 1632.7 2015-12-18 4.7
65 2014-09-30 1600.0 1534.4 2015-12-18 6.4
66 2014-09-30 1700.0 1436.6 2015-12-18 8.6
67 2014-09-30 1750.0 1388.0 2015-12-18 9.9
68 2014-09-30 1800.0 1339.5 2015-12-18 11.5
69 2014-09-30 1850.0 1291.2 2015-12-18 13.2
70 2014-09-30 1900.0 1243.2 2015-12-18 15.1
71 2014-09-30 1950.0 1195.4 2015-12-18 17.3
72 2014-09-30 2000.0 1147.8 2015-12-18 19.7
73 2014-09-30 2050.0 1100.6 2015-12-18 22.5
74 2014-09-30 2100.0 1053.7 2015-12-18 25.6
75 2014-09-30 2150.0 1007.1 2015-12-18 29.0
76 2014-09-30 2200.0 961.0 2015-12-18 32.8
77 2014-09-30 2250.0 915.3 2015-12-18 37.1
78 2014-09-30 2300.0 870.1 2015-12-18 41.9
79 2014-09-30 2350.0 825.4 2015-12-18 47.3
80 2014-09-30 2400.0 781.4 2015-12-18 53.2
81 2014-09-30 2450.0 738.0 2015-12-18 59.8
82 2014-09-30 2500.0 695.5 2015-12-18 67.3
83 2014-09-30 2550.0 653.6 2015-12-18 75.4
84 2014-09-30 2600.0 612.9 2015-12-18 84.7
85 2014-09-30 2650.0 573.2 2015-12-18 94.9
86 2014-09-30 2700.0 534.5 2015-12-18 106.2
87 2014-09-30 2750.0 496.9 2015-12-18 118.6
88 2014-09-30 2800.0 460.3 2015-12-18 132.1
89 2014-09-30 2850.0 425.0 2015-12-18 146.6
90 2014-09-30 2900.0 390.5 2015-12-18 162.2
91 2014-09-30 2950.0 357.6 2015-12-18 179.3
92 2014-09-30 3000.0 325.8 2015-12-18 197.7
93 2014-09-30 3050.0 295.7 2015-12-18 217.3
94 2014-09-30 3100.0 267.2 2015-12-18 238.8
95 2014-09-30 3150.0 240.1 2015-12-18 261.7
96 2014-09-30 3200.0 214.7 2015-12-18 286.4
97 2014-09-30 3250.0 191.1 2015-12-18 312.9
98 2014-09-30 3300.0 169.3 2015-12-18 340.9
99 2014-09-30 3350.0 149.1 2015-12-18 370.7
100 2014-09-30 3400.0 130.7 2015-12-18 402.3
101 2014-09-30 3450.0 113.9 2015-12-18 435.5
102 2014-09-30 3500.0 98.7 2015-12-18 470.3
103 2014-09-30 3550.0 85.1 2015-12-18 506.7
104 2014-09-30 3600.0 72.9 2015-12-18 544.4
105 2014-09-30 3650.0 62.0 2015-12-18 583.6
106 2014-09-30 3700.0 52.4 2015-12-18 624.0
107 2014-09-30 3750.0 44.0 2015-12-18 665.5
108 2014-09-30 3800.0 36.6 2015-12-18 708.2
109 2014-09-30 3850.0 30.3 2015-12-18 751.8
110 2014-09-30 3900.0 24.9 2015-12-18 796.4
111 2014-09-30 3950.0 20.4 2015-12-18 841.8
112 2014-09-30 4000.0 16.6 2015-12-18 888.0
113 2014-09-30 4100.0 10.8 2015-12-18 982.3
114 2014-09-30 4200.0 7.0 2015-12-18 1078.4
115 2014-09-30 4300.0 4.5 2015-12-18 1175.9
116 2014-09-30 4400.0 2.9 2015-12-18 1274.2
117 2014-09-30 4500.0 1.8 2015-12-18 1373.2
118 2014-09-30 4600.0 1.1 2015-12-18 1472.5
119 2014-09-30 4700.0 0.7 2015-12-18 1572.1
120 2014-09-30 4800.0 0.5 2015-12-18 1671.8
121 2014-09-30 700.0 2337.6 2016-12-16 0.5
122 2014-09-30 800.0 2238.1 2016-12-16 0.9
123 2014-09-30 900.0 2138.9 2016-12-16 1.7
124 2014-09-30 1000.0 2040.1 2016-12-16 2.7
125 2014-09-30 1100.0 1941.6 2016-12-16 4.2
126 2014-09-30 1200.0 1843.7 2016-12-16 6.3
127 2014-09-30 1300.0 1746.4 2016-12-16 8.9
128 2014-09-30 1400.0 1649.8 2016-12-16 12.2
129 2014-09-30 1500.0 1554.0 2016-12-16 16.4
130 2014-09-30 1600.0 1459.2 2016-12-16 21.6
131 2014-09-30 1700.0 1365.6 2016-12-16 27.9
132 2014-09-30 1800.0 1273.3 2016-12-16 35.5
133 2014-09-30 1900.0 1182.5 2016-12-16 44.7
134 2014-09-30 2000.0 1093.6 2016-12-16 55.7
135 2014-09-30 2100.0 1006.8 2016-12-16 68.8
136 2014-09-30 2150.0 964.3 2016-12-16 76.3
137 2014-09-30 2200.0 922.4 2016-12-16 84.4
138 2014-09-30 2250.0 881.2 2016-12-16 93.2
139 2014-09-30 2300.0 840.8 2016-12-16 102.6
140 2014-09-30 2350.0 801.2 2016-12-16 113.0
141 2014-09-30 2400.0 762.2 2016-12-16 124.0
142 2014-09-30 2450.0 724.0 2016-12-16 135.9
143 2014-09-30 2500.0 686.8 2016-12-16 148.7
144 2014-09-30 2550.0 650.7 2016-12-16 162.5
145 2014-09-30 2600.0 615.3 2016-12-16 177.1
146 2014-09-30 2650.0 581.1 2016-12-16 192.9
147 2014-09-30 2700.0 547.8 2016-12-16 209.5
148 2014-09-30 2750.0 515.6 2016-12-16 227.3
149 2014-09-30 2800.0 484.3 2016-12-16 246.0
150 2014-09-30 2850.0 454.4 2016-12-16 266.0
151 2014-09-30 2900.0 425.4 2016-12-16 287.0
152 2014-09-30 2950.0 397.8 2016-12-16 309.1
153 2014-09-30 3000.0 370.9 2016-12-16 332.4
154 2014-09-30 3050.0 345.6 2016-12-16 357.0
155 2014-09-30 3100.0 321.2 2016-12-16 382.6
156 2014-09-30 3150.0 298.1 2016-12-16 409.6
157 2014-09-30 3200.0 276.2 2016-12-16 437.5
158 2014-09-30 3250.0 255.2 2016-12-16 466.7
159 2014-09-30 3300.0 235.7 2016-12-16 497.0
160 2014-09-30 3350.0 217.0 2016-12-16 528.3
161 2014-09-30 3400.0 199.7 2016-12-16 560.9
162 2014-09-30 3450.0 183.2 2016-12-16 594.4
163 2014-09-30 3500.0 167.9 2016-12-16 629.0
164 2014-09-30 3550.0 153.6 2016-12-16 664.8
165 2014-09-30 3600.0 140.2 2016-12-16 701.4
166 2014-09-30 3650.0 127.7 2016-12-16 738.8
167 2014-09-30 3700.0 116.3 2016-12-16 777.3
168 2014-09-30 3750.0 105.8 2016-12-16 816.8
169 2014-09-30 3800.0 95.9 2016-12-16 857.0
170 2014-09-30 3850.0 86.9 2016-12-16 897.9
171 2014-09-30 3900.0 78.5 2016-12-16 939.6
172 2014-09-30 3950.0 71.1 2016-12-16 982.1
173 2014-09-30 4000.0 64.1 2016-12-16 1025.0
174 2014-09-30 4100.0 52.0 2016-12-16 1112.9
175 2014-09-30 4200.0 42.0 2016-12-16 1202.8
176 2014-09-30 4300.0 33.8 2016-12-16 1294.5
177 2014-09-30 4400.0 27.1 2016-12-16 1387.7
178 2014-09-30 4500.0 21.6 2016-12-16 1482.2
179 2014-09-30 4600.0 17.2 2016-12-16 1577.8
180 2014-09-30 4700.0 13.7 2016-12-16 1674.2
181 2014-09-30 4800.0 10.9 2016-12-16 1771.3
182 2014-09-30 4900.0 8.6 2016-12-16 1869.0
183 2014-09-30 5000.0 6.8 2016-12-16 1967.1
184 2014-09-30 5200.0 4.2 2016-12-16 2164.4
185 2014-09-30 5400.0 2.6 2016-12-16 2362.7
186 2014-09-30 5600.0 1.6 2016-12-16 2561.6
187 2014-09-30 5800.0 1.0 2016-12-16 2760.8
188 2014-09-30 6000.0 0.6 2016-12-16 2960.3
189 2014-09-30 1200.0 1946.1 2015-06-19 0.5
190 2014-09-30 1250.0 1896.2 2015-06-19 0.6
191 2014-09-30 1300.0 1846.4 2015-06-19 0.8
192 2014-09-30 1350.0 1796.5 2015-06-19 0.9
193 2014-09-30 1400.0 1746.7 2015-06-19 1.1
194 2014-09-30 1450.0 1697.0 2015-06-19 1.4
195 2014-09-30 1500.0 1647.2 2015-06-19 1.6
196 2014-09-30 1550.0 1597.6 2015-06-19 1.9
197 2014-09-30 1600.0 1547.9 2015-06-19 2.3
198 2014-09-30 1650.0 1498.3 2015-06-19 2.7
199 2014-09-30 1700.0 1448.8 2015-06-19 3.2
200 2014-09-30 1750.0 1399.3 2015-06-19 3.7
201 2014-09-30 1800.0 1349.9 2015-06-19 4.3
202 2014-09-30 1850.0 1300.6 2015-06-19 5.0
203 2014-09-30 1900.0 1251.4 2015-06-19 5.8
204 2014-09-30 1950.0 1202.3 2015-06-19 6.7
205 2014-09-30 2000.0 1153.3 2015-06-19 7.7
206 2014-09-30 2050.0 1104.5 2015-06-19 8.9
207 2014-09-30 2100.0 1055.9 2015-06-19 10.3
208 2014-09-30 2150.0 1007.6 2015-06-19 11.9
209 2014-09-30 2200.0 959.4 2015-06-19 13.8
210 2014-09-30 2250.0 911.6 2015-06-19 16.0
211 2014-09-30 2300.0 864.1 2015-06-19 18.5
212 2014-09-30 2350.0 817.1 2015-06-19 21.4
213 2014-09-30 2400.0 770.4 2015-06-19 24.8
214 2014-09-30 2450.0 724.3 2015-06-19 28.7
215 2014-09-30 2500.0 678.7 2015-06-19 33.1
216 2014-09-30 2550.0 633.8 2015-06-19 38.2
217 2014-09-30 2600.0 589.6 2015-06-19 44.0
218 2014-09-30 2650.0 546.2 2015-06-19 50.6
219 2014-09-30 2700.0 503.7 2015-06-19 58.1
220 2014-09-30 2750.0 462.2 2015-06-19 66.6
221 2014-09-30 2800.0 421.8 2015-06-19 76.2
222 2014-09-30 2850.0 382.6 2015-06-19 87.0
223 2014-09-30 2900.0 344.8 2015-06-19 99.1
224 2014-09-30 2950.0 308.5 2015-06-19 112.9
225 2014-09-30 3000.0 274.0 2015-06-19 128.3
226 2014-09-30 3050.0 241.3 2015-06-19 145.7
227 2014-09-30 3100.0 210.8 2015-06-19 165.2
228 2014-09-30 3150.0 182.4 2015-06-19 186.8
229 2014-09-30 3200.0 156.4 2015-06-19 210.6
230 2014-09-30 3250.0 132.4 2015-06-19 236.8
231 2014-09-30 3300.0 110.9 2015-06-19 265.3
232 2014-09-30 3350.0 91.7 2015-06-19 296.1
233 2014-09-30 3400.0 74.8 2015-06-19 329.1
234 2014-09-30 3450.0 60.1 2015-06-19 364.4
235 2014-09-30 3500.0 47.6 2015-06-19 401.9
236 2014-09-30 3550.0 37.1 2015-06-19 441.4
237 2014-09-30 3600.0 28.5 2015-06-19 482.9
238 2014-09-30 3650.0 21.6 2015-06-19 526.0
239 2014-09-30 3700.0 16.2 2015-06-19 570.5
240 2014-09-30 3750.0 12.0 2015-06-19 616.3
241 2014-09-30 3800.0 8.8 2015-06-19 663.2
242 2014-09-30 3850.0 6.4 2015-06-19 710.8
243 2014-09-30 3900.0 4.7 2015-06-19 759.0
244 2014-09-30 3950.0 3.3 2015-06-19 807.7
245 2014-09-30 4000.0 2.4 2015-06-19 856.7
246 2014-09-30 4100.0 1.2 2015-06-19 955.5
247 2014-09-30 4200.0 0.6 2015-06-19 1054.9
248 2014-09-30 800.0 2248.7 2016-06-17 0.6
249 2014-09-30 900.0 2149.2 2016-06-17 1.1
250 2014-09-30 1000.0 2050.0 2016-06-17 1.9
251 2014-09-30 1500.0 1559.5 2016-06-17 11.2
252 2014-09-30 1600.0 1463.1 2016-06-17 14.8
253 2014-09-30 1700.0 1367.6 2016-06-17 19.2
254 2014-09-30 1800.0 1273.0 2016-06-17 24.6
255 2014-09-30 1850.0 1226.2 2016-06-17 27.8
256 2014-09-30 1900.0 1179.8 2016-06-17 31.3
257 2014-09-30 1950.0 1133.6 2016-06-17 35.2
258 2014-09-30 2000.0 1088.0 2016-06-17 39.5
259 2014-09-30 2050.0 1042.7 2016-06-17 44.2
260 2014-09-30 2100.0 998.0 2016-06-17 49.5
261 2014-09-30 2150.0 953.8 2016-06-17 55.2
262 2014-09-30 2200.0 910.1 2016-06-17 61.6
263 2014-09-30 2250.0 867.1 2016-06-17 68.6
264 2014-09-30 2300.0 824.8 2016-06-17 76.2
265 2014-09-30 2350.0 783.1 2016-06-17 84.5
266 2014-09-30 2400.0 742.2 2016-06-17 93.6
267 2014-09-30 2450.0 702.2 2016-06-17 103.5
268 2014-09-30 2500.0 662.8 2016-06-17 114.2
269 2014-09-30 2550.0 624.4 2016-06-17 125.8
270 2014-09-30 2600.0 586.9 2016-06-17 138.3
271 2014-09-30 2650.0 550.5 2016-06-17 151.8
272 2014-09-30 2700.0 515.1 2016-06-17 166.2
273 2014-09-30 2750.0 480.7 2016-06-17 182.0
274 2014-09-30 2800.0 447.7 2016-06-17 198.9
275 2014-09-30 2850.0 415.7 2016-06-17 216.9
276 2014-09-30 2900.0 385.2 2016-06-17 236.5
277 2014-09-30 2950.0 355.9 2016-06-17 257.0
278 2014-09-30 3000.0 327.9 2016-06-17 279.1
279 2014-09-30 3050.0 301.4 2016-06-17 302.6
280 2014-09-30 3100.0 276.3 2016-06-17 327.4
281 2014-09-30 3150.0 252.5 2016-06-17 353.6
282 2014-09-30 3200.0 230.1 2016-06-17 381.2
283 2014-09-30 3250.0 208.9 2016-06-17 410.2
284 2014-09-30 3300.0 189.3 2016-06-17 440.4
285 2014-09-30 3350.0 170.9 2016-06-17 472.0
286 2014-09-30 3400.0 153.9 2016-06-17 504.9
287 2014-09-30 3450.0 138.2 2016-06-17 539.2
288 2014-09-30 3500.0 123.7 2016-06-17 574.7
289 2014-09-30 3550.0 110.4 2016-06-17 611.3
290 2014-09-30 3600.0 98.3 2016-06-17 649.2
291 2014-09-30 3650.0 87.2 2016-06-17 688.0
292 2014-09-30 3700.0 77.2 2016-06-17 728.0
293 2014-09-30 3750.0 68.1 2016-06-17 768.9
294 2014-09-30 3800.0 59.9 2016-06-17 810.7
295 2014-09-30 3850.0 52.5 2016-06-17 853.3
296 2014-09-30 3900.0 45.9 2016-06-17 896.7
297 2014-09-30 3950.0 40.0 2016-06-17 940.8
298 2014-09-30 4000.0 34.7 2016-06-17 985.5
299 2014-09-30 1400.0 1817.2 2015-03-20 0.5
300 2014-09-30 1450.0 1767.3 2015-03-20 0.5
301 2014-09-30 1500.0 1717.4 2015-03-20 0.7
302 2014-09-30 1550.0 1667.5 2015-03-20 0.8
303 2014-09-30 1600.0 1617.7 2015-03-20 0.9
304 2014-09-30 1650.0 1567.8 2015-03-20 1.1
305 2014-09-30 1700.0 1518.0 2015-03-20 1.3
306 2014-09-30 1750.0 1468.2 2015-03-20 1.5
307 2014-09-30 1800.0 1418.5 2015-03-20 1.8
308 2014-09-30 1850.0 1368.8 2015-03-20 2.1
309 2014-09-30 1900.0 1319.2 2015-03-20 2.4
310 2014-09-30 1950.0 1269.6 2015-03-20 2.8
311 2014-09-30 2000.0 1220.0 2015-03-20 3.3
312 2014-09-30 2050.0 1170.6 2015-03-20 3.9
313 2014-09-30 2100.0 1121.2 2015-03-20 4.5
314 2014-09-30 2150.0 1071.9 2015-03-20 5.2
315 2014-09-30 2200.0 1022.8 2015-03-20 6.0
316 2014-09-30 2250.0 973.7 2015-03-20 7.0
317 2014-09-30 2300.0 924.9 2015-03-20 8.1
318 2014-09-30 2350.0 876.2 2015-03-20 9.5
319 2014-09-30 2400.0 827.8 2015-03-20 11.1
320 2014-09-30 2450.0 779.7 2015-03-20 12.9
321 2014-09-30 2500.0 731.9 2015-03-20 15.2
322 2014-09-30 2550.0 684.5 2015-03-20 17.8
323 2014-09-30 2600.0 637.6 2015-03-20 20.9
324 2014-09-30 2650.0 591.2 2015-03-20 24.4
325 2014-09-30 2700.0 545.4 2015-03-20 28.6
326 2014-09-30 2750.0 500.3 2015-03-20 33.6
327 2014-09-30 2800.0 456.2 2015-03-20 39.5
328 2014-09-30 2825.0 434.5 2015-03-20 42.8
329 2014-09-30 2850.0 413.1 2015-03-20 46.4
330 2014-09-30 2875.0 392.0 2015-03-20 50.3
331 2014-09-30 2900.0 371.2 2015-03-20 54.5
332 2014-09-30 2925.0 350.8 2015-03-20 59.1
333 2014-09-30 2950.0 330.7 2015-03-20 64.0
334 2014-09-30 2975.0 311.0 2015-03-20 69.3
335 2014-09-30 3000.0 291.7 2015-03-20 75.0
336 2014-09-30 3025.0 272.8 2015-03-20 81.1
337 2014-09-30 3050.0 254.5 2015-03-20 87.8
338 2014-09-30 3075.0 236.6 2015-03-20 94.9
339 2014-09-30 3100.0 219.3 2015-03-20 102.6
340 2014-09-30 3125.0 202.6 2015-03-20 110.8
341 2014-09-30 3150.0 186.4 2015-03-20 119.7
342 2014-09-30 3175.0 171.0 2015-03-20 129.2
343 2014-09-30 3200.0 156.1 2015-03-20 139.4
344 2014-09-30 3225.0 142.0 2015-03-20 150.3
345 2014-09-30 3250.0 128.5 2015-03-20 161.8
346 2014-09-30 3275.0 115.8 2015-03-20 174.0
347 2014-09-30 3300.0 103.7 2015-03-20 187.0
348 2014-09-30 3325.0 92.5 2015-03-20 200.8
349 2014-09-30 3350.0 82.0 2015-03-20 215.3
350 2014-09-30 3375.0 72.2 2015-03-20 230.5
351 2014-09-30 3400.0 63.3 2015-03-20 246.6
352 2014-09-30 3425.0 55.1 2015-03-20 263.4
353 2014-09-30 3450.0 47.7 2015-03-20 281.0
354 2014-09-30 3475.0 41.0 2015-03-20 299.3
355 2014-09-30 3500.0 35.1 2015-03-20 318.4
356 2014-09-30 3525.0 29.8 2015-03-20 338.1
357 2014-09-30 3550.0 25.2 2015-03-20 358.4
358 2014-09-30 3575.0 21.1 2015-03-20 379.4
359 2014-09-30 3600.0 17.6 2015-03-20 400.8
360 2014-09-30 3625.0 14.5 2015-03-20 422.8
361 2014-09-30 3650.0 11.9 2015-03-20 445.2
362 2014-09-30 3700.0 7.9 2015-03-20 491.2
363 2014-09-30 3750.0 5.1 2015-03-20 538.4
364 2014-09-30 3800.0 3.2 2015-03-20 586.5
365 2014-09-30 3850.0 2.0 2015-03-20 635.3
366 2014-09-30 3900.0 1.3 2015-03-20 684.6
367 2014-09-30 3950.0 0.8 2015-03-20 734.1
368 2014-09-30 4000.0 0.5 2015-03-20 783.8
369 2014-09-30 2025.0 1202.2 2014-11-21 0.5
370 2014-09-30 2050.0 1177.3 2014-11-21 0.5
371 2014-09-30 2075.0 1152.3 2014-11-21 0.6
372 2014-09-30 2100.0 1127.3 2014-11-21 0.6
373 2014-09-30 2125.0 1102.4 2014-11-21 0.7
374 2014-09-30 2150.0 1077.5 2014-11-21 0.7
375 2014-09-30 2175.0 1052.5 2014-11-21 0.8
376 2014-09-30 2200.0 1027.6 2014-11-21 0.9
377 2014-09-30 2225.0 1002.7 2014-11-21 0.9
378 2014-09-30 2250.0 977.8 2014-11-21 1.0
379 2014-09-30 2275.0 952.8 2014-11-21 1.1
380 2014-09-30 2300.0 928.0 2014-11-21 1.2
381 2014-09-30 2325.0 903.1 2014-11-21 1.3
382 2014-09-30 2350.0 878.2 2014-11-21 1.4
383 2014-09-30 2375.0 853.3 2014-11-21 1.6
384 2014-09-30 2400.0 828.4 2014-11-21 1.7
385 2014-09-30 2425.0 803.6 2014-11-21 1.9
386 2014-09-30 2450.0 778.8 2014-11-21 2.0
387 2014-09-30 2475.0 753.9 2014-11-21 2.2
388 2014-09-30 2500.0 729.1 2014-11-21 2.4
389 2014-09-30 2525.0 704.3 2014-11-21 2.6
390 2014-09-30 2550.0 679.6 2014-11-21 2.8
391 2014-09-30 2575.0 654.9 2014-11-21 3.1
392 2014-09-30 2600.0 630.2 2014-11-21 3.4
393 2014-09-30 2625.0 605.5 2014-11-21 3.8
394 2014-09-30 2650.0 580.9 2014-11-21 4.2
395 2014-09-30 2675.0 556.3 2014-11-21 4.6
396 2014-09-30 2700.0 531.8 2014-11-21 5.1
397 2014-09-30 2725.0 507.4 2014-11-21 5.6
398 2014-09-30 2750.0 483.0 2014-11-21 6.3
399 2014-09-30 2775.0 458.7 2014-11-21 7.0
400 2014-09-30 2800.0 434.5 2014-11-21 7.8
401 2014-09-30 2825.0 410.5 2014-11-21 8.8
402 2014-09-30 2850.0 386.6 2014-11-21 9.9
403 2014-09-30 2875.0 362.8 2014-11-21 11.1
404 2014-09-30 2900.0 339.3 2014-11-21 12.6
405 2014-09-30 2925.0 316.0 2014-11-21 14.2
406 2014-09-30 2950.0 292.9 2014-11-21 16.2
407 2014-09-30 2975.0 270.2 2014-11-21 18.4
408 2014-09-30 3000.0 247.8 2014-11-21 21.1
409 2014-09-30 3025.0 225.9 2014-11-21 24.2
410 2014-09-30 3050.0 204.5 2014-11-21 27.8
411 2014-09-30 3075.0 183.7 2014-11-21 32.0
412 2014-09-30 3100.0 163.6 2014-11-21 36.9
413 2014-09-30 3125.0 144.3 2014-11-21 42.5
414 2014-09-30 3150.0 125.8 2014-11-21 49.1
415 2014-09-30 3175.0 108.3 2014-11-21 56.6
416 2014-09-30 3200.0 91.9 2014-11-21 65.2
417 2014-09-30 3225.0 76.7 2014-11-21 75.0
418 2014-09-30 3250.0 62.8 2014-11-21 86.1
419 2014-09-30 3275.0 50.4 2014-11-21 98.7
420 2014-09-30 3300.0 39.6 2014-11-21 112.9
421 2014-09-30 3325.0 30.3 2014-11-21 128.6
422 2014-09-30 3350.0 22.6 2014-11-21 145.9
423 2014-09-30 3375.0 16.4 2014-11-21 164.7
424 2014-09-30 3400.0 11.6 2014-11-21 184.9
425 2014-09-30 3425.0 8.0 2014-11-21 206.3
426 2014-09-30 3450.0 5.4 2014-11-21 228.7
427 2014-09-30 3475.0 3.5 2014-11-21 251.8
428 2014-09-30 3500.0 2.3 2014-11-21 275.6
429 2014-09-30 3525.0 1.5 2014-11-21 299.8
430 2014-09-30 3550.0 1.0 2014-11-21 324.2
431 2014-09-30 3575.0 0.6 2014-11-21 348.9
432 2014-09-30 2575.0 658.2 2014-10-17 0.5
433 2014-09-30 2600.0 633.3 2014-10-17 0.5
434 2014-09-30 2625.0 608.3 2014-10-17 0.6
435 2014-09-30 2650.0 583.4 2014-10-17 0.6
436 2014-09-30 2675.0 558.5 2014-10-17 0.7
437 2014-09-30 2700.0 533.6 2014-10-17 0.8
438 2014-09-30 2725.0 508.7 2014-10-17 0.9
439 2014-09-30 2750.0 483.8 2014-10-17 1.0
440 2014-09-30 2775.0 458.9 2014-10-17 1.2
441 2014-09-30 2800.0 434.1 2014-10-17 1.3
442 2014-09-30 2825.0 409.3 2014-10-17 1.5
443 2014-09-30 2850.0 384.5 2014-10-17 1.8
444 2014-09-30 2875.0 359.8 2014-10-17 2.1
445 2014-09-30 2900.0 335.2 2014-10-17 2.4
446 2014-09-30 2925.0 310.6 2014-10-17 2.8
447 2014-09-30 2950.0 286.1 2014-10-17 3.4
448 2014-09-30 2975.0 261.8 2014-10-17 4.0
449 2014-09-30 3000.0 237.7 2014-10-17 4.9
450 2014-09-30 3025.0 213.8 2014-10-17 6.0
451 2014-09-30 3050.0 190.2 2014-10-17 7.4
452 2014-09-30 3075.0 167.0 2014-10-17 9.3
453 2014-09-30 3100.0 144.5 2014-10-17 11.7
454 2014-09-30 3125.0 122.7 2014-10-17 14.9
455 2014-09-30 3150.0 101.8 2014-10-17 19.1
456 2014-09-30 3175.0 82.3 2014-10-17 24.5
457 2014-09-30 3200.0 64.3 2014-10-17 31.5
458 2014-09-30 3225.0 48.3 2014-10-17 40.5
459 2014-09-30 3250.0 34.6 2014-10-17 51.8
460 2014-09-30 3275.0 23.5 2014-10-17 65.8
461 2014-09-30 3300.0 15.1 2014-10-17 82.3
462 2014-09-30 3325.0 9.1 2014-10-17 101.3
463 2014-09-30 3350.0 5.1 2014-10-17 122.4
464 2014-09-30 3375.0 2.8 2014-10-17 145.0
465 2014-09-30 3400.0 1.5 2014-10-17 168.7
466 2014-09-30 3425.0 0.8 2014-10-17 193.0
467 2014-09-30 1500.0 1644.3 2015-09-18 3.1
468 2014-09-30 2000.0 1154.6 2015-09-18 13.4
469 2014-09-30 2100.0 1058.8 2015-09-18 17.5
470 2014-09-30 2200.0 964.1 2015-09-18 22.8
471 2014-09-30 2300.0 870.9 2015-09-18 29.6
472 2014-09-30 2400.0 779.7 2015-09-18 38.4
473 2014-09-30 2500.0 690.8 2015-09-18 49.5
474 2014-09-30 2550.0 647.4 2015-09-18 56.0
475 2014-09-30 2600.0 604.7 2015-09-18 63.4
476 2014-09-30 2650.0 563.0 2015-09-18 71.6
477 2014-09-30 2700.0 522.2 2015-09-18 80.8
478 2014-09-30 2750.0 482.4 2015-09-18 91.1
479 2014-09-30 2800.0 443.9 2015-09-18 102.6
480 2014-09-30 2850.0 406.5 2015-09-18 115.2
481 2014-09-30 2900.0 370.6 2015-09-18 129.2
482 2014-09-30 2950.0 336.1 2015-09-18 144.7
483 2014-09-30 3000.0 303.1 2015-09-18 161.8
484 2014-09-30 3050.0 271.9 2015-09-18 180.5
485 2014-09-30 3100.0 242.3 2015-09-18 201.0
486 2014-09-30 3150.0 214.7 2015-09-18 223.3
487 2014-09-30 3200.0 188.9 2015-09-18 247.5
488 2014-09-30 3250.0 164.9 2015-09-18 273.4
489 2014-09-30 3300.0 143.0 2015-09-18 301.6
490 2014-09-30 3350.0 123.0 2015-09-18 331.7
491 2014-09-30 3400.0 104.9 2015-09-18 363.5
492 2014-09-30 3450.0 88.8 2015-09-18 397.4
493 2014-09-30 3500.0 74.5 2015-09-18 433.1
494 2014-09-30 3550.0 62.0 2015-09-18 470.6
495 2014-09-30 3600.0 51.2 2015-09-18 509.8
496 2014-09-30 3650.0 41.9 2015-09-18 550.5
497 2014-09-30 3700.0 34.0 2015-09-18 592.6
498 2014-09-30 3750.0 27.4 2015-09-18 635.9
499 2014-09-30 3800.0 21.8 2015-09-18 680.3
500 2014-09-30 3850.0 17.2 2015-09-18 725.7
501 2014-09-30 3900.0 13.4 2015-09-18 772.0
502 2014-09-30 3950.0 10.4 2015-09-18 818.9
#
# Analyzing Returns from Geometric Brownian Motion
# 03_stf/GBM_returns.py
#
# (c) Dr. Yves J. Hilpisch
# Derivatives Analytics with Python
#
import math
import numpy as np
import pandas as pd
import scipy.stats as scs
import statsmodels.api as sm
import matplotlib as mpl
import matplotlib.pyplot as plt
plt.style.use('seaborn')
mpl.rcParams['font.family'] = 'serif'
#
# Helper Function
#
def dN(x, mu, sigma):
''' Probability density function of a normal random variable x.
Parameters
==========
mu : float
expected value
sigma : float
standard deviation
Returns
=======
pdf : float
value of probability density function
'''
z = (x - mu) / sigma
pdf = np.exp(-0.5 * z ** 2) / math.sqrt(2 * math.pi * sigma ** 2)
return pdf
# Return Sample Statistics and Normality Tests
def print_statistics(data):
print("RETURN SAMPLE STATISTICS")
print("---------------------------------------------")
print("Mean of Daily Log Returns %9.6f" % np.mean(data['returns']))
print("Std of Daily Log Returns %9.6f" % np.std(data['returns']))
print("Mean of Annua. Log Returns %9.6f" % (np.mean(data['returns']) * 252))
print("Std of Annua. Log Returns %9.6f" % \
(np.std(data['returns']) * math.sqrt(252)))
print("---------------------------------------------")
print("Skew of Sample Log Returns %9.6f" % scs.skew(data['returns']))
print("Skew Normal Test p-value %9.6f" % scs.skewtest(data['returns'])[1])
print("---------------------------------------------")
print("Kurt of Sample Log Returns %9.6f" % scs.kurtosis(data['returns']))
print("Kurt Normal Test p-value %9.6f" % \
scs.kurtosistest(data['returns'])[1])
print("---------------------------------------------")
print("Normal Test p-value %9.6f" % \
scs.normaltest(data['returns'])[1])
print("---------------------------------------------")
print("Realized Volatility %9.6f" % data['rea_vol'].iloc[-1])
print("Realized Variance %9.6f" % data['rea_var'].iloc[-1])
#
# Graphical Output
#
# daily quotes and log returns
def quotes_returns(data):
''' Plots quotes and returns. '''
plt.figure(figsize=(10, 6))
plt.subplot(211)
data['index'].plot()
plt.ylabel('daily quotes')
plt.grid(True)
plt.axis('tight')
plt.subplot(212)
data['returns'].plot()
plt.ylabel('daily log returns')
plt.grid(True)
plt.axis('tight')
# histogram of annualized daily log returns
def return_histogram(data):
''' Plots a histogram of the returns. '''
plt.figure(figsize=(10, 6))
x = np.linspace(min(data['returns']), max(data['returns']), 100)
plt.hist(np.array(data['returns']), bins=50, normed=True)
y = dN(x, np.mean(data['returns']), np.std(data['returns']))
plt.plot(x, y, linewidth=2)
plt.xlabel('log returns')
plt.ylabel('frequency/probability')
plt.grid(True)
# Q-Q plot of annualized daily log returns
def return_qqplot(data):
''' Generates a Q-Q plot of the returns.'''
plt.figure(figsize=(10, 6))
sm.qqplot(data['returns'], line='s')
plt.grid(True)
plt.xlabel('theoretical quantiles')
plt.ylabel('sample quantiles')
# realized volatility
def realized_volatility(data):
''' Plots the realized volatility. '''
plt.figure(figsize=(10, 6))
data['rea_vol'].plot()
plt.ylabel('realized volatility')
plt.grid(True)
# mean return, volatility and correlation (252 days moving = 1 year)
def rolling_statistics(data):
''' Calculates and plots rolling statistics (mean, std, correlation). '''
plt.figure(figsize=(11, 8))
plt.subplot(311)
mr = pd.rolling_mean(data['returns'], 252) * 252
mr.plot()
plt.grid(True)
plt.ylabel('returns (252d)')
plt.axhline(mr.mean(), color='r', ls='dashed', lw=1.5)
plt.subplot(312)
vo = pd.rolling_std(data['returns'], 252) * math.sqrt(252)
vo.plot()
plt.grid(True)
plt.ylabel('volatility (252d)')
plt.axhline(vo.mean(), color='r', ls='dashed', lw=1.5)
vx = plt.axis()
plt.subplot(313)
co = pd.rolling_corr(mr, vo, 252)
co.plot()
plt.grid(True)
plt.ylabel('correlation (252d)')
cx = plt.axis()
plt.axis([vx[0], vx[1], cx[2], cx[3]])
plt.axhline(co.mean(), color='r', ls='dashed', lw=1.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment