Converted to HTML from QuantLibMma-demo.nb

Niels Elken Sønderby, May 2003

http://www.nielses.dk/quantlib/mma

http://quantlib.org/

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

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

** << QuantLib`**

** ? QuantLib**

"QuantLib gives access to various financial functions from the\

QuantLib C++ library. http://www.nielses.dk/quantlib/mma \

Uninstall[QuantLib] can be used to terminate the external\

QuantLibMma program."

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

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.

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

** ? EuropeanOption**

"EuropeanOption[strike, residualtime, type] represents a\

european option of the given type (CallOption or PutOption),\

exercise price (strike) and time to maturity (residualtime)."

** instr = EuropeanOption[110, 0.25, CallOption] ;**

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

** ? BlackScholesModel**

"BlackScholesModel[stockprice, riskfreerate, dividend, vol]\

represents a model where the underlying asset is worth\

stockprice at time zero and the risk free rate, dividend yield\

and volatility are as given."

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

BlackScholesModel[100, 0.1`, 0.01`, 0.15`]

Now we can calculate keyfigures for this instrument.

** Keyfigures[instr, model]**

{Value -> 0.6828593225781923`, Delta -> 0.17489412560177403`,

Gamma -> 0.03432544935471783`, Theta -> -5.367374250563904`,

Vega -> 12.872043508019189`, Rho -> 4.201638309399803`,

DividendRho -> -4.372353140044351`}

A short cut for having the value is:

** Value[instr, model]**

0.6828593225781923`

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

** Delta /. Keyfigures[instr, model]**

0.17489412560177403`

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}]**

- Graphics -

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}]**

- SurfaceGraphics -

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]**

- SurfaceGraphics -

Remember how we defined the model:

** model**

BlackScholesModel[100, 0.1`, 0.01`, 0.15`]

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

** ? MonteCarloPath**

"MonteCarloPath[model, length, timesteps] gives a simulated path\

for the given model under the risk neutral measure."

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

- Graphics -

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}]**

Wednesday

Was this a business day?

** BusinessDayQ[{2002, 12, 25}]**

False

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

** Options[BusinessDayQ]**

{HolidayCalendar -> TARGETCalendar}

** ? TARGETCalendar**

"TARGETCalendar is a holiday calendar representing the\

Trans-European Automated Real-time Gross Express-settlement \

Transfer system calendar.\

http://www.ecb.int/press/00/pr001214_4.htm"

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

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

True

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]**

{HolidayCalendar -> None}

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

{2002, 12, 30}

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

{2003, 1, 8}

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

{2003, 1, 10}

When counting days different day count conventions can be used.

** $DayCountConventions**

{Actual365, Actual360, Thirty360American, Thirty360European,

Thirty360Italian, ActualActualISMA, ActualActualAFB,

ActualActualISDA}

** Options[DaysBetween]**

{DayCountConvention -> Actual365}

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]**

366

** DaysBetween[date1, date2, DayCountConvention -> Thirty360European]**

361

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

** YearsBetween[date1, date2]**

1.0027397260273974`

** YearsBetween[date1, date2, DayCountConvention -> Thirty360European]**

1.0027777777777778`

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]**

{True, True}

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**

"Following is a rolling convention, which adjust to the first\

business day after the given holiday."

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

{2003, 1, 6}

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

{2002, 12, 30}

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

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

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

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}]**

QuantLibMma :: error : Date::Date : day outside month (2)\

day-range Date::Date : month 2[1,28]

$Failed

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

QuantLibMma :: error : negative or zero volatility given

ReplaceAll :: reps : {$Failed}

is neither a list of replacement rules nor a valid dispatch\

table, and so cannot be used for replacing.

Value /. $Failed

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]**