PyQL : a new set of Python wrappers for QuantLib

March 23, 2012

Hi folks,

We are happy to announce the release of PyQL [1], a new set of Python wrappers for QuantLib.

The project is available here :

* URL: http://github.com/enthought/pyql
* License: BSD license.
* Authors: Didrik Pinte, Enthought and Patrick Henaff, IAE Paris.

Why another set of Python wrappers for QuantLib?

The SWIG wrappers provide a very good coverage of the library but have
a number of pain points:

  •   few Pythonic optimisation in the syntax: the code a user must writeon the Python side looks like the C++ version
  • no docstring or function signature available on the Python side
  • complex debugging and complex customization of the wrappers
  • monolithic build process
  • complete loss of the C++ code organisation with a flat namespace in Python
  • SWIG typemaps development is not that fun

For those reasons and to have the ability to expose some of the
QuantLib internals that could be very useful on the Python side, we
chosed another road. PyQL is build on top of Cython and creates a thin
Pythonic layer on top of QuantLib. It allows a tight control on the
wrapping and provides higher level Python integration.

Features:

  1. Integration with standard datatypes (like datetime objects) and numpy arrays
  2. Simplifed API on the Python side (e.g. usage of Handles completely hidden from the user)
  3. Support full docstring and expose detailed function signatures to Python
  4. Code organised in subpackages to provide a decent namespace, very close to the C++ code organisation
  5. Easy extendibility thanks to Cython and shorter build time when adding new functionnalities
  6. Sphinx documentation

It supports QuantLib >= 1.1 and currently builds very nicely on MacOSX
and Linux. The Windows builds will be there soon. Regarding the build
process, make sure you read the build instruction!
(Cython 0.15 needs a simple patch available in the repo)

The library comes with a decent test suite and many examples: from the
very basic option valuation to more complex heston model calibration
within an IPython notebook.

For more details, take a look at the code, contact the authors, or
discuss on the list!

We are looking forward questions, comments, contributions.

[1] The name is still subject to modification as PyQL is already used
by other projects unrelated to QuantLib. Suggestions are welcome!


Building MacVim with full Python EPD support

January 2, 2011

If you want to use the :python or :pyfile commands or the omnicompletion plugin of Vim on a Mac with EPD, you will most probably want to recompile it with the Python support linked on the EPD instllation in place of the Mac Python.

Here are the steps to follow (done on the latest stable 7.3 sources):

1. As EPD 6.x 64bit on MacOSX does not have the full ETS, you will have to compile it against the 32bit version. So, set the right flags to make it work :


export CFLAGS=-m32
export CPPFLAGS=-m32

Then configure it to use the EPD installation. If your PATH is update to point to EPD, it is straightforward :


./configure --enable-pythoninterp --with-macarchs=i386

The output should contains something like the following lines :


...
checking Python version... 2.6
checking Python is 1.4 or better... yep
checking Python's install prefix... /Library/Frameworks/Python.framework/Versions/6.3
checking Python's execution prefix... /Library/Frameworks/Python.framework/Versions/6.3
checking Python's configuration directory... /Library/Frameworks/Python.framework/Versions/6.3/lib/python2.6/config
...

Then calling make will result in an issue when generating the icons. It has to use the default Python installation to make them but the default Python installation works in 64bit mode. The EPD Python will miss some modules. To fix that, edit src/MacVim/icons/Makefile and prefix the python calls with the following :


arch -i386 /usr/bin/python make_icons.py $(OUTDIR)

This will ensure you use the default Mac installation in 32bit mode. Do the same for all the python calls in the Makefile.

After that, the app must build fine and you can benefit from the full EPD module list from within MacVim


Building Quantlib Python bindings (32bit) on MacOSX

December 30, 2010

I needed to use Quantlib with Traits on MacOSX. As the Traits backends are not available in 64bit on MacOSX with EPD, I had to build a 32bit version of Quantlib and the related swig bindings to make it work. By default, Quantlib will be built in 64bit mode and you will get the following warning when building the Python swig bindings using a 32bit Python installation (python setup.py build):

ld: warning: in /opt/local/lib/libQuantLib.dylib, file was built for unsupported file format which is not the architecture being linked (i386)

and the following error when trying to run the test (python setup.py test)

ImportError: dlopen(build/lib.macosx-10.5-i386-2.6/QuantLib/_QuantLib.so, 2): Symbol not found: __ZN8QuantLib12SmileSection6updateEv
Referenced from: /Users/dpinte/Downloads/QuantLib-SWIG-1.0/Python/build/lib.macosx-10.5-i386-2.6/QuantLib/_QuantLib.so
Expected in: flat namespace
in /Users/dpinte/Downloads/QuantLib-SWIG-1.0/Python/build/lib.macosx-10.5-i386-2.6/QuantLib/_QuantLib.so

1. Build Quantlib after making sure you do have Boost installed (you can use MacPorts for that):

export CFLAGS=-m32
export CPPFLAGS=-m32
export CXXFLAGS=-m32
./configure --enable-static --with-boost-include=/opt/local/include/ \
--with-boost-lib=/opt/local/lib/ --prefix=/opt/local/
make
make install

2. Build the swig bindings:


python setup.py build
python setup.py test
python setup.py install

And it should work fine :


...
Ran 25 tests in 0.889s

OK


Interfacing GSL with Python using Cython / comparison with weave

April 22, 2010

The second example I wrote while testing how easy it is to use a C library from Python using Cython is the Gnu Scientific Library, gsl. The goal here is to compare how easy it is to do it using Cython or weave.

The first example is a bessel function (from gsl/gsl_sf_bessel.h):

double gsl_sf_bessel_J0(const double x);

Read the rest of this entry »


Interfacing ta-lib with Python using Cython : moving average function example

April 22, 2010

Following up on my previous post about how Cython could be used to improve the performance, I wanted to show how easy it is to interact with a C library. Here is the example of ta-lib : TA-Lib is widely used by trading software developers requiring to perform technical analysis of financial market data. ta-lib has a SWIG Python interface but I wanted to have my own quick and dirty interface to one specific function. It would be interesting to compare the effectivness of the SWIG interface compared to the Cython (for a later post).

1. Interfacing ta-lib

What you need is a compiled version of the library. On Windows using EPD, I had to install msys to have the needed command for autotools build systems. With msys and the EPD mingw install, I compiled ta-lib with a standard .configure, make, make install. The library and header files were then available within c:\msys\1.0\local\lib and c:\msys\1.0\local\include
Read the rest of this entry »


Surface plots using a 4th parameter for colormap in Mayavi

March 30, 2010

Playing with Mayavi, I wanted to find an easy way to use the colormap in an mlab.surf() call to display of fourth dimension. It is a very nice way to explore datasets. The image here below shows an option valuation tool that overlays the Black & Scholes call delta value on top of the surface made using the strikes, time to expiry and Black & Scholes call value.

How can you do it ?
Read the rest of this entry »


NumPy/MKL vs Matlab performance

March 16, 2010

Further to the post I wrote on the MKL performance improvement on NumPy, I have tried to get some figures comparing it to Matlab. Here are some results. Any suggestion to improve the comparison is welcome. I will update it with the different values I do collect.

Here is the Matlab script I used to compare the Python code :

disp('Eig');tic;data=rand(500,500);eig(data);toc;
disp('Svd');tic;data=rand(1000,1000);[u,s,v]=svd(data);s=svd(data);toc;
disp('Inv');tic;data=rand(1000,1000);result=inv(data);toc;
disp('Det');tic;data=rand(1000,1000);result=det(data);toc;
disp('Dot');tic;a=rand(1000,1000);b=inv(a);result=a*b-eye(1000);toc;
disp('Done');

Each line is linked to the corresponding Python function in my test script (see my MKL post).

The results are interesting. Tests have been made with R2007a and R2008a versions and compared to and EPD 6.1 with NumPy using MKL.

Here are the timings on an Intel dual core computer running Matlab R2007a :

Eig : Elapsed time is 0.718934 seconds.
Svd : Elapsed time is 17.039340 seconds.
Inv : Elapsed time is 0.525181 seconds.
Det : Elapsed time is 0.200815 seconds.
Dot : Elapsed time is 0.958015 seconds.

And those are the timings on an Intel Xeon 8 cores machine running Matlab R2008a :

Eig : Elapsed time is 1.235884 seconds.
Svd : Elapsed time is 25.971139 seconds.
Inv : Elapsed time is 0.277503 seconds.
Det : Elapsed time is 0.142898 seconds.
Dot : Elapsed time is 0.354413 seconds.

Compared to the NumPy/MKL tests, here are the results :

Function Core2Duo-With MKL Core2Duo-R2007a Speedup using numpy
test_eigenvalue 752ms 718ms 0.96
test_svd 4608ms 17039ms 3.70
test_inv 418ms 525ms 1.26
test_det 186ms 200ms 1.07
test_dot 666ms 958ms 1.44
Function Xeon8core-With MKL Xeon8core-R2008a Speedup using numpy
test_eigenvalue 772ms 986ms 1.28
test_svd 2119ms 26081ms 12.5
test_inv 153ms 230ms 1.52
test_det 65ms 105ms 1.61
test_dot 235ms 287ms 1.23

Cloud computing with Python and Excel : picloud integration

March 12, 2010

Further to my previous post about making Python GUI interact with Excel, we thought it would be interesting to test how cloud computing could be easily accessible from Excel using Python and the picloud library.

We have just added interfaced some Python code that do compute the put and call price of a stock based on a Black & Scholes Monte-Carlo simulation. We end up with one function call in Excel :

Pyxll integration with picloud

Implementation details
Read the rest of this entry »


Interactive Python graphics/visualisation with Excel

March 12, 2010

Making Excel users benefit of the quick and powerful Python GUI tools was an idea that looked pretty interesting.

The two main arguments for that are :

  1. Python code is easily maintainable, tested and integrated compared to what can be inside of an Excel sheet
  2. Python has some powerful libraries for numerical computing, 2D/3D visualisation, etc.

For the last London Financial PUG meeting (March 11), Travis and I had the idea of making Chaco playing with Excel thanks to a cool tool named pyxll and the pywin32 extension. The example allows the user to select a range of columns in Excel, send them to a Chaco regression tool where the user can select points. A Chaco tool does lively compute a regression on those points and update the Excel sheets.

The result is pretty interesting as shown on the screenshot here below. (I will most probably post a video showing how interactive it is).

Pyxll Chaco interactive session

Pyxll is a very interesting library allowing you to very easily make your Python function available within Excel (either as menu or functions). Thanks a lot to Tony Roberts for his excellent pieces of advice on using pyxll for the demo.

Chaco Python plotting application toolkit that facilitates writing plotting applications at all levels of complexity, from simple scripts with hard-coded data to large plotting programs with complex data interrelationships and a multitude of interactive tools. Chaco is part of the Enthought Tool Suite and available under the BDS license

Implementation details
Read the rest of this entry »


Improving Python speed using Cython : Binomial option valuation example

February 12, 2010

For the last LFPUG meeting, I have developed a simple example showing how Cython could be used to improve the performance of Python. The idea here is not to find the best way to do it but just to try to get a great speed up without digging into the hard stuff.

1. The model

The theoretical binomial model can be used to value options. “Paul Wilmott’s introduces Quantitative Finance” shows a VB example of a call valuation with such model. The following code is inspired by the VB code and gives you a pure Python version of it.
Read the rest of this entry »