Setting up QuantLib in Linux

[ This is an article that I started writing a few years back, when I was experimenting with Puppy Linux, then put on hold. Much of it is still useful, so the post merits to go public. ]

Having written a walk-through on how to set up a Linux system ready for compiling software (see here), in this post I give a walk-through of how to set up and use the open-source quant library QuantLib.

Since QuantLib is built using the Boost libraries, we start by installing the Boost C++ libraries.

Boost

The QuantLib website states that Boost 1.34.1 is the minimal version needed. As of writing this post, Boost is at version 1.52.0. I downloaded version 1.52.0 from here.

Copy this file into your source-code-building space and unpack it with tar xjvf boost_1_52_0tar.bz2, there are lots of files in there!Puppy431_InstallingBoost1

Puppy431_InstallingBoost2

This creates a directory tree, and you can read the html guide by opening the index.htm file in the top directory in that tree.Puppy431_InstallingBoost3

Puppy431_InstallingBoost4

In the top banner there is a link to the getting started section which is where I found the details of how to build and install boost.

Surprising news for me was  that a large part of Boost is made up of header files — there is nothing to build for most of it.

One thing to note is that the configure step found Python 2.5 in /usr.Puppy431_BoostInstall1

Although the message was to use b2, I instead followed the guide and chose to use
./bjam install
Build took 15 minutes in total and looks like thisPuppy431_BoostInstall2

and something is definitely happening with Python:Puppy431_BoostInstall3

but it all seemed to end well:Puppy431_BoostInstall4

The guide explains that LD_LIBRARY_PATH should be updated.

Installing QuantLib

I ran ./configure --prefix=$HOME but it complained that Boost was not available. So adding the C and CPLUS include paths to .bashrc:Puppy431_BashrcForQuantLibInstall

Then the configure seemed to work.

Now onto make.

All fine, and make install filled up the personal space, so I am having to resize and reboot.

Still ran out of space, so I scanned the file usage of /root and deleted a few larger files that were unnecessary.

Building QuantLib

This stage is not complicated, it just takes a long time.

The usual
./configure --prefix=$HOME
make

can be used. Note that we are not yet going to do a make install; first we will test that all is okay.

Testing QuantLib

Once you have unpacked and make-ed the tarball you will find an Examples directory which contains a number of folders. These are obviously going to contain examples of such-and-such pricing features, and we will now run through a couple to see check that they work. You can already see that there are a few make-related files in there.Puppy431_QuantLibExample_Swap

The examples are all set up so that we can build them with make. However, we are going to start to investigate what exactly is happening during the make process, as it will give us an opportunity to get a bit more familiar with the tools that often get used for compiling/linking/distributing source code.

We start by moving into the Swap directory. Note that you can easily get a command line within a directory you are browsing by using a right-click, as I show here:Puppy431_QuantLibExample_SwapDirectoryOnce you have the terminal opened, run the program with ./SwapValuation.

That should display some pricings of a swap, based on three different discount curves.

We now see how you can tweak the code and re-build it with make.

Installing QuantLib and experimenting with your own code

The make install will copy the libraries into our usual location: /root/lib

Let’s create a folder in our /root space and copy across the whole of the Examples/Swap folder.

We will try to get it working with a simple g++ command. The failures that we hit along the way will serve as a great lesson in the basics of compilation, namely the two-steps of compilation and linking.

The answer that works:
g++ swapvaluation.cpp /root/lib/libQuantLib.a -o foo

Basically the static library just gets listed as part of the package that needs to be linked together. Note that the ordering is important: there is an implicit left-to-tight dependency.

Here are the failures

A few steps which all end in failure, but which are useful to see:

  1. Enter the directory and run make. Here is my error message:Puppy431_InvestigatingMakeSwap

Debugging with gdb

An SO post here explains how gdb finds the source code.

Info here on how the source files are searched.

There is a front end to gdb if you fancy it: install ddd from here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s