Back of the Envelope

Observations on the Theory and Empirics of Mathematical Finance

Posts Tagged ‘Computational Finance

[Exec-Ed] “Practical Quantitative Finance using Python and QuantLib”

leave a comment »

Some bit of shameless self-promotion, for those with some interest in derivatives:

We are starting a new online executive program covering topics on derivatives pricing and computational finance using Python and QuantLib. The program begins in the 3rd week of January. If you’d like to join or are interested to know more, you may write to me or get in touch with our office. More details here.

Written by Vineet

January 1, 2017 at 6:19 pm

[CompFin] Installing QuantLib and QuantLib-Python in Windows

with 13 comments

In our recent paper introducing QuantLib, we had an appendix which described how to set both QuantLib and QuantLib-Python up for use in Ubuntu and Windows. Since I rarely use Windows these days, for instructions for Windows we mostly relied on what was available on the QuantLib website and answers to relevant questions on stackoverflow.

When I got my hands on a Windows laptop recently, to my embarrassment I realized I could not use those instructions to get QuantLib and QuantLib-Python working on a Windows 7 machine. Needless to say this needed sorting out, and now it has been.

I’ll have the paper updated on our website shortly, but in the mean time here are the instructions for installing QuantLib and QuantLib-Python in Windows. These have been tested on three different Windows machines, so we are reasonably sure they work. However, if something doesn’t work for you, do drop in a comment.

Step-by-step by instructions for installing QuantLib and QuantLib-Python in Windows

  1. QuantLib requires a working C++ Environment, and for the purposes of instructions here (meant for a novice), it is recommended that the user install the free Microsoft Visual Express Desktop 2013 edition which comes with Visual C++ 12 (MSVC12).
  2. QuantLib-Python requires a working Python environment, and we recommend working with the Anaconda meta-package. Instructions are available from the Anaconda install page. For our purpose here, Windows users should work with the 32-bit version of Anaconda. It is recommended to install Anaconda with all the default settings, and update it by running \texttt{conda update conda} and \texttt{conda update anaconda}. Again, it does not matter if one installs Anaconda-2.x (with Python 2.7) or Anaconda-3.x (with Python 3), but it is recommended that the user install the Python 3 version, as that’s where the Python language seems to be headed.
  3. Compiling QuantLib requires Boost C++ libraries installed. And this is where the utility of working with MSVC becomes most apparent. Pre-packaged binaries for Boost are available for specific versions of MSVC (in our case MSVC12) from its sourceforge page, and installing Boost on MSVC is no more difficult than installing any other software in Windows. (Users should download the executable for the 32-bit architecture, as the free version of MSVC does not seem to contain the necessary utilities for compiling in 64-bit. There is no such problem if you have access to a professional edition of MSVC though.)
  4. After installing Boost, we need to install SWIG. Pre-packaged binaries for SWIG for Windows are available from its source page (latest version 3.0.5), and it is enough to extract the SWIG zip file in a convenient folder.
  5. Download the same version of QuantLib and QuantLib-Python (latest version 1.6)
  6. Launch the Visual Studio command prompt: All the commands below assume that we are working in Visual Studio command prompt (or if not, all the relevant Visual Studio related environment variables have been set). It can be launched from the Start menu (in Windows 7) or from Apps (in Windows 8). If you can’t find it, see here.
    • NB: Though we haven’t encountered any issue when using MSVC12, as pointed out by Andres in comments, when working with VS 2015, it might be safest to work with x64 Native Tools Command Prompt.
  7. After the above steps, installing QuantLib in Windows simply requires the following steps (mind the gaps):
     cd \path\to\QuantLib-1.6
    set myboost=\path\to\boost\boost_1_58_0
    msbuild /p:AdditionalLibPaths="%myboost\lib32-msvc-12.0" /p:Configuration=Release /p:Platform=Win32 QuantLib_vc12.sln 

    The last step above may take a while (on an Intel i5, Windows 7 machine with 4 GB RAM it took almost 45 minutes).

    Note that throughout in this post \texttt{\textbackslash path\textbackslash to\textbackslash someplace} represents the directory of \texttt{someplace}. For example, if \texttt{boost\_1\_58\_0} is installed in \texttt{C:\textbackslash boost}\texttt{\textbackslash path\textbackslash to\textbackslash boost\_1\_58\_0} should be taken to mean \texttt{C:\textbackslash boost\textbackslash boost\_1\_58\_0}.

  8. One should ensure that QuantLib is correctly installed in the previous step, as what follows assume a working QuantLib. If it is correctly installed, you should see something like the following message at some stage before the installation completes:
     Tests completed in 16m 50s
    Test suite "Master Test Suite" passed with:
    1002021 assertions out of 1002021 assertions passed
    609 test cases out of 609 passed 

    And at the very end:

     Build succeeded.
    0 Warning(s)
    0 Error(s)
    Time Elapsed 00:44:20.07
  9. Although not absolutely necessary, to ensure that all is well, it is recommended to also build QuantLib in Debug mode (replace ‘Release’ by ‘Debug’ in the \texttt{msbuild} command in step 7 above; and will take roughly half the time it took to build the Release version), and test the code in the QuantLib Usage section on the QuantLib install page for Windows.
  10. Installing QuantLib-Python requires a few extra settings. At the Visual Studio command prompt, set the following (again, mind the gaps):
     cd \path\to\QuantLib-SWIG-1.6\Python
    set PATH=\path\to\Anaconda3;\path\to\Anaconda3\scripts;\path\to\swigwin-3.0.5;\path\to\QuantLib-1.6\lib;%PATH%
    set QL_DIR=\path\to\QuantLib-1.6
    set INCLUDE=\path\to\boost_1_58_0;%INCLUDE%
    set LIB=\path\to\boost\lib32-msvc-12.0;%LIB%
    echo [build] > setup.cfg && echo compiler=msvc >> setup.cfg 
  11. Once the environment variables have been set in step 10 above, all we need is to run the following steps:
     cd \path\to\QuantLib-SWIG-1.6\Python
    python wrap
    python build
    python test
    python install 

    If you are wondering how would you know that QuantLib-Python has been installed correctly, after the \texttt{python test} step, you should see a message something like this:

    Ran 36 tests in 1.253s
  12. To be doubly sure, you should now go to Luigi Ballabio’s QuantLib page and run the examples there.

My co-author has a more detailed post on his blog discussing this, and in addition to providing motivation for some of our choices, he also describes how to install QuantLib and QuantLib-Python using only open-source tools (for “free as in free speech”) and without using Visual Studio. Highly recommended.

Update (8th July): Paper updated with these instructions.

Written by Vineet

July 7, 2015 at 2:16 am

[WP] Derivatives pricing using QuantLib

with 8 comments

My colleague and I have a new paper introducing QuantLib for pricing derivatives in practice. Here is the abstract:

Given the complexity of over-the-counter derivatives and structured products, almost all of derivatives pricing today is based on numerical methods. While large financial institutions typically have their own team of developers who maintain state-of-the-art financial libraries, till a few years ago none of that sophistication was available for use in teaching and research. For the last decade, there is now a reliable C++ open-source library available called QuantLib. This note introduces QuantLib for pricing derivatives and documents our experience using QuantLib in our course on Computational Finance at the Indian Institute of Management Ahmedabad. The fact that it is also available (and extendable) in Python has allowed us to harness the power of C++ with the ease of iPython notebooks in the classroom as well as for student’s projects.

As always, comments welcome.

Written by Vineet

April 3, 2015 at 7:18 pm

Simulating Correlated Stochastic Differential Equations (or How to Simulate Heston Stochastic Volatility Model)

leave a comment »

I notice that students new to computational finance often make mistakes in simulating correlated Brownian motion paths. Here is a ready reckoner.

Let’s take the example of generating paths for asset prices using the Heston stochastic volatility model:

\begin{aligned} dS &= rSdt + \sqrt{\nu} S dW_1 \\ d \nu &= \kappa (\theta - \nu) dt + \sigma \sqrt{\nu} dW_2 \end{aligned}

where \nu is the instantaneous variance of asset returns, and the increment in Brownian motions dW_1 and dW_2 are correlated with correlation coefficient \rho, i.e. \langle dW_1, dW_2 \rangle = \rho dt.

The simplest way to generate paths S and \nu is to use the Euler discretization (there are better methods available of course, for Heston in particular) as:

\begin{aligned} ln S(t + \Delta t) &= ln S(t) + r \Delta t - \frac{1}{2} \nu (t) \Delta t + \sqrt{\nu (t)} \epsilon_1 \sqrt{\Delta t} \\ \nu (t + \Delta t) &=\nu (t) + \kappa (\theta - \nu (t) ) \Delta t + \sigma \sqrt{\nu (t)} \epsilon_2 \sqrt{\Delta t} \end{aligned}

where \epsilon_1 and \epsilon_2 are standard Gaussian random variables with correlation \rho

To generate correlated standard Gaussian random variables, i.e. \epsilon_1 and \epsilon_2, the most popular method is to use what is called the Cholesky decomposition. Given two uncorrelated standard Gaussian random variables Z_1 and Z_2 (easily done both in Excel and in R), Cholesky decomposition can be used to generate \epsilon_1 and \epsilon_2 as:

\begin{aligned} \epsilon_1 &= Z_1 \\ \epsilon_2 &= \rho Z_1 + \sqrt{1 - \rho^2} Z_2 \end{aligned}

If, God forbid, your job requires simulating three correlated stochastic differential equations, say when you are using a Double Heston or a Double Lognormal model, then you would need to simulate three jointly correlated Gaussian random variables.

In that case if the correlation structure is \langle dW_1, dW_2 \rangle = \rho_{12} dt\langle dW_2, dW_3 \rangle = \rho_{23} dt and \langle dW_1, dW_3 \rangle = \rho_{13} dt, then given three uncorrelated Gaussian random variables Z_1, Z_2 and Z_3, one could use Cholesky decomposition to generate generate \epsilon_1, \epsilon_2 and \epsilon_3 as:

\begin{aligned} \epsilon_1 &= Z_1 \\ \epsilon_2 &= \rho_{12} Z_1 + \sqrt{1 - \rho^2_{12}} Z_2 \\ \epsilon_3 &=\rho_{13} Z_1 + \rho^*_{23} Z_2 + \rho^*_{33} Z_3\end{aligned}


\displaystyle \rho^*_{23} = \frac{\rho_{23} - \rho_{12} \rho_{13}}{\sqrt{1 - \rho^2_{12}}} and \displaystyle \rho^*_{33} = \sqrt{\frac{1 - \rho^2_{12} - \rho^2_{23} - \rho^2_{13} + 2 \rho_{12} \rho_{23} \rho_{13}}{1 - \rho^2_{12}}}.

Written by Vineet

January 9, 2013 at 7:35 pm