Professional Documents
Culture Documents
print-pdf#/
A Python-based Journey
Dr Yves J Hilpisch
1 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
About Me
2 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
Out[3]:
Out[4]:
3 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
Out[5]:
4 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
Out[6]:
5 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
I am an author (III).
Out[7]:
6 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
More information and further links under http://tpq.io (http://fpq.io) and http://hilpisch.com
(http://hilpisch.com).
7 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
Agenda
8 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
Go to http://derivatives-analytics-with-python.com (http://derivatives-analytics-
with-python.com) to �nd links to all the resources and Python codes (eg Quant Platform,
Github repository).
9 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
10 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
Let us �rst set the stage with standard normally distributed (pseudo-) random numbers ...
In [9]: a.mean()
Out[9]: 0.018485352902666726
In [10]: a.std()
Out[10]: 1.0084113501147707
11 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
... and a simulated geometric Brownian motion (GBM) path. We make the following
assumptions.
# simulation parameters
np.random.seed(250000)
gbm_dates = pd.DatetimeIndex(start='30-09-2004',
end='31-08-2015',
freq='B')
M = len(gbm_dates) # time steps
dt = 1 / 252. # fixed for simplicity
df = math.exp(-r * dt) # discount factor
12 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
13 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
14 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
15 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
A histogram of the log returns compared to the normal distribution (with same mean/std).
In [15]: return_histogram(gbm)
16 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
In [16]: return_qqplot(gbm)
<matplotlib.figure.Figure at 0x7fc05f43b3d0>
17 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
In [17]: realized_volatility(gbm)
18 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
In [18]: rolling_statistics(gbm)
19 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
20 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
We work with historical DAX data. The following function equips us with the necessary time
series data.
def read_dax_data():
''' Reads historical DAX data from Yahoo! Finance, calculates log returns,
realized variance and volatility.'''
DAX = web.DataReader('^GDAXI', data_source='yahoo',
start='30-09-2004', end='31-08-2015')
DAX.rename(columns={'Adj Close' : 'index'}, inplace=True)
DAX['returns'] = np.log(DAX['index'] / DAX['index'].shift(1))
DAX['rea_var'] = 252 * np.cumsum(DAX['returns'] ** 2) / np.arange(len(DAX))
DAX['rea_vol'] = np.sqrt(DAX['rea_var'])
DAX = DAX.dropna()
return DAX
21 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
CPU times: user 123 ms, sys: 7.97 ms, total: 131 ms
Wall time: 427 ms
In [21]: print_statistics(DAX)
22 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
The (in-memory) data structure.
In [22]: DAX.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 2786 entries, 2004-10-01 to 2015-08-31
Data columns (total 9 columns):
Open 2786 non-null float64
High 2786 non-null float64
Low 2786 non-null float64
Close 2786 non-null float64
Volume 2786 non-null int64
index 2786 non-null float64
returns 2786 non-null float64
rea_var 2786 non-null float64
rea_vol 2786 non-null float64
dtypes: float64(8), int64(1)
memory usage: 217.7 KB
Out[23]:
index returns rea_var rea_vol
Date
2015-08-25 10128.120117 0.048521 0.048124 0.219371
2015-08-26 9997.429688 -0.012988 0.048122 0.219366
2015-08-27 10315.620117 0.031331 0.048193 0.219529
2015-08-28 10298.530273 -0.001658 0.048176 0.219491
2015-08-31 10259.459961 -0.003801 0.048160 0.219454
23 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
In [24]: quotes_returns(DAX)
24 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
A histogram of the log returns compared to the normal distribution (with same mean/std).
In [25]: return_histogram(DAX)
25 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
The QQ-plot.
In [26]: return_qqplot(DAX)
<matplotlib.figure.Figure at 0x7fc05f476190>
26 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
In [27]: realized_volatility(DAX)
27 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
In [28]: rolling_statistics(DAX)
28 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
Finally, we want to look for jumps (heuristically). We use this simple function.
29 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
Out[30]: 31
Out[31]: 0
30 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
negative jumps:
P (rn < −0.05) = 0.0002911
positive jumps:
P (rn > +0.05) = 0.0003402
for the DAX index given a return observation rn . In such a setting the number of return
observations lower than −0.05 and higher than +0.05 would be expected to be:
Out[32]: 0.8110046
Out[33]: 0.9477971999999999
31 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
32 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
Important add-on topic: volatility smiles and term structure — here implied volatilities from
European call options on the EURO STOXX 50 on 30. September 2014.
<matplotlib.figure.Figure at 0x7fc05ec7b750>
In [35]: data.tail()
Out[35]:
Date Strike Call Maturity Put
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
33 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
In [37]: plot_imp_vols(data)
34 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
35 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
36 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
37 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
We then know that the arbitrage value of an attainable European call option is
Ct = e−r(T−t) EQ
t (CT )
where q(s) is the risk-neutral probability density function (pdf) of ST . Unfortunately, the
pdf is quite often not known in closed form — whereas the characteristic function (CF) of ST
is.
The fundamental insight of Fourier-based option pricing is to replace both the pdf by the
CF and the call option payoff CT by its Fourier transform.
38 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
Let a random variable X be distributed with pdf q(x). The characteristic function q^ of X is
the Fourier transform of its pdf
∞
q^(u) ≡ ∫ eiux q(x)dx = EQ (eiuX )
−∞
39 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
= ur + iui with ui > 1, the Fourier transform of the European call option payoff
For u
CT = max[ST − K, 0]is given by:
K iu+1
Ĉ T (u) = − 2
u − iu
40 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
Lewis (2001): With φ as the CF of the rv ST and assuming ui ∈ (0, 1) , the call option
present value is
∞+iui
Ke−rT du
C0 = S0 − ∫ e−iuk φ(−u) 2
2π −∞+iui u − ui
= 0.5 gives
Furthermore, setting ui
−−−− ∞
√S0 K e−rT/2 dz
C0 = S0 − ∫ Re [eizk φ(z − i/2)] 2
π 0 z + 1/4
where Re[x] denotes the real part of x.
41 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
42 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
In the Merton (1976) jump-diffusion model, the risk-neutral index level dynamics are given
by the SDE
σ constant volatility of S
Zt standard Brownian motion
Jt jump at date t with distribution log(1 + Jt ) ≈ N (log(1 + μJ ) − δ2
2
, δ2)
N as the cumulative distribution function of a standard normal random variable
Nt Poisson process with intensity λ
43 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
The characteristic function for the Merton (1976) model is given as:
u2 σ 2
φM76
0 (u, T ) = exp((iuω − + λ (eiuμJ −u2 δ 2 /2
− 1)) T )
2
where the risk-neutral drift term ω takes on the form
σ2
− λ (eμJ +δ /2 − 1)
2
ω=r−
2
44 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
Combining this with the option pricing result from Lewis (2001) we get for the price of a
European call option
−−−− ∞
√S0 K e−rT/2 dz
C0 = S0 − ∫ Re [eizk φM76
0 (z − i/2, T )]
π 0 z 2 + 1/4
45 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
Let us implement European call option in Python. First, the characteristic function.
46 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
47 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
48 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
49 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
50 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
To value a European call option with strike price K by MCS consider the following
discretization of the Merton (1976) SDE
with the ztn being standard normally distributed and the yt being Poisson distributed with
intensity λ.
51 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
np.random.seed(10000)
rand1 = np.random.standard_normal(shape)
rand2 = np.random.standard_normal(shape)
rand3 = np.random.poisson(lamb * dt, shape)
52 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
53 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
54 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
In [47]: %%time
I = 200000
S = M76_generate_paths(S0, T, r, sigma, lamb, mu, delta, M, I)
print "Value of Call Option %8.3f" % M76_value_call_MCS(K)
55 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
56 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
57 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
In simple terms, the problem of calibration is to �nd parameters for the Merton (1976)
model such that observed market quotes of liquidly traded plain vanilla options are
replicated as good as possible. To this end, one de�nes an error function that is to be
minimized. Such a function could be the Root Mean Squared Error (RMSE). The task is then
to solve the problem
−−−− −−−−−−−−−−−−−−−−−−−−−
1
min ∑ (Cn∗ − CnM76 (σ, λ, μJ , δ))
N
2
σ,λ,μJ ,δ ⎷ N
n=1
with the Cn∗ being the market or input prices and the CnM76 being the model or output
prices for the options n = 1, . . . , N .
58 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
EXCURSION: The minimization problem is ill-posed (I). Let's analyze properties of the error
function for a single European call option.
59 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
60 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
In [51]: plot_error_function()
61 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
EXCURSION: The simple example illustrates that the calibration of the Merton (1976) jump
diffusion model leads to a number of problems:
62 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
Let us import some real option quotes for European call options on the EURO STOXX 50
index.
# Option Selection
tol = 0.05
options = data[(np.abs(data['Strike'] - S0) / S0) < tol]
mats = sorted(set(options['Maturity']))
options = options[options['Maturity'] == mats[0]]
63 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
In [53]: options
Out[53]:
Date Strike Call Maturity Put
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
64 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
65 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
66 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
In [56]: %%time
import scipy.optimize as sop
np.set_printoptions(suppress=True,
formatter={'all': lambda x: '%6.3f' % x})
p0 = sop.brute(M76_error_function, ((0.10, 0.201, 0.025),
(0.10, 0.80, 0.10), (-0.40, 0.01, 0.10),
(0.00, 0.121, 0.02)), finish=None)
67 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
In [57]: %%time
opt = sop.fmin(M76_error_function, p0, xtol=0.00001,
ftol=0.00001, maxiter=750, maxfun=1500)
68 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
In [58]: i = 0
M76_error_function(p0)
Out[58]: 0.9695219560421178
In [59]: i = 0
M76_error_function(opt)
Out[59]: 0.7665083562548708
69 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
70 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
71 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
72 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
73 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
Conclusions
markets: time series in �nancial markets strongly deviate from the Gaussian
benchmark(s); there are generally eg stochastic volatility, jumps, implied volatility
smiles observed in historical data
Merton (1976) model: the model of Merton is capable of accounting for some
observed stylized facts like jumps and volatility smiles
calibration issues: numerical �nance and optimization (i.e. calibration) faces a
number of issues, eg with regard the determinacy of solutions and convexity of
error functions
Python: Python is really close to mathematical and �nancial syntax; the
implementation of �nancial algorithms generally is ef�cient and performant (when
using the right libraries and idioms like NumPy with vectorization)
All details, codes, proofs, etc. in the book "Derivatives Analytics with Python" — cf.
http://derivatives-analytics-with-python.com (http://derivatives-analytics-
with-python.com).
74 of 75 08.04.2016 09:49
cqf_lecture slides http://tpq.io/cqf_lecture_apr_2016.html?print-pdf#/
75 of 75 08.04.2016 09:49