D03 Chapter Contents
D03 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentD03NCF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

D03NCF solves the Black–Scholes equation for financial option pricing using a finite difference scheme.

## 2  Specification

 SUBROUTINE D03NCF ( KOPT, X, MESH, NS, S, NT, T, TDPAR, R, Q, SIGMA, ALPHA, NTKEEP, F, THETA, DELTA, GAMMA, LAMBDA, RHO, LDF, WORK, IWORK, IFAIL)
 INTEGER KOPT, NS, NT, NTKEEP, LDF, IWORK(NS), IFAIL REAL (KIND=nag_wp) X, S(NS), T(NT), R(*), Q(*), SIGMA(*), ALPHA, F(LDF,NTKEEP), THETA(LDF,NTKEEP), DELTA(LDF,NTKEEP), GAMMA(LDF,NTKEEP), LAMBDA(LDF,NTKEEP), RHO(LDF,NTKEEP), WORK(4*NS) LOGICAL TDPAR(3) CHARACTER(1) MESH

## 3  Description

D03NCF solves the Black–Scholes equation (see Hull (1989) and Wilmott et al. (1995))
 $∂f ∂t +r-qS ∂f ∂S +σ2S22 ∂2f ∂S2 =rf$ (1)
 $Smin (2)
for the value $f$ of a European or American, put or call stock option, with exercise price $X$. In equation (1) $t$ is time, $S$ is the stock price, $r$ is the risk free interest rate, $q$ is the continuous dividend, and $\sigma$ is the stock volatility. According to the values in the array TDPAR, the parameters $r$, $q$ and $\sigma$ may each be either constant or functions of time. The routine also returns values of various Greeks.
D03NCF uses a finite difference method with a choice of time-stepping schemes. The method is explicit for ${\mathbf{ALPHA}}=0.0$ and implicit for nonzero values of ALPHA. Second order time accuracy can be obtained by setting ${\mathbf{ALPHA}}=0.5$. According to the value of the parameter MESH the finite difference mesh may be either uniform, or user-defined in both $S$ and $t$ directions.

## 4  References

Hull J (1989) Options, Futures and Other Derivative Securities Prentice–Hall
Wilmott P, Howison S and Dewynne J (1995) The Mathematics of Financial Derivatives Cambridge University Press

## 5  Parameters

1:     KOPT – INTEGERInput
On entry: specifies the kind of option to be valued.
${\mathbf{KOPT}}=1$
A European call option.
${\mathbf{KOPT}}=2$
An American call option.
${\mathbf{KOPT}}=3$
A European put option.
${\mathbf{KOPT}}=4$
An American put option.
Constraint: ${\mathbf{KOPT}}=1$, $2$, $3$ or $4$.
2:     X – REAL (KIND=nag_wp)Input
On entry: the exercise price $X$.
3:     MESH – CHARACTER(1)Input
On entry: indicates the type of finite difference mesh to be used:
${\mathbf{MESH}}=\text{'U'}$
Uniform mesh.
${\mathbf{MESH}}=\text{'C'}$
Custom mesh supplied by you.
Constraint: ${\mathbf{MESH}}=\text{'U'}$ or $\text{'C'}$.
4:     NS – INTEGERInput
On entry: the number of stock prices to be used in the finite difference mesh.
Constraint: ${\mathbf{NS}}\ge 2$.
5:     S(NS) – REAL (KIND=nag_wp) arrayInput/Output
On entry: if ${\mathbf{MESH}}=\text{'C'}$, ${\mathbf{S}}\left(\mathit{i}\right)$ must contain the $\mathit{i}$th stock price in the mesh, for $\mathit{i}=1,2,\dots ,{\mathbf{NS}}$. These values should be in increasing order, with ${\mathbf{S}}\left(1\right)={S}_{\mathrm{min}}$ and ${\mathbf{S}}\left({\mathbf{NS}}\right)={S}_{\mathrm{max}}$.
If ${\mathbf{MESH}}=\text{'U'}$, ${\mathbf{S}}\left(1\right)$ must be set to ${S}_{\mathrm{min}}$ and ${\mathbf{S}}\left({\mathbf{NS}}\right)$ to ${S}_{\mathrm{max}}$, but ${\mathbf{S}}\left(2\right),{\mathbf{S}}\left(3\right),\dots ,{\mathbf{S}}\left({\mathbf{NS}}-1\right)$ need not be initialized, as they will be set internally by the routine in order to define a uniform mesh.
On exit: if ${\mathbf{MESH}}=\text{'U'}$, the elements of S define a uniform mesh over $\left[{S}_{\mathrm{min}},{S}_{\mathrm{max}}\right]$.
If ${\mathbf{MESH}}=\text{'C'}$, the elements of S are unchanged.
Constraints:
• if ${\mathbf{MESH}}=\text{'C'}$, ${\mathbf{S}}\left(1\right)\ge 0.0$ and ${\mathbf{S}}\left(\mathit{i}\right)<{\mathbf{S}}\left(\mathit{i}+1\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{NS}}-1$;
• if ${\mathbf{MESH}}=\text{'U'}$, $0.0\le {\mathbf{S}}\left(1\right)<{\mathbf{S}}\left({\mathbf{NS}}\right)$.
6:     NT – INTEGERInput
On entry: the number of time-steps to be used in the finite difference method.
Constraint: ${\mathbf{NT}}\ge 2$.
7:     T(NT) – REAL (KIND=nag_wp) arrayInput/Output
On entry: if ${\mathbf{MESH}}=\text{'C'}$ then ${\mathbf{T}}\left(\mathit{j}\right)$ must contain the $\mathit{j}$th time in the mesh, for $\mathit{j}=1,2,\dots ,{\mathbf{NT}}$. These values should be in increasing order, with ${\mathbf{T}}\left(1\right)={t}_{\mathrm{min}}$ and ${\mathbf{T}}\left({\mathbf{NT}}\right)={t}_{\mathrm{max}}$.
If ${\mathbf{MESH}}=\text{'U'}$ then ${\mathbf{T}}\left(1\right)$ must be set to ${t}_{\mathrm{min}}$ and ${\mathbf{T}}\left({\mathbf{NT}}\right)$ to ${t}_{\mathrm{max}}$, but ${\mathbf{T}}\left(2\right),{\mathbf{T}}\left(3\right),\dots ,{\mathbf{T}}\left({\mathbf{NT}}-1\right)$ need not be initialized, as they will be set internally by the routine in order to define a uniform mesh.
On exit: if ${\mathbf{MESH}}=\text{'U'}$, the elements of T define a uniform mesh over $\left[{t}_{\mathrm{min}},{t}_{\mathrm{max}}\right]$.
If ${\mathbf{MESH}}=\text{'C'}$, the elements of T are unchanged.
Constraints:
• if ${\mathbf{MESH}}=\text{'C'}$, ${\mathbf{T}}\left(1\right)\ge 0.0$ and ${\mathbf{T}}\left(\mathit{j}\right)<{\mathbf{T}}\left(\mathit{j}+1\right)$, for $\mathit{j}=1,2,\dots ,{\mathbf{NT}}-1$;
• if ${\mathbf{MESH}}=\text{'U'}$, $0.0\le {\mathbf{T}}\left(1\right)<{\mathbf{T}}\left({\mathbf{NT}}\right)$.
8:     TDPAR($3$) – LOGICAL arrayInput
On entry: specifies whether or not various parameters are time-dependent. More precisely, $r$ is time-dependent if ${\mathbf{TDPAR}}\left(1\right)=\mathrm{.TRUE.}$ and constant otherwise. Similarly, ${\mathbf{TDPAR}}\left(2\right)$ specifies whether $q$ is time-dependent and ${\mathbf{TDPAR}}\left(3\right)$ specifies whether $\sigma$ is time-dependent.
9:     R($*$) – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array R must be at least ${\mathbf{NT}}$ if ${\mathbf{TDPAR}}\left(1\right)=\mathrm{.TRUE.}$, and at least $1$ otherwise.
On entry: if ${\mathbf{TDPAR}}\left(1\right)=\mathrm{.TRUE.}$ then ${\mathbf{R}}\left(\mathit{j}\right)$ must contain the value of the risk-free interest rate $r\left(t\right)$ at the $\mathit{j}$th time in the mesh, for $\mathit{j}=1,2,\dots ,{\mathbf{NT}}$.
If ${\mathbf{TDPAR}}\left(1\right)=\mathrm{.FALSE.}$ then ${\mathbf{R}}\left(1\right)$ must contain the constant value of the risk-free interest rate $r$. The remaining elements need not be set.
10:   Q($*$) – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array Q must be at least ${\mathbf{NT}}$ if ${\mathbf{TDPAR}}\left(2\right)=\mathrm{.TRUE.}$, and at least $1$ otherwise.
On entry: if ${\mathbf{TDPAR}}\left(2\right)=\mathrm{.TRUE.}$ then ${\mathbf{Q}}\left(\mathit{j}\right)$ must contain the value of the continuous dividend $q\left(t\right)$ at the $\mathit{j}$th time in the mesh, for $\mathit{j}=1,2,\dots ,{\mathbf{NT}}$.
If ${\mathbf{TDPAR}}\left(2\right)=\mathrm{.FALSE.}$ then ${\mathbf{Q}}\left(1\right)$ must contain the constant value of the continuous dividend $q$. The remaining elements need not be set.
11:   SIGMA($*$) – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array SIGMA must be at least ${\mathbf{NT}}$ if ${\mathbf{TDPAR}}\left(3\right)=\mathrm{.TRUE.}$, and at least $1$ otherwise.
On entry: if ${\mathbf{TDPAR}}\left(3\right)=\mathrm{.TRUE.}$ then ${\mathbf{SIGMA}}\left(\mathit{j}\right)$ must contain the value of the volatility $\sigma \left(t\right)$ at the $\mathit{j}$th time in the mesh, for $\mathit{j}=1,2,\dots ,{\mathbf{NT}}$.
If ${\mathbf{TDPAR}}\left(3\right)=\mathrm{.FALSE.}$ then ${\mathbf{SIGMA}}\left(1\right)$ must contain the constant value of the volatility $\sigma$. The remaining elements need not be set.
12:   ALPHA – REAL (KIND=nag_wp)Input
On entry: the value of $\lambda$ to be used in the time-stepping scheme. Typical values include:
${\mathbf{ALPHA}}=0.0$
Explicit forward Euler scheme.
${\mathbf{ALPHA}}=0.5$
Implicit Crank–Nicolson scheme.
${\mathbf{ALPHA}}=1.0$
Implicit backward Euler scheme.
The value $0.5$ gives second-order accuracy in time. Values greater than $0.5$ give unconditional stability. Since $0.5$ is at the limit of unconditional stability this value does not damp oscillations.
Suggested value: ${\mathbf{ALPHA}}=0.55$.
Constraint: $0.0\le {\mathbf{ALPHA}}\le 1.0$.
13:   NTKEEP – INTEGERInput
On entry: the number of solutions to be stored in the time direction. The routine calculates the solution backwards from ${\mathbf{T}}\left({\mathbf{NT}}\right)$ to ${\mathbf{T}}\left(1\right)$ at all times in the mesh. These time solutions and the corresponding Greeks will be stored at times ${\mathbf{T}}\left(\mathit{i}\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{NTKEEP}}$, in the arrays F, THETA, DELTA, GAMMA, LAMBDA and RHO. Other time solutions will be discarded. To store all time solutions set ${\mathbf{NTKEEP}}={\mathbf{NT}}$.
Constraint: $1\le {\mathbf{NTKEEP}}\le {\mathbf{NT}}$.
14:   F(LDF,NTKEEP) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mathbf{F}}\left(\mathit{i},\mathit{j}\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{NS}}$ and $\mathit{j}=1,2,\dots ,{\mathbf{NTKEEP}}$, contains the value $f$ of the option at the $\mathit{i}$th mesh point ${\mathbf{S}}\left(\mathit{i}\right)$ at time ${\mathbf{T}}\left(\mathit{j}\right)$.
15:   THETA(LDF,NTKEEP) – REAL (KIND=nag_wp) arrayOutput
16:   DELTA(LDF,NTKEEP) – REAL (KIND=nag_wp) arrayOutput
17:   GAMMA(LDF,NTKEEP) – REAL (KIND=nag_wp) arrayOutput
18:   LAMBDA(LDF,NTKEEP) – REAL (KIND=nag_wp) arrayOutput
19:   RHO(LDF,NTKEEP) – REAL (KIND=nag_wp) arrayOutput
On exit: the values of various Greeks at the $i$th mesh point ${\mathbf{S}}\left(i\right)$ at time ${\mathbf{T}}\left(j\right)$, as follows:
 $THETAij= ∂f ∂t , DELTAij= ∂f ∂S , GAMMAij= ∂2f ∂S2 , LAMBDAij= ∂f ∂σ , RHOij= ∂f ∂r .$
20:   LDF – INTEGERInput
On entry: the first dimension of the arrays F, THETA, DELTA, GAMMA, LAMBDA and RHO as declared in the (sub)program from which D03NCF is called.
Constraint: ${\mathbf{LDF}}\ge {\mathbf{NS}}$.
21:   WORK($4×{\mathbf{NS}}$) – REAL (KIND=nag_wp) arrayWorkspace
22:   IWORK(NS) – INTEGER arrayWorkspace
23:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{KOPT}}<1$, or ${\mathbf{KOPT}}>4$, or ${\mathbf{MESH}}\ne \text{'U'}$ or $\text{'C'}$, or ${\mathbf{NS}}<2$, or ${\mathbf{NT}}<2$, or ${\mathbf{S}}\left(1\right)<0.0$, or ${\mathbf{T}}\left(1\right)<0.0$, or ${\mathbf{ALPHA}}<0.0$, or ${\mathbf{ALPHA}}>1.0$, or ${\mathbf{NTKEEP}}<1$, or ${\mathbf{NTKEEP}}>{\mathbf{NT}}$, or ${\mathbf{LDF}}<{\mathbf{NS}}$.
${\mathbf{IFAIL}}=2$
${\mathbf{MESH}}=\text{'U'}$ and the constraints:
• ${\mathbf{S}}\left(1\right)<{\mathbf{S}}\left({\mathbf{NS}}\right)$,
• ${\mathbf{T}}\left(1\right)<{\mathbf{T}}\left({\mathbf{NT}}\right)$
are violated. Thus the end points of the uniform mesh are not in order.
${\mathbf{IFAIL}}=3$
${\mathbf{MESH}}=\text{'C'}$ and the constraints:
• ${\mathbf{S}}\left(\mathit{i}\right)<{\mathbf{S}}\left(\mathit{i}+1\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{NS}}-1$,
• ${\mathbf{T}}\left(\mathit{i}\right)<{\mathbf{T}}\left(\mathit{i}+1\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{NT}}-1$
are violated. Thus the mesh points are not in order.

## 7  Accuracy

The accuracy of the solution $f$ and the various derivatives returned by the routine is dependent on the values of NS and NT supplied, the distribution of the mesh points, and the value of ALPHA chosen. For most choices of ALPHA the solution has a truncation error which is second-order accurate in $S$ and first order accurate in $t$. For ${\mathbf{ALPHA}}=0.5$ the truncation error is also second-order accurate in $t$.
The simplest approach to improving the accuracy is to increase the values of both NS and NT.

### 8.1  Timing

Each time-step requires the construction and solution of a tridiagonal system of linear equations. To calculate each of the derivatives LAMBDA and RHO requires a repetition of the entire solution process. The time taken for a call to the routine is therefore proportional to ${\mathbf{NS}}×{\mathbf{NT}}$.

### 8.2  Algorithmic Details

D03NCF solves equation (1) using a finite difference method. The solution is computed backwards in time from ${t}_{\mathrm{max}}$ to ${t}_{\mathrm{min}}$ using a $\lambda$ scheme, which is implicit for all nonzero values of $\lambda$, and is unconditionally stable for values of $\lambda >0.5$. For each time-step a tridiagonal system is constructed and solved to obtain the solution at the earlier time. For the explicit scheme ($\lambda =0$) this tridiagonal system degenerates to a diagonal matrix and is solved trivially. For American options the solution at each time-step is inspected to check whether early exercise is beneficial, and amended accordingly.
To compute the arrays LAMBDA and RHO, which are derivatives of the stock value $f$ with respect to the problem parameters $\sigma$ and $r$ respectively, the entire solution process is repeated with perturbed values of these parameters.

## 9  Example

This example, taken from Hull (1989), solves the one-dimensional Black–Scholes equation for valuation of a $5$-month American put option on a non-dividend-paying stock with an exercise price of \$50. The risk-free interest rate is 10% per annum, and the stock volatility is 40% per annum.
A fully implicit backward Euler scheme is used, with a mesh of $20$ stock price intervals and $10$ time intervals.

### 9.1  Program Text

Program Text (d03ncfe.f90)

### 9.2  Program Data

Program Data (d03ncfe.d)

### 9.3  Program Results

Program Results (d03ncfe.r)