The team at QuantStart have begun working on an options pricing library in Python. To date a Path Dependent Asian option pricer has been developed with validated results. At this stage it still requires optimisation to run at an acceptable speed on our servers.
Although C++ is the predominant language for options pricing, it was decided for the purposes of learning that the team would concentrate on an all-Python based library. Not only would this provide the team with the chance to practice their Python skills, but it also allows a straightforward integration of the library into the site.
The library is tentatively named PyQuant and has very limited functionality at this stage. It consists of two major components, a set of closed-form solutions for Vanilla calls/puts and the Digitals and a basic Monte Carlo pricer which prices the Double-Digitals and Power Options. In time further options that contain closed-form solutions will be added for validation purposes, but at this stage the team is concentrating on the development of the Monte Carlo solver.
The closed form solutions rely on two statistical functions - the Normal Probability Density Function and the Cumulative Normal Distribution Function. A numerical approximation to the CNDF can be found in . With the NPDF and CNDF it is possible to calculate solutions for the Vanilla Calls and Puts, as well as the common Greeks: Delta, Gamma, Rho, Vega and Theta. A closed form solution for Digital options is also possible.
The Monte Carlo based solutions work differently. There is one module that contains all of the pay-off objects for each type of option - Call, Put, Forward, Digital Call etc. Another module stores option objects. In the case of the Vanilla option, an expiry time and a pay-off are required. The strike is encapsulated in the pay-off object, which ensures code resuability for both pay-offs and options. The final module includes Monte Carlo engines which calculate a large range of stock path evolutions (based on Geometric Brownian Motion) and use these to calculate an expected pay-off of the option. The pay-off is discounted at the risk-free rate and this provides the price.
At this stage it is computationally expensive to re-run the Monte Carlo pricer when changing an input. There are a few ways to optimise this. The first is to make use of SciPy, a Python scientific library. It includes plenty of optimisation strategies - take a look at this article on Performance Python, its performance compared to C++ may just surprise you. The second is to actually write a dedicated C++ library which can be called from Python. However this is contrary to the idea a Python derivatives pricing library.