Python is a multi-purpose programming language and many people use Python for everything but their core applications. However, Python can even be used for compute-intensive tasks like Monte Carlo simulation — a common numerical method in Finance in general and Financial Engineering in particular.
Monte Carlo simulation (MCS) is a quite flexible numerical method, in particular when it comes to derivatives valuation and risk calculations, e.g. in the context of Value-at-Risk (VaR) computations. There is hardly a problem which cannot be tackled by this method.
However, it took until 2001 that the seminal paper by Francis Longstaff and Eduardo Schwartz was published which solved the problem of valuing American options by Monte Carlo satisfactorily. They proposed an algorithm, called Least-Squares Monte Carlo (LSM), to devise the optimal exercise strategy for American or Bermudan options. These are options which can be exercised over a certain period of time or at a number of different exercise dates.
Generally, MCS demands for millions of different calculations for a single option to be valued. In the case of American options, dozens or even hundreds of least-squares regressions have to be computed on top. Such tasks typically are attacked on the basis of fast, compiled languages like C or C++.
Python can also be used to this end. One has to take care, however, that the most time-consuming tasks — generally those with (nested) for loops — are delegated to the NumPy library (visit the Web site). This library’s specialty is the efficient and convenient handling of array manipulations and calculations of any kind. It is quite fast since its core is implemented in C and Fortran. If done right, large for loops are executed at the speed of pure C and/or Fortran code plus “a little bit” of overhead for the communication between Python and NumPy.
As a general rule, you should avoid (nested) for loops on the Python level as far as possible. NumPy helps you considerably when it comes to implement array operations in a compact and high performing manner. As a result you get compact, yet highly readable and fast code.
The following script illustrates the use of NumPy through a rather concise implementation of the LSM algorithm. Execution speed is, of course, hardware dependent but in general satisfactory. What may be even more important from a commercial point of view is that algorithms implemented in such a manner are in general much more easy to understand, test, debug and maintain than counterparts implemented in C or C++. Python in combination with NumPy leads to code that is quite close to the “mathematical language” used to describe the algorithms in the first place.
The script values the first (non-trivial) example of the seminal paper Longstaff-Schwartz (2001). For details refer to the paper itself (see publisher’s Web site) or to the recent book “Derivatives Analytics with Python” by Dr. Yves J. Hilpisch, MD of Visixion GmbH.
# # Least-Squares Monte Carlo for American Put # (c) Visixion GmbH -- For Illustration Purposes Only. # from pylab import * from time import time t0=time() ## Parameters M=50;I=50000 # Simulation S0=36.;K=40.;r=0.06;vol=0.2;T=1.0 # Option dt=T/M;df=exp(-r*dt) # Time & Discounting ## LSM Algorithm S=ones((M+1,I),'d') S[1:,:]=exp(cumsum((r-vol**2/2)*dt +vol*standard_normal((M,I))*sqrt(dt), axis=0)) V=maximum(K-S*S0,0) for t in range(M-1,0,-1): # Dynamic Optimization/Exercise Decisions V[t,:]=where(V[t,:]>polyval( polyfit(S[t,:],V[t+1,:]*df,3), S[t,:]),V[t,:],V[t+1,:]*df) ## Output print "Value %8.3f" %(sum(V[1,:]*df)/I) # LSM estimator print "Time %8.3f" %(time()-t0)
Go to Python for Finance (II)
Go to Python for Finance (III)