Derivatives Analytics With Python Numpy PDF
Derivatives Analytics With Python Numpy PDF
EuroPython 2011
Derivatives Analytics
EuroPython 2011
1 / 34
CV Yves Hilpisch
Derivatives Analytics
EuroPython 2011
2 / 34
Data Analysis
Time Series
Cross-Sectional Data
Derivatives Analytics
EuroPython 2011
3 / 34
Derivatives Analytics is concerned with the valuation, hedging and risk management
of derivative financial instruments
In contrast to ordinary financial instruments which may have an intrinsic value (like
the stock of a company), derivative instruments derive their values from other
instruments
Tyical tasks in this context are
simulation
data analysis (historical, current, simulated data)
discounting
arithmetic operations (summing, averaging, etc.)
linear algebra (vector and matrix operations, regression)
solving optimization problems
visualization
...
Python can do all this quite wellbut C, C++, C#, Matlab, VBA, JAVA and other
languages still dominate the financial services industry
Derivatives Analytics
EuroPython 2011
4 / 34
Open Source: Python and the majority of available libraries are completely open
source
Syntax: Python programming is easy to learn, the code is quite compact and in
general highly readable (= fast development + easy maintenance)
Libraries: nowadays, there is a wealth of powerful libraries available and the supply
grows steadily; there is hardly a problem which cannot be easily attacked with an
existing library
Derivatives Analytics
EuroPython 2011
5 / 34
in the London area (mainly financial services) the number of Python developer
contract offerings evolved as follows (respectively for the three months period ending
on 22 April)
142 in year 2009
245 in year 2010
644 in year 2011
these figures imply a more than fourfold demand for the Python skill in 2011 as
compared to 2009
over the same period, the average daily rate for contract work increased from 400
GBP to 475 GBP1
obviously, Python is catching up at a rapid pace in the financial services industry ...
Derivatives Analytics
EuroPython 2011
6 / 34
Data Analysis
Derivatives Analytics
EuroPython 2011
7 / 34
Data Analysis
Time Series
8000
DAX Daily Quotes
7000
6000
5000
4000
3000
2000
1992
1994
1996
1998
2000
2002
2004
2006
2008
2010
1992
1994
1996
1998
2000
2002
2004
2006
2008
2010
0.10
0.05
0.00
0.05
Derivatives Analytics
EuroPython 2011
8 / 34
Data Analysis
Time Series
from
from
from
from
from
Derivatives Analytics
EuroPython 2011
9 / 34
Data Analysis
Time Series
Derivatives Analytics
EuroPython 2011
10 / 34
Data Analysis
Time Series
0.8
0.6
0.4
0.2
0.0
0.2
0.4
0.6
0.8
1.01992
0.50
0.45
0.40
0.35
0.30
0.25
0.20
0.15
0.101992
1.0
1996
1998
2000
2002
2004
2006
2008
2010
1994
1996
1998
2000
2002
2004
2006
2008
2010
1994
1996
1998
2000
2002
2004
2006
2008
2010
0.5
0.0
0.5
1.01992
1994
Derivatives Analytics
EuroPython 2011
11 / 34
Data Analysis
Time Series
Derivatives Analytics
EuroPython 2011
12 / 34
Data Analysis
Cross-Sectional Data
21
20
Implied Volatility
19
18
17
16
15
14
13
12
7000
7200
7400
Strike
7600
7800
8000
Derivatives Analytics
EuroPython 2011
13 / 34
Model Economy
(1)
St index level at date t, r constant risk-less short rate, constant volatility of the
index and Zt standard Brownian motion
the process S generates the filtration F, i.e. Ft F(S0st )
a risk-less zero-coupon bond satisfies the DE
dBt
= rdt
Bt
(2)
the time t value of a zero-coupon bond paying one unit of currency at T with
0 t < T is Bt (T ) = er(T t)
Derivatives Analytics
EuroPython 2011
14 / 34
Model Economy
to simulate the financial model, i.e. to generate numerical values for St , the SDE
(1) has to be discretized
to this end, divide the given time interval [0, T ] in equidistant sub-intervals t such
that now t {0, t, 2t, ..., T }, i.e. there are M + 1 points in time with M T /t
a discrete version of the continuous time market model (1)(2) is
St
Stt
Bt
Btt
r 2
ert
t+
tzt
(3)
(4)
Derivatives Analytics
EuroPython 2011
15 / 34
European Options
1
I
h(ST,i )
i=1
Derivatives Analytics
EuroPython 2011
16 / 34
European Options
Average Drift
50
40
30
Jan 1, 2011
Dec 1, 2011
Source: analytics.dexision.com
Y. Hilpisch (Visixion GmbH)
Derivatives Analytics
EuroPython 2011
17 / 34
European Options
#
# Valuation of European Put Option
# by Monte Carlo Simulation
#
from numpy import *
from numpy . random import standard_normal , seed
from time import time
t0 = time ()
# # Parameters -- American Put Option
S0 = 36 .
# initial stock level
K = 40 .
# strike price
T = 1.0
# time - to - maturity
vol = 0 . 2
# volatility
r = 0 . 06
# short rate
# # Simulation Parameters
seed ( 150000 )
# seed for Python RNG
M = 50
# time steps
I = 50000
# simulation paths
dt = T / M
# length of time interval
df = exp ( - r * dt ) # discount factor per time interval
Derivatives Analytics
EuroPython 2011
18 / 34
European Options
# # Output
print " Time elapsed in Seconds
% 8 . 3f " %( time () - t0 )
print " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "
print " European Put Option Value % 8 . 3f " % V0
print " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "
Derivatives Analytics
EuroPython 2011
19 / 34
American Options
sup
{0,t,2t,...,T }
EQ
0 (B0 ( )h (S ))
(5)
it is well-known that the value of the American option at date t is then given by
(6)
i.e. the maximum of the payoff ht (s) of immediate exercise and the expected payoff
Ct (s) of not exercising; this quantity is given as
rt
Vt+t (St+t )|St = s)
Ct (s) = EQ
t (e
Derivatives Analytics
(7)
EuroPython 2011
20 / 34
American Options
problem: given a date t and a path i, you do not know the expected value in
(7)you only know the single simulated continuation value Yt,i
solution of Longstaff and Schwartz (2001): estimate the continuation values
Ct,i by ordinary least-squares regressiongiven the I simulated index levels St,i and
continuation values Yt,i (use cross section of simulated data at date t)
their algorithm is called Least Squares Monte Carlo (LSM)
Derivatives Analytics
EuroPython 2011
21 / 34
American Options
Source: analytics.dexision.com
Y. Hilpisch (Visixion GmbH)
Derivatives Analytics
EuroPython 2011
22 / 34
American Options
Source: analytics.dexision.com
Y. Hilpisch (Visixion GmbH)
Derivatives Analytics
EuroPython 2011
23 / 34
American Options
Derivatives Analytics
EuroPython 2011
24 / 34
American Options
# # Function Definitions
def RNG ( I ):
if AP == True :
ran = standard_no rm a l ( I / 2 )
ran = concatenate (( ran , - ran ))
else :
ran = standard_no rm a l ( I )
if MM == True :
ran = ran - mean ( ran )
ran = ran / std ( ran )
return ran
def GenS ( I ):
S = zeros (( M +1 , I ) , 'd ')
# index level matrix
S [0 ,:]= S0
# initial values
for t in range (1 , M +1 , 1 ):
# index level paths
ran = RNG ( I )
S [t ,:]= S [t -1 ,:]* exp (( r - vol ** 2 / 2 )* dt + vol * ran * sqrt ( dt ))
return S
def IV ( S ):
return maximum ( 40 . -S , 0 )
Derivatives Analytics
EuroPython 2011
25 / 34
American Options
# # Valuation by LSM
S = GenS ( I )
# generate stock price
h = IV ( S )
# inner value matrix
V = IV ( S )
# value matrix
for t in range (M -1 , -1 , - 1 ):
rg = polyfit ( S [t ,:] , V [ t +1 ,:]* df , reg )
#
C = polyval ( rg , S [t ,:])
#
V [t ,:]= where ( h [t ,:] > C , h [t ,:] , V [ t +1 ,:]* df ) #
V0 = sum ( V [0 ,:])/ I # LSM estimator
paths
regression at time t
continuation values
exercise decision
# # Output
print " Time elapsed in Seconds
% 8 . 3f " %( time () - t0 )
print " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "
print " Right Value
% 8 . 3f " % V0_right
print " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "
print " LSM Value for Am . Option % 8 . 3f " % V0
print " Absolute Error
% 8 . 3f " %( V0 - V0_right )
print " Relative Error in Percent % 8 . 3f " %(( V0 - V0_right )/ V0_right * 100 )
print " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "
Derivatives Analytics
EuroPython 2011
26 / 34
realistic market models generally include multiple sources of randomness which are
possibly correlated
the simulation of such complex models in combination with Least Squares Monte
Carlo is computationally demanding and time consuming
in their research paper, Medvedev and Scaillet (2009) analyze the valuation of
American put options in the presence of stochastic volatility and stochastic short
rates
Medvedev and Scaillet (2009) write on page 16:
To give an idea of the computational advantage of our method, a Matlab code
implementing the algorithm of Longstaff and Schwartz (2001) takes dozens of
minutes to compute a single option price while our approximation takes roughly a
tenth of a second.
Derivatives Analytics
EuroPython 2011
27 / 34
Derivatives Analytics
EuroPython 2011
28 / 34
Derivatives Analytics
EuroPython 2011
29 / 34
Capabilities
Example products:
plain vanilla options
American options
Asian options
options on baskets
certificates (bonus, express, etc.)
swaps, swaptions
real options
portfolios of options
life insurance contracts
Example underlyings:
indices
stocks
bonds
interest rates
currencies
commodities
Derivatives Analytics
EuroPython 2011
30 / 34
Capabilities
Derivatives Analytics
EuroPython 2011
31 / 34
Technology
Derivatives Analytics
EuroPython 2011
32 / 34
Technology
Derivatives Analytics
EuroPython 2011
33 / 34
Technology
Contact
Derivatives Analytics
EuroPython 2011
34 / 34