QuantLib for Mathematica demonstration

Niels Elken Sønderby, May 2003
http://www.nielses.dk/quantlib/mma
http://quantlib.org/

Installation

At this point only binaries for Microsoft Windows are available. It should be quite easy to make QuantLib for Mathematica work on other platforms, but it requires that you know how to compile the C++ files (see install.txt for details).

To install QuantLib for Mathematica, please extract all the files from the distribution ZIP-file (download) and copy the folder QuantLib into your Mathematica application directory. In version 4 on a Microsoft Windows system this is most often c:\Program Files\Wolfram Research\Mathematica\4.0\AddOns\Applications

Loading the package

After installing QuantLib for Mathematica on your system, you can load the package and start the external program by writing:

<< QuantLib`

?QuantLib

Now you have access to a number of new financial functions.

Design principles and the naming of things

One of the principles applied in the development of QuantLib for Mathematica is that it should give Mathematica users easy access to the functionality in QuantLib rather than giving QuantLib users and developers a front-end for QuantLib. This means that the names of the function are following the Mathematica naming conventions rather than reflecting the QuantLib class hierarchy names. I.e. BusinessDayQ instead of isBusinessDay, MonteCarloPath instead of PathGenerator.next().

It is the goal to make QuantLib for Mathematica have the look and feel of a standard Mathematica package, and still expose all the QuantLib functionality with things like instruments, term structures, calendars and finite difference pricing engines in a powerful way. The decision to make the things "the Mathematica way" also means that in principle another back-end than QuantLib could be used. The most obvious example would be to code it all in Mathematica itself, but other C++ toolkits could be used as well.

Valuing options

Let's try to price a European option. First we define the option.

?EuropeanOption

instr = EuropeanOption[110, 0.25, CallOption];

Then we define the model under which it should be priced:

?BlackScholesModel

model = BlackScholesModel[100, 0.10, 0.01, 0.15]

Now we can calculate keyfigures for this instrument.

Keyfigures[instr, model]

A short cut for having the value is:

Value[instr, model]

For the other keyfigures we must use the Mathematica syntax "/." (ReplaceAll):

Delta  /.  Keyfigures[instr, model]

We could now make a function, which gives the price given a certain spot price:

optval[s_] := 
      Value[instr, BlackScholesModel[s, 0.10, 0.01, 0.15]]

Plot[optval[s], {s, 80, 120}]

Or make it depend on both time to maturity and spot price:

optval[s_, t_] := 
      Value[EuropeanOption[110, t, PutOption], 
        BlackScholesModel[s, 0.10, 0.01, 0.15]]

And make a 3D plot:

Plot3D[optval[s, t], {s, 70, 140}, {t, 0.01, 1}]

Same thing with delta:

optdelta[s_, t_] := 
      Delta /.  Keyfigures[EuropeanOption[110, t, PutOption], 
          BlackScholesModel[s, 0.10, 0.01, 0.15]]

Plot3D[optdelta[s, t], {s, 70, 140}, {t, 0.01, 1}, 
      AxesLabel -> {"s", "t", "delta"}, 
      PlotPoints -> 30]

Monte Carlo simulation

Remember how we defined the model:

model

We can simulate a random path under this model with Monte Carlo simulation

?MonteCarloPath

ListPlot[MonteCarloPath[model, 15, 1000], 
      PlotJoined -> True]

Calendar functions

In the financial world dates are of great importance. So are holiday calendars, day counting conventions and rolling conventions. QuantLib for Mathematica makes the calendar and date tools from QuantLib available.

What day was Christmas Day 2002?

DayOfWeek[{2002, 12, 25}]

Was this a business day?

BusinessDayQ[{2002, 12, 25}]

No, it wasn't, but that's because BusinessDayQ by default uses the trans-european holiday calendar TARGET.

Options[BusinessDayQ]

?TARGETCalendar

Instead in Japan, they don't fool around having holidays because of Christmas.

BusinessDayQ[{2002, 12, 25}, 
      HolidayCalendar -> TokyoCalendar]

So "10 days later" mean different thing, depending on, whether you think of calendar days, business days in London or business days in Tokyo.

Options[DaysPlus]

DaysPlus[{2002, 12, 20}, 10]

DaysPlus[{2002, 12, 20}, 10, 
      HolidayCalendar -> LondonCalendar]

DaysPlus[{2002, 12, 20}, 10, 
      HolidayCalendar -> TokyoCalendar]

When counting days different day count conventions can be used.

$DayCountConventions

Options[DaysBetween]

So when counting days between two days, it depends on the day counting convention:

date1 = {2002, 1, 1}; date2 = {2003, 1, 2};

DaysBetween[date1, date2]

DaysBetween[date1, date2, 
      DayCountConvention -> Thirty360European]

This also affects calculations of the fraction of years between the dates:

YearsBetween[date1, date2]

YearsBetween[date1, date2, 
      DayCountConvention -> Thirty360European]

Rolling conventions decides in which direction to change ("roll") e.g. a settlement date, when it falls on a holiday.

HolidayQ[{{2003, 1, 1}, {2003, 1, 2}}, 
      HolidayCalendar -> TokyoCalendar]

In Japan New Years Day is a holiday and January 2nd is also. So if a settlement day falls on this date is must be rolled. The default is Following.

?Following

RollDate[{2003, 1, 1}, 
      HolidayCalendar -> TokyoCalendar]

RollDate[{2003, 1, 1}, HolidayCalendar -> TokyoCalendar, 
      RollingConvention -> Preceding]

Also you can check if a certain year is a leap year.

LeapYearQ[{1800, 1804, 2000, 2002}]

Error handling

At this stage there is not much error handling in QuantLib for Mathematica, but if something goes wrong in the C++ part, it is reported.

DayOfWeek[{2002, 2, 31}]

Value[EuropeanOption[110, 1, CallOption], 
      BlackScholesModel[100, 0.1, 0.01, -0.15]]

Stopping the external program

As we now have stopped using the package, you can stop the external program, if you wish. Though, it will be stopped automatically anyway, when you exit Mathematica (or quit the kernel).

Uninstall[QuantLib]