Derivatives Analytics With Python Numpy PDF
Derivatives Analytics With Python Numpy PDF
EuroPython 2011
Derivatives Analytics
EuroPython 2011
1 / 34
CV Yves Hilpisch
19931996 Dipl.-Kfm. (MBA) at Saarland University (Banks and Financial Markets) 19962000 Dr.rer.pol. (Ph.D.) at Saarland University (Mathematical Finance) 19972004 Management Consultant Financial Services & Insurance Industry 2005present Founder and MD of Visixion GmbH
management and technical consulting work DEXISIONDerivatives Analytics on Demand (www.dexision.com)
2 3 4
Derivatives Analytics
EuroPython 2011
2 / 34
Derivatives Analytics and Python Data Analysis Time Series Cross-Sectional Data Monte Carlo Simulation Model Economy European Options American Options Speed-up of 480+ Times DEXISIONOur Analytics Suite Capabilities Technology
Derivatives Analytics
EuroPython 2011
3 / 34
Derivatives Analytics is concerned with the valuation, hedging and risk management of derivative nancial instruments In contrast to ordinary nancial 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 nancial 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) Multi-Paradigm: Python is as good at functional programming as well as at object oriented programming Interpreted: Python is an interpreted language which makes rapid prototyping and development in general a bit more convenient 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 Speed: a common prejudice with regard to interpreted languagescompared to compiled ones like C++ or Cis the slow speed of code execution; however, nancial applications are more or less all about matrix/array manipulations and other operations which can be done at the speed of C code with the essential library Numpy
Derivatives Analytics
EuroPython 2011
5 / 34
in the London area (mainly nancial services) the number of Python developer contract oerings 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 gures 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 nancial services industry ...
Data Analysis
Data Analysis
Time Series
8000 7000 DAX Daily Quotes 6000 5000 4000 3000 2000 1992 0.10 DAX Daily Log Returns 0.05 0.00 0.05 1992 1994 1996 1998 2000 2002 2004 2006 2008 2010 1994 1996 1998 2000 2002 2004 2006 2008 2010
Data Analysis
Time Series
xlrd import open_workbook pandas import * datetime import * matplotlib . pyplot import * numpy import *
# DAX Open Workbook , Read xls = open_workbook ( ' D A X _ D a i l y _ 1 9 9 0 _ 2 0 1 1 . xls ' ) for s in xls . sheets (): datesDAX = []; quoteDAX = [] for row in range ( s . nrows -1 ,0 , - 1 ): year = int ( s . cell ( row , 0 ). value ) month = int ( s . cell ( row , 1 ). value ) day = int ( s . cell ( row , 2 ). value ) datesDAX . append ( date ( year , month , day )) quoteDAX . append ( float ( s . cell ( row , 8 ). value )) print
Derivatives Analytics
EuroPython 2011
9 / 34
Data Analysis
Time Series
DAXq = Series ( quoteDAX , index = datesDAX ) DAXr = Series ( log ( DAXq / DAXq . shift ( 1 )) , index = datesDAX ) DAXr = where ( isnull ( DAXr ) , 0 .0 , DAXr ) # Data Frames for Quotes and Returns data = { ' QUO ' : DAXq , ' RET ' : DAXr , ' RVO ' : rv } DAX = DataFrame ( data , index = DAXq . index ) # Graphical Output figure () subplot ( 211 ) plot ( DAX . index , DAX [ ' QUO ' ]) ylabel ( ' DAX Daily Quotes ' ) grid ( True ); axis ( ' tight ' ) subplot ( 212 ) plot ( DAX . index , DAX [ ' RET ' ]) ylabel ( ' DAX Daily Log Returns ' ) grid ( True ); axis ( ' tight ' )
Derivatives Analytics
EuroPython 2011
10 / 34
Data Analysis
Time Series
DAX time series252 moving mean return, volatility and correlation between both4
0.8 0.6 0.4 0.2 0.0 0.2 0.4 0.6 0.8 1.0 1992 0.50 0.45 0.40 0.35 0.30 0.25 0.20 0.15 0.10 1992 1.0 0.5 0.0 0.5 1.0 1992
1994
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
Data Analysis
Time Series
# mean return , volatility and correlation ( 252 days moving = 1 year ) figure () subplot ( 311 ) mr252 = Series ( rolling_mean ( DAX [ ' RET ' ] , 252 )* 252 , index = DAX . index ) mr252 . plot (); grid ( True ); ylabel ( ' Return ( 252d Mov ) ' ) x , y = REG ( mr252 , 0 ); plot (x , y ) subplot ( 312 ) vo252 = Series ( rolling_std ( DAX [ ' RET ' ] , 252 )* sqrt ( 252 ) , index = DAX . index ) vo252 . plot (); grid ( True ); ylabel ( ' Vola ( 252d Mov ) ' ) x , y = REG ( vo252 , 0 ); plot (x , y ); vx = axis () subplot ( 313 ) co252 = Series ( rolling_corr ( mr252 , vo252 , 252 ) , index = DAX . index ) co252 . plot (); grid ( True ); ylabel ( ' Corr ( 252d Mov ) ' ) x , y = REG ( co252 , 0 ); plot (x , y ); cx = axis () axis ([ vx [ 0 ] , vx [ 1 ] , cx [ 2 ] , cx [ 3 ]])
Derivatives Analytics
EuroPython 2011
12 / 34
Data Analysis
Cross-Sectional Data
maturities: 21 (red dots), 49 (green crosses), 140 (blue triangles), 231 (yellow stones) and 322 days (purple hectagons)
5
Model Economy
economy with nal date T, 0 < T < uncertainty is represented by a ltered probability space {, F , F, P } for 0 t T the risk-neutral index dynamics are given by the SDE dSt = rdt + dZt St (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 ltration F, i.e. Ft F (S0st ) a risk-less zero-coupon bond satises the DE dBt = rdt Bt 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) (2)
Derivatives Analytics
EuroPython 2011
14 / 34
Model Economy
to simulate the nancial 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
2
= =
t+
tzt
(3) (4)
ert
for t {t, ..., T } and standard normally distributed zt this scheme is an Euler discretization which is known to be exact for the geometric Brownian motion (1)
Derivatives Analytics
EuroPython 2011
15 / 34
European Options
a European put option on the index S pays at maturity T h(ST ) max[K ST , 0] for a xed strike price K to value such an option, simulate I paths of St such that you get I values ST,i , i {1, ..., I } the Monte Carlo estimator for the put option value then is V0 = erT 1 I
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
# # Index Level Path Generation S = zeros (( M +1 , I ) , ' d ' ) # index value matrix S [0 ,:]= S0 # initial values for t in range (1 , M +1 , 1 ): # stock price paths ran = s ta ndard_normal ( I ) # pseudo - random numbers S [t ,:]= S [t -1 ,:]* exp (( r - vol ** 2 / 2 )* dt + vol * ran * sqrt ( dt )) # # Valuation h = maximum (K - S [ - 1 ] , 0 ) V0 = exp ( - r * T )* sum ( h )/ I # inner values at maturity # MCS estimator
# # 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
to value American options by Monte Carlo simulation, a discrete optimal stopping problem has to be solved: V0 = 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 Vt (s) = max[ht (s), Ct (s)] (6)
i.e. the maximum of the payo ht (s) of immediate exercise and the expected payo Ct (s) of not exercising; this quantity is given as
r t Vt+t (St+t )|St = s) Ct (s) = EQ t (e
(7)
Derivatives Analytics
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 Longsta 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
# # 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 Longsta 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
Python is well-suited to implement ecent, i.e. fast and accurate, numerical valuation algorithms
MCS/LSM with 25 steps/35,000 paths: 180 megabytes of data crunched in 1.5 seconds MCS/LSM with 50 steps/100,000 paths: 980 megabytes of data crunched in 8.5 seconds
reported times are from my 3 years old notebook ... the speed-up compared to the times reported in Medvedev and Scaillet (2009) is 480+ times (1.5 seconds vs. 720+ seconds) to reach this speed-up, our algorithm mainly uses variance reductions techniques (like moment matching and control variates) which allows to reduce the number of time steps and paths signicantly
Derivatives Analytics
EuroPython 2011
28 / 34
Results from 3 simulation runs for the 36 American put options of Medvedev and Scaillet (2009)
---------------------------------------------------Start Calculations 2011-06-22 13:43:02.163000 ---------------------------------------------------Name of Simulation Base_3_25_35_TTF_2.5_1.5 Seed Value for RNG 150000 Number of Runs 3 Time Steps 25 Paths 35000 Control Variates True Moment Matching True Antithetic Paths False Option Prices 108 Absolute Tolerance 0.0250 Relative Tolerance 0.0150 Errors 0 Error Ratio 0.0000 Aver Val Error -0.0059 Aver Abs Val Error 0.0154 Time in Seconds 135.7890 Time in Minutes 2.2631 Time per Option 1.2573 ---------------------------------------------------End Calculations 2011-06-22 13:45:17.952000 ----------------------------------------------------
Derivatives Analytics
EuroPython 2011
29 / 34
Capabilities
DEXISION can handle a number of nancial derivatives ranging from plain vanilla to complex and exotic
Example products:
plain vanilla options American options Asian options options on baskets certicates (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
nancial research: researchers, lecturers and students in (mathematical) nance nd in DEXISION an easy-to-learn tool to model, value and analyze nancial derivatives nancial engineering: nancial engineers and risk managers in investment banks, hedge funds, etc. can use DEXISION to quickly model and value diverse nancial products, to cross-check valuations and to assess risks of complex derivatives portfolios actuarial calculations: those responsible for the design, valuation and risk management of market-oriented insurance products can engineer, value and test new and existing products easily nancial reporting: IFRS and other reporting standards require the use of formal (option) pricing models when there are no market prices; DEXISION considerably simplies the modelling, valuation and risk assessment for illiquid, complex, non-traded nancial instruments and embedded options real options valuation: DEXISION oers unique capabilities to account for the specics of real options (as compared to nancial options)
Derivatives Analytics
EuroPython 2011
31 / 34
Technology
Suse Linux 11.1 as 64 bit operating system Apache 2 as Web server MySQL 5.0.67 as relational database Python 2.6 as core language (integrated via mod_python in Apache) Numpy 1.3.0 as fast linear algebra library Dojo 1.0 as JavaScript framework for the GUI SVG for all custom graphics MoinMoin Wiki (Python powered) for Web documentation
Derivatives Analytics
EuroPython 2011
32 / 34
Technology
recently, Visixion added Web services to DEXISIONs functionalities which allow to integrate it into any environment once a structure is modeled in DEXISION, updates of valuations can be received in real-time via these Web services (with data delivered e.g. in XML format) during the Web service call, data/variables can also be provided a call to value an American put option on the DAX index could look like: https://company.dexision.com/DEXISIONeval.py?company=X&user=Y&pwd= Z&paths=50000&steps=150&portfolio=DAX/DAX_Am_Put_Dec_2011&DAX_ current=7200&DAX_vola=0.175&rate=0.03&strike=6800
Derivatives Analytics
EuroPython 2011
33 / 34
Technology
Contact
Dr. Yves J. Hilpisch Visixion GmbH Rathausstrasse 75-79 66333 Voelklingen Germany www.visixion.com Derivatives Analytics and Python Programming www.dexision.com Derivatives Analytics On Demand E [email protected] T/F +49 3212 1129194
Derivatives Analytics
EuroPython 2011
34 / 34