# Using the NAG Toolbox for MATLAB® - Part 5

The NAG Toolbox makes the full functionality of the NAG Library available from within MATLAB. This article is the latest in an occasional series (the previous article is here) that illustrates the Toolbox's capabilities using examples which highlight the use of specific routines within MATLAB applications. Here, we turn our attention to part of the S chapter of the Library, which contains routines for the calculation of approximations to special functions in mathematics and physics. In the Library's latest release, these have been supplemented by new routines that determine prices for so-called *financial options*, and we present an application here that uses these routines to calculate and display option prices in MATLAB (a preliminary description of this application has already appeared in the NAG Blog). Our application (which can be downloaded from here) might be of interest for teaching purposes, or for those readers who are simply curious about the behaviour of option pricing functions.

## Background

We have previously given an elementary introduction to option pricing elsewhere; some salient points are summarised here for the sake of convenience. An option is a contract giving the holder the right (but not the obligation) to buy or sell an asset at an agreed price (called the *strike*) on or before the *expiration* of the option. An option to buy is labelled a *call*, whilst a *put* option allows the holder to sell the asset. The strike is agreed when the holder and seller enter into the option; if the holder chooses to exercise the option, the seller is obliged to sell or buy the asset at this price. The type of option determines the circumstances under which the holder can exercise - thus for example, a *European* option can only be exercised at expiry, while an *American* option can be exercised at any time prior to this date.

Because options are traded on financial markets, some method for determining their value is required. This in turn requires the construction of a model for the time-dependence of the asset price, which is essentially a random walk. The pricing model of Black and Scholes is probably the best-known, although (see below) many others exist. The implementation of some of these models leads to closed-form formulae for option prices, which may be evaluated using routines in the NAG Library.

The option price depends on several parameters (for example, the asset price and the time to expiry), and its sensitivity to changes in these parameters is of interest to financial analysts. These sensitivities can can be formally expressed as partial derivatives which are usually referred to as *the Greeks*, because they are denoted by Greek letters. Thus, for example, the partial derivative of the option price with respect to the asset price is called *Delta*, while *Theta* gives the sensitivity of the option price to the time to expiry. Given a closed-form expression for the option price, explicit formulae for the Greeks can be readily found - for example, those for the Black-Scholes model have been tabulated in our earlier note.

Figure 1: Using a NAG Toolbox routine to calculate option price (upper plot) and Delta (lower plot) as a function of asset (here assumed to be a stock) price for a put option, as given by the Black-Scholes formula with strikePrice = 70, expiryTime = 0.5, volatility = 0.3, interestRate = 0.05 and yieldRate = 0.0.

## NAG Toolbox option pricing routines

Starting with s30aa (which computes the price of a European option according to the Black-Scholes formula), the NAG Toolbox offers a range of MATLAB routines for calculating option prices. For the sake of convenience, each pricing formula - with a few exceptions - is offered in two routines: one that only calculates the price, and one that calculates the Greeks as well (the exceptions are those models for which estimates of the Greeks are not available in the current release of the Toolbox). For example, this code fragment uses s30ab to calculate the price and Greeks for a European option using the Black-Scholes formula:

(see the routine documentation for details about the input and output parameters). We can use this routine to produce (see Figure 1) a plot of the price as a function of asset price, along with one of the Greeks (in this case *Delta*, which is the slope of the upper plot). Figure 1 also shows the option *payoff* (which is related to the difference between the strike price and the stock price), a quantity which is used by the option holder when deciding whether or not to exercise the option (more details about the determination and utilization of the payoff are available here). Figure 2 shows the option price for the same parameter values as those used in Figure 1, but now plotted against time to expiry; we also show the Greek *Theta*, which is the (negative of the) slope of the upper plot in Figure 2.

Figure 2: As Figure 1, but now showing option price (upper plot) and Theta (lower plot) as a function of time to expiry.

As noted above, the option price is a multivariate function, and the way in which its value changes as its dependent parameters' values are altered is of interest. Figure 3 illustrates how the shape of the price curve depends on changes in the value of some of the parameters.

(a) expiryTime = 0.5

(b) volatility = 0.5

(c) interestRate = 0.3

(d) yieldRate = 0.5

Figure 3: Option price as a function of asset price for a put option, as calculated by the Black-Scholes formula. Starting from the set of parameter values used in Figure 1, we first set (a) expiryTime to 0.5, and then - retaining this new value for expiryTime - set volatility to 0.5 (b). In a similar fashion, we subsequently set interestRate to 0.3 (c) and, finally, yieldRate to 0.5 (d).

## Application functionality and user interface

Our previous note described calling the s30 NAG Library routines from within a Microsoft Excel spreadsheet - or more specifically, from a Visual Basic for Applications (VBA) program which is then inserted into a Module Sheet in an Excel Workbook; it is straightforward to invoke routines from a DLL (such as the NAG Library) from VBA. The spreadsheet-based demo allowed the user to select the pricing model and set values for dependent parameters before calling the appropriate NAG routine; it then used Excel's plotting capabilities to display the behaviour of the price and the Greeks, producing graphs similar to those shown in Figures 1-3.

As noted above, we have re-implemented that application in MATLAB; its user interface is shown in Figure 4. We note in passing that the pricing model which has been selected in this snapshot of the application in action is the so-called asset-or-nothing option, which pays the asset price at expiry if that is less than the strike price (for a put option, or if it's greater than the strike for a call). The qualitative difference in the behaviour of different types of option can be seen by comparing the plots in Figure 4 with Figure 1.

Figure 4: The control panel for our demo MATLAB application. The pricing model and option type can be selected via the controls at top left, along with the Greek to be plotted and the choice of the x-axis in the plots (Stock Price or Time To Expiry). Numerical values for the model parameters are set via the controls on the left, while the results are plotted in the graphs on the right. This is displaying the option price and Delta for the same parameter values as Figure 1, but now using the asset-or-nothing pricing model.

Our MATLAB re-implementation has allowed for some extensions - for example, the new user interface allows parameter values to be automatically swept back and forth, which produces animated plots of the price and Greeks that might be useful for teaching purposes, or determining a better understanding of the interrelationship between the parameters that determine the price. Figure 5 shows the control used in our application for setting a numerical parameter value.

Figure 5: An example of the control for setting a numerical parameter value in our application - in this case, the volatility. The value can be entered directly in the text box at top right, or set using the slider. Selecting the Sweep? option at right causes the value to be automatically increased in small increments until the maximum value on the slider is reached, at which point it is decreased towards the slider's minimum. The net result is a continuous cycling back and forth of the value between the two limits.

## Conclusions

In this note, we have continued our exploration of the capabilities of the NAG Toolbox for MATLAB by discussing a demo application that uses some of the routines from the Special Functions chapter of the Toolbox to calculate financial option prices and display them in MATLAB. We have briefly described the way in which the routines are called and presented some of the aspects of the application's user interface, including the functionality for automatically sweeping through values for dependent parameters and observing the variation in the option price as an animation.

The application described in this article may be downloaded from here. The NAG Toolbox for MATLAB is available for both 32 bit and 64 bit versions of Linux and Microsoft Windows; more information about its availability can be found at http://www.nag.com/numeric/MB/start.asp.