Friday, April 04, 2014

Quantlib and Python - Getting Started

[See part 1 of this series here]

When it comes to open source software Linux is the only way to go.

Why would you use anything other than Photoshop on a Mac or a cutting edge game on Windows? - using Linux is just easier for Python development and all the quant lib packages come ready made for your machine (no messy compiling required!).

In order to install Quantlib for Python on Debian (sure to work on Ubuntu and Mint also) Linux system run:

apt-get install quantlib-python quantlib-swig

which will install everything you need.

Now give your setup a quick test:

cd /usr/share/doc/quantlib-python/examples/
python bonds.py

With any luck you will see a bunch of analytical results in your console.

This collection of examples are one of two resources I have found about using Python for Quantlib, which hopefully I can add to a little with these blog posts.

The other reference which you can use are the test units also included in the same folder.

ls /usr/share/doc/quantlib-python/examples/test/

These tests are used as a check by the library developers to ensure everything is good to go and no bugs have been introduced.  With such meagre references the tests are valuable reference for code written in Python.

One of the other places to go to with is the Quantlib C++ library reference itself.  As I said before, C++ is not my forte and in this day and age more of an academic pursuit in finance than anything else.  Nevertheless, example Python code is in short supply - and the python code that you do write is pretty close to what you see written in C++.

The upshot is on the one hand your Python code doesn't feel super Pythonic because you are tied so close to the C++ libraries.  On the other hand the C++ idioms start making more and more sense after days and weeks of banging your head of the wall! (no pain no gain?)

The places to ask questions are Wilmott, Quant Stack Exchange and the mailing list.  None are high traffic when it comes to Quantlib (scared off yet?) so your mileage may vary.

[Continued here]


4 comments:

Andre de Boer said...

Hello John,

I used 'apt-get install quantlib-python' as you mentioned in your blog, but I get the message:
ImportError: /usr/lib/python2.7/dist-packages/QuantLib/_QuantLib.so: undefined symbol: _ZThn104_N8QuantLib19CappedFlooredCoupon6updateEv

Earlier you wrote:
You have two different versions installed. I would wipe and install from the apt repo from scratch

I have by default python2 and python3 installed in ubuntu.
Do you mean uninstall both and install only one of the two?

Kind regards,
André

Unknown said...

Andre,

It's been a while since I've done this - I will do a full reinstall from scratch and let you know what to do (I think you're missing a package somewhere...)

Unknown said...

Andre,

It's been a while since I've done this - I will do a full reinstall from scratch and let you know what to do (I think you're missing a package somewhere...)

Andre de Boer said...

John,

That would be very helpful, thanks a lot.