A Subjective and Biased Overview
Dr. Yves J. Hilpisch | The Python Quants GmbH
analytics@pythonquants.com | www.quant-platform.com
Pycon Ireland, 11. October 2014
Black-Scholes-Merton (1973) SDE of geometric Brownian motion.
$$ dS_t = rS_tdt + \sigma S_t dZ_t $$
Monte Carlo simulation: draw $I$ standard normally distributed random number $z_t^i$ and apply them to the following by Euler disctretization scheme to simulate $I$ end values of the GBM:
$$ S_{T} = S_0 \exp \left(\left( r - \frac{1}{2} \sigma^2\right) T + \sigma \sqrt{T} z_T \right) $$
Latex description of Euler discretization.
S_T = S_0 \exp (( r - 0.5 \sigma^2 ) T + \sigma \sqrt{T} z_T)
Python implementation of algorithm.
from pylab import *
S_0 = 100.; r = 0.01; T = 0.5; sigma = 0.2
z_T = standard_normal(10000)
S_T = S_0 * exp((r - 0.5 * sigma ** 2) * T + sigma * sqrt(T) * z_T)
Again, Latex for comparison:
S_T = S_0 \exp (( r - 0.5 \sigma^2 ) T + \sigma \sqrt{T} z_T)
Interactive visualization of simulation results.
%matplotlib inline
pyfig = figure()
hist(S_T, bins=40);
grid()
By others:
By us:
DX Analytics is a Python library for advanced financial and derivatives analytics written by The Python Quants. It is particularly suited to model multi-risk derivatives and to do a consistent valuation of portfolios of complex derivatives. It mainly uses Monte Carlo simulation since it is the only numerical method capable of valuing and risk managing complex, multi-risk derivatives books.
An example with an European maximum call option on two underlyings.
%%time
import dx
%run dx_example.py
# sets up market environments
# and defines derivative instrument
# calculates a number of numerical results
CPU times: user 4.9 s, sys: 335 ms, total: 5.24 s Wall time: 5.26 s
max_call.payoff_func
# payoff of a maximum call option
# on two underlyings (European exercise)
"np.maximum(np.maximum(maturity_value['gbm'], maturity_value['jd']) - 34., 0)"
max_call.vega('jd')
# numerical Vega with respect
# to one risk factor
4.194600000000115
A Vega surface for one risk factor with respect to the initial values of both risk factors.
dx.plot_greeks_3d([a_1, a_2, vega_gbm], ['gbm', 'jd', 'vega gbm'])
# Vega surface plot
From http://www.risk.net:
"Murex provides a complete cross-asset and front-to-back offering for structured products, combining out-of-the box complex payoffs and models with structuring tools, and model and products catalogue extensors.
Key features include:
We analyze the statistical correlation between the EURO STOXX 50 stock index and the VSTOXX volatility index.
First the EURO STOXX 50 data.
import pandas as pd
cols = ['Date', 'SX5P', 'SX5E', 'SXXP', 'SXXE',
'SXXF', 'SXXA', 'DK5F', 'DKXF', 'DEL']
es_url = 'http://www.stoxx.com/download/historical_values/hbrbcpe.txt'
try:
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']
except:
# read stored data if there is no Internet connection
es = pd.HDFStore('data/SX5E.h5', 'r')['SX5E']
Second, the VSTOXX data.
vs_url = 'http://www.stoxx.com/download/historical_values/h_vstoxx.txt'
try:
vs = pd.read_csv(vs_url, # filename
index_col=0, # index column (dates)
parse_dates=True, # parse date information
dayfirst=True, # day before month
header=2) # header/column names
except:
# read stored data if there is no Internet connection
vs = pd.HDFStore('data/V2TX.h5', 'r')['V2TX']
Generating log returns with Python and pandas.
import numpy as np
# log returns for the major indices' time series data
datv = pd.DataFrame({'SX5E' : es['SX5E'], 'V2TX': vs['V2TX']}).dropna()
rets = np.log(datv / datv.shift(1)).dropna()
ES = rets['SX5E'].values
VS = rets['V2TX'].values
Bridging to R from within IPython Notebook and pushing Python data to the R run-time.
%load_ext rpy2.ipython
The rpy2.ipython extension is already loaded. To reload it, use: %reload_ext rpy2.ipython
%Rpush ES VS
Plotting with R in IPython Notebook.
%R plot(ES, VS, pch=19, col='blue'); grid(); title("Log returns ES50 & VSTOXX")
Linear regression with R.
%R c = coef(lm(VS~ES))
<FloatVector - Python:0x10b247680 / R:0x10e9135c8> [-0.000057, -2.756117]
%R print(summary(lm(VS~ES)))
Call: lm(formula = VS ~ ES) Residuals: Min 1Q Median 3Q Max -0.32413 -0.02192 -0.00215 0.02018 0.53679 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -5.661e-05 6.160e-04 -0.092 0.927 ES -2.756e+00 4.073e-02 -67.661 <2e-16 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.03905 on 4016 degrees of freedom Multiple R-squared: 0.5327, Adjusted R-squared: 0.5326 F-statistic: 4578 on 1 and 4016 DF, p-value: < 2.2e-16
Regression line visualized.
%R plot(ES, VS, pch=19, col='blue'); grid(); abline(c, col='red', lwd=5)
Pulling data from R to Python and using it.
%Rpull c
plt.figure(figsize=(9, 6))
plt.plot(ES, VS, 'b.')
plt.plot(ES, c[0] + c[1] * ES, 'r', lw=3)
plt.grid(); plt.xlabel('ES'); plt.ylabel('VS')
<matplotlib.text.Text at 0x105ee14d0>
If you want to have it nicer, interactive and embeddable anywhere – use plot.ly
import plotly.plotly as ply
ply.sign_in('yves', 'token')
Let us generate a plot with a bit fewer data points.
pyfig = plt.figure(figsize=(9, 6)); n = 100
plt.plot(ES[:n], VS[:n], 'b.')
plt.plot(ES[:n], c[0] + c[1] * ES[:n], 'r', lw=3)
plt.grid(); plt.xlabel('ES'); plt.ylabel('VS')
<matplotlib.text.Text at 0x110d83f10>
Only single line of code needed to convert matplotlib plot into interactive D3 plot.
ply.iplot_mpl(pyfig) # convert mpl plot into interactive D3
Finance algorithms are loop-heavy; Python loops are slow; Python is too slow for finance.
def counting_py(N):
s = 0
for i in xrange(N):
for j in xrange(N):
s += int(cos(log(1)))
return s
N = 2000
%time counting_py(N)
# memory efficient but slow
CPU times: user 12.9 s, sys: 222 ms, total: 13.1 s Wall time: 13.1 s
4000000
First approach: vectorization with NumPy.
%%time
arr = ones((N, N))
print int(sum(cos(log(arr))))
4000000 CPU times: user 75.4 ms, sys: 44.8 ms, total: 120 ms Wall time: 119 ms
arr.nbytes # much faster but NOT memory efficient
32000000
Second approach: dynamic compiling with Numba.
import numba
counting_nb = numba.jit(counting_py)
%time counting_nb(N)
# some overhead the first time
CPU times: user 140 ms, sys: 12.8 ms, total: 153 ms Wall time: 144 ms
4000000
%timeit counting_nb(N)
# even faster AND memory efficient
10 loops, best of 3: 59.2 ms per loop
Hardware-bound IO operations are standard for Python.
%time one_gb = standard_normal((12500, 10000))
one_gb.nbytes
# a giga byte worth of data
CPU times: user 5.27 s, sys: 373 ms, total: 5.65 s Wall time: 5.65 s
1000000000
%time save('one_gb', one_gb)
CPU times: user 53.5 ms, sys: 1.72 s, total: 1.77 s Wall time: 2.23 s
!ls -n one_gb*
-rw-r--r-- 1 501 20 1000000080 10 Okt 19:11 one_gb.npy
!rm one_gb*
Integrating it all and adding collaboration and scalability (http://quant-platform.com).
At the moment, the Python Quant Platform comprises the following components and features:
rpy2
and IPython Notebook Working on the browser-based shell and using, for instance, IPython.
Or doing code editing with Vim, working with Git, etc.
Or checking in on resource usage with htop.
from IPython.display import HTML
HTML('<iframe src=http://analytics.quant-platform.com/trial/yves/ \
width=100% height=550></iframe>')
392 Python Jobs in Greenwich, CT – obviously a famous hedge fund place.
Automated Trading powerd by Python.
Trading and Risk Management powerd by Python.
Python-based tutorials by Eurex (http://www.eurexchange.com/vstoxx/).
By others:
By myself:
Available as ebook and from December 2015 as print version (currently 50% discount – see my Twitter account @dyjh).
Forthcoming 2015 at Wiley Finance ...
"The appendices present an implementation in Python of our experiments." (p. 3)
"Knowledge and Skills: Our graduates have working experience with C++, VBA, Python, R, and Matlab for financial applications. They share an exceptionally strong work ethic and possess excellent interpersonal, teamwork, and communication skills."
By others:
By myself:
HTML('<iframe src=http://quantshub.com/content/python-finance-yves-j-hilpisch \
width=100% height=550></iframe>')
HTML('<iframe src="http://quant-platform.com/conf/" \
width=100% height=650></iframe>')
My wish for Python in the future: to become THE glue language and platform for
Python has the potential to accomplish what MMA has done for the Martial Arts.
Please contact us if you have any questions or want to get involved in our Python community events.
Python Quant Platform | http://quant-platform.com
Derivatives Analytics with Python | Derivatives Analytics @ Wiley Finance
Python for Finance | Python for Finance @ O'Reilly