Monthly Archives: May 2015

How to build a C++ library package in R

I am building an R package that has Boost and QuantLib dependency. It is a library package, which took me a while to compile in R. I thought it might be interesting to share my experience with others. I came up with the following solution that works for a Windows 64-bit platform. Here are the instructions:

  1. Download and install R from: http://cran.r-project.org/bin/windows/base/
  2. Download and install latest version of Rtools from: http://cran.r-project.org/bin/windows/Rtools/
  3. Download and install MikTex from: http://miktex.org/download
  4. Add to System Environment PATH: to R, example: C:\R-3.1.2\bin; to RTools, example: C:\Rtools\bin;c:\Rtools\gcc-4.6.3\bin;C:\Rtools\gcc-4.6.3\i686-w64-mingw32\bin; to perl, example: to miktex, example: C:\Program Files (x86)\MiKTeX 2.9\miktex\bin; Control Panel -> System -> Advanced System Settings -> Advanced -> Environment Variables Edit path variable by appending paths after ;
  5. Download Boost and build it with Rtools
    1. Unpack loaded code
    2. Open command line Win+R -> cmd.exe
    3. Change current folder to your Boost folder. For example, cd C:\libraries\boost_1_57_0 4. Type command: bootstrap mingw
    4. Type command: b2 toolset=gcc address-model=3. Your compiled libraries are now in stage/lib subfolder
    5. Type command: b2 toolset=gcc address-model=64. Your compiled libraries are now in stage/lib subfolder
    6.  In case you have problems due to missing ml, make sure MASM and MASM64 (ml.exe and ml64.exe) are available in PATH. This should be in the C:\Program Files (x86)\Microsoft Visual Studio path
  6. Download QuantLib and build it with your version of Boost headers. You can download QuantLib from http://quantlib.org/ make sure to download the .tar.gz file. Here we have loaded QuantLib-1.5
    1. open msys and run the following instructions
      1. cd C:/Working/QuantLib
      2. tar xzvf QuantLib-1.4.tar.gz. Also make sure that the path to mingw is set to the one in Rtools
    2. open msys, run the following instructions:
      1. cd C:/Working/QuantLib/QuantLib-1.4
      2. configure CXX=’g++ -m32′ CXXFLAGS=’-g -O2 -std=c++0x’ –with-boost-include=C:/Working/boost/boost_1_57_0 –prefix=C:/Working/QuantLib/QuantLib-1.4
    3. Build the same library for x64 architecture: CXX=’g++ -m64′ CXXFLAGS=’-g -O2 -std=c++0x’ Note that you should have around 5GB of free space for successful compilation
  7. Create a folder for your package
    1. Create three folders: R, man, src
    2. Create or update DESCRIPTION file with information about the package, its author, and link libraries
    3. Copy the code you need to build the function into the src folder
    4. If the original code could be compiled without issues with Rtools compiler (mingw), you should only add dllmain.cpp with defined dllentry
    5. Make a file named NAMESPACE in the root directory. There we define which dlls we need for the package to work (useDynLib) and what we should export from them (export or export pattern). Also we should identify which R packets we should import (import)
  8. R package run:
    1. Write wrapper on R to call function from dll
    2. To make documentation, open R. Type install.packages(‘roxygen2’). Change folder to your package root folder. Type roxygen2::roxygenise().
  9. Create file Makevars.win to build your package library in the windows environment. In this file it is very important to set following the variables: SOURCES, OBJECTS, PKG_CPPFLAGS, PKG_LIBS. These variables will be updated as you continue to develop your code. Of course, you can add additional build targets. SOURCES is an enumeration of all cpp files you need to build your function from the project (including dependencies). You should update this variable whenever you add additional cpp files. OBJECTS is an enumeration of object files compiled from SOURCES. PKG_CPPFLAGS is the set of flags used for code compilation, including paths to include files. Note we also use the -std=c++0x flag for Boost compatibility. Here the $(shell RScript -e Rcpp:::CxxFlags()) command helps access R and Rcpp include files. You should update this variable whenever you add new h or hpp files to build. PKG_LIBS is the set of flags used for code linking, including paths to additional libraries. $(shell RScript -e Rcpp:::LdFlags()) helps to link Rcpp libraries. You should update this variable whenever you require the use of a new library in your code
  10. Prepare environment. For some reason in current version of R, makefile for package uses only gcc for linking. And we need to use g++ to support template functionality of boost %.dll: replace line 216 in \etc\i386\Makeconf and in \etc\x64\Makeconf $(SHLIB_LD) -shared $(DLLFLAGS) -o $@ $*.def $^ $(ALL_LIBS) with $(SHLIB_CXXLD) -shared -o $@ $*.def $^ $(ALL_LIBS) or replace line: CC = $(BINPREF)gcc $(M_ARCH) with CC = $(BINPREF)g++ $(M_ARCH)
  11. If you successfully complete all the previous steps, you can verify your package. Open command line Win+R cmd.exe. Change current folder to your package parent folder. Type: R CMD check

On share repurchase programs

A type of structured product that has become fashionable these days is the structured share repurchase program. When a corporate client wants to buy back its own shares, it normally uses its broker or investment bank to buy shares on the open market. The repurchase program will be announced before the start of the program and the announcement may have an impact on the share price.

In a normal share repurchase program, the broker behaves as an agent and buys a fixed amount per day up to a certain notional amount or number of shares. The amount purchased per day depends on the deadline and the target notional or target number of shares. The deadline for the completion of the repurchase program is often quite important for the corporate client and is normally set before the end of a quarterly accounting period or announcement. Depending on whether the corporate client targets a notional amount or a number of shares, the share repurchase program is called fixed notional or fixed shares.

A new structure proposed by investment banks has recently become popular as it can provide cheaper prices for the corporate client. Under these new structures, the maturity and target number of shares or notional amount are fixed, but the total number of shares that are repurchased can vary. The investment bank has the optionality on how many shares to purchase per day as long as it commits itself to finishing the repurchase program by the deadline and not buying back more than a certain number of shares each day. With such programs the investment bank is able to give a discount to the client. At the same time the investment bank will be paid the volume weighted average price (VWAP) over the period minus the discount. A good trader taking advantage of the daily optionality can beat the VWAP average price. Hence in many circumstances it is desirable for the investment bank to not delta hedge its position and keep the option naked in the book so that it can make money out of daily price movements.