# NAG FL Interfaced03ndf (dim1_​blackscholes_​closed)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

d03ndf computes an analytic solution to the Black–Scholes equation for a certain set of option types.

## 2Specification

Fortran Interface
 Subroutine d03ndf ( kopt, x, s, t, tmat, r, q, f, rho,
 Integer, Intent (In) :: kopt Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: x, s, t, tmat, r(*), q(*), sigma(*) Real (Kind=nag_wp), Intent (Out) :: f, theta, delta, gamma, lambda, rho Logical, Intent (In) :: tdpar(3)
#include <nag.h>
 void d03ndf_ (const Integer *kopt, const double *x, const double *s, const double *t, const double *tmat, const logical tdpar[], const double r[], const double q[], const double sigma[], double *f, double *theta, double *delta, double *gamma, double *lambda, double *rho, Integer *ifail)
The routine may be called by the names d03ndf or nagf_pde_dim1_blackscholes_closed.

## 3Description

d03ndf computes an analytic solution to the Black–Scholes equation (see Hull (1989) and Wilmott et al. (1995))
 $∂f ∂t + (r-q) S ∂f ∂S + σ2 S2 2 ∂2f ∂S2 =rf$ (1)
 $Smin< S (2)
for the value $f$ of a European put or call option, or an American call option with zero dividend $q$. In equation (1) $t$ is time, $S$ is the stock price, $X$ is the exercise price, $r$ is the risk free interest rate, $q$ is the continuous dividend, and $\sigma$ is the stock volatility. The parameter $r$, $q$ and $\sigma$ may be either constant, or functions of time. In the latter case their average instantaneous values over the remaining life of the option should be provided to d03ndf. An auxiliary routine d03nef is available to compute such averages from values at a set of discrete times. Equation (1) is subject to different boundary conditions depending on the type of option. For a call option the boundary condition is
 $f (S, t = tmat ) = max (0,S-X)$
where ${t}_{\mathrm{mat}}$ is the maturity time of the option. For a put option the equation (1) is subject to
 $f (S, t = tmat ) = max (0,X-S) .$
d03ndf also returns values of the Greeks
 $Θ= ∂f ∂t , Δ= ∂f ∂x , Γ= ∂2f ∂x2 , Λ= ∂f ∂σ , ρ= ∂f ∂r .$
s30abf also computes the European option price given by the Black–Scholes–Merton formula together with a more comprehensive set of sensitivities (Greeks).
Further details of the analytic solution returned are given in Section 9.1.
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

## 5Arguments

1: $\mathbf{kopt}$Integer Input
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.
Constraints:
• ${\mathbf{kopt}}=1$, $2$ or $3$;
• if $q\ne 0$, ${\mathbf{kopt}}\ne 2$.
2: $\mathbf{x}$Real (Kind=nag_wp) Input
On entry: the exercise price $X$.
Constraint: ${\mathbf{x}}\ge 0.0$.
3: $\mathbf{s}$Real (Kind=nag_wp) Input
On entry: the stock price at which the option value and the Greeks should be evaluated.
Constraint: ${\mathbf{s}}\ge 0.0$.
4: $\mathbf{t}$Real (Kind=nag_wp) Input
On entry: the time at which the option value and the Greeks should be evaluated.
Constraint: ${\mathbf{t}}\ge 0.0$.
5: $\mathbf{tmat}$Real (Kind=nag_wp) Input
On entry: the maturity time of the option.
Constraint: ${\mathbf{tmat}}\ge {\mathbf{t}}$.
6: $\mathbf{tdpar}\left(3\right)$Logical array Input
On entry: specifies whether or not various arguments 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.
7: $\mathbf{r}\left(*\right)$Real (Kind=nag_wp) array Input
Note: the dimension of the array r must be at least $3$ if ${\mathbf{tdpar}}\left(1\right)=\mathrm{.TRUE.}$, and at least $1$ otherwise.
On entry: if ${\mathbf{tdpar}}\left(1\right)=\mathrm{.FALSE.}$ then ${\mathbf{r}}\left(1\right)$ must contain the constant value of $r$. The remaining elements need not be set.
If ${\mathbf{tdpar}}\left(1\right)=\mathrm{.TRUE.}$ then ${\mathbf{r}}\left(1\right)$ must contain the value of $r$ at time t and ${\mathbf{r}}\left(2\right)$ must contain its average instantaneous value over the remaining life of the option:
 $r^=∫ttmatr(ζ)dζ.$
The auxiliary routine d03nef may be used to construct r from a set of values of $r$ at discrete times.
8: $\mathbf{q}\left(*\right)$Real (Kind=nag_wp) array Input
Note: the dimension of the array q must be at least $3$ if ${\mathbf{tdpar}}\left(2\right)=\mathrm{.TRUE.}$, and at least $1$ otherwise.
On entry: if ${\mathbf{tdpar}}\left(2\right)=\mathrm{.FALSE.}$ then ${\mathbf{q}}\left(1\right)$ must contain the constant value of $q$. The remaining elements need not be set.
If ${\mathbf{tdpar}}\left(2\right)=\mathrm{.TRUE.}$ then ${\mathbf{q}}\left(1\right)$ must contain the constant value of $q$ and ${\mathbf{q}}\left(2\right)$ must contain its average instantaneous value over the remaining life of the option:
 $q^=∫ttmatq(ζ)dζ.$
The auxiliary routine d03nef may be used to construct q from a set of values of $q$ at discrete times.
9: $\mathbf{sigma}\left(*\right)$Real (Kind=nag_wp) array Input
Note: the dimension of the array sigma must be at least $3$ if ${\mathbf{tdpar}}\left(3\right)=\mathrm{.TRUE.}$, and at least $1$ otherwise.
On entry: if ${\mathbf{tdpar}}\left(3\right)=\mathrm{.FALSE.}$ then ${\mathbf{sigma}}\left(1\right)$ must contain the constant value of $\sigma$. The remaining elements need not be set.
If ${\mathbf{tdpar}}\left(3\right)=\mathrm{.TRUE.}$ then ${\mathbf{sigma}}\left(1\right)$ must contain the value of $\sigma$ at time t, ${\mathbf{sigma}}\left(2\right)$ the average instantaneous value $\stackrel{^}{\sigma }$, and ${\mathbf{sigma}}\left(3\right)$ the second-order average $\overline{\sigma }$, where:
 $σ^=∫ttmatσ(ζ)dζ,$
 $σ¯= (∫ttmatσ2(ζ)dζ) 1/2 .$
The auxiliary routine d03nef may be used to compute sigma from a set of values at discrete times.
Constraints:
• if ${\mathbf{tdpar}}\left(3\right)=\mathrm{.FALSE.}$, ${\mathbf{sigma}}\left(1\right)>0.0$;
• if ${\mathbf{tdpar}}\left(3\right)=\mathrm{.TRUE.}$, ${\mathbf{sigma}}\left(\mathit{i}\right)>0.0$, for $\mathit{i}=1,2,3$.
10: $\mathbf{f}$Real (Kind=nag_wp) Output
On exit: the value $f$ of the option at the stock price s and time t.
11: $\mathbf{theta}$Real (Kind=nag_wp) Output
12: $\mathbf{delta}$Real (Kind=nag_wp) Output
13: $\mathbf{gamma}$Real (Kind=nag_wp) Output
14: $\mathbf{lambda}$Real (Kind=nag_wp) Output
15: $\mathbf{rho}$Real (Kind=nag_wp) Output
On exit: the values of various Greeks at the stock price s and time t, as follows:
 $theta=Θ= ∂f ∂t , delta=Δ= ∂f ∂s , gamma=Γ= ∂2f ∂s2 , lambda=Λ= ∂f ∂σ , rho=ρ= ∂f ∂r .$
16: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ 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).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-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}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{kopt}}=1$, $2$ or $3$.
On entry, ${\mathbf{q}}\left(1\right)$ is not equal to $0.0$ with American call option. ${\mathbf{q}}\left(1\right)=⟨\mathit{\text{value}}⟩$.
On entry, ${\mathbf{s}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{s}}\ge 0.0$.
On entry, ${\mathbf{sigma}}\left(⟨\mathit{\text{value}}⟩\right)=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{sigma}}\left(\mathit{i}\right)>0.0$.
On entry, ${\mathbf{t}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{t}}\ge 0.0$.
On entry, ${\mathbf{tmat}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{t}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{tmat}}\ge {\mathbf{t}}$.
On entry, ${\mathbf{x}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{x}}\ge 0.0$.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

Given accurate values of r, q and sigma no further approximations are made in the evaluation of the Black–Scholes analytic formulae, and the results should, therefore, be within machine accuracy. The values of r, q and sigma returned from d03nef are exact for polynomials of degree up to $3$.

## 8Parallelism and Performance

d03ndf is not threaded in any implementation.

### 9.1Algorithmic Details

The Black–Scholes analytic formulae are used to compute the solution. For a European call option these are as follows:
 $f= S e -q^ (T-t) N (d1)- X e -r^ (T-t) N(d2)$
where
 $d1 = log(S/X)+(r^-q^+σ¯2/2)(T-t) σ¯T-t , d2 = log(S/X)+(r^-q^-σ¯2/2)(T-t) σ¯T-t =d1-σ¯T-t,$
$N\left(x\right)$ is the cumulative Normal distribution function and ${N}^{\prime }\left(x\right)$ is its derivative
 $N(x) = 12π ∫-∞x e-ζ2/2dζ, N′(x) = 12π e-x2/2.$
The functions $\stackrel{^}{q}$, $\stackrel{^}{r}$, $\stackrel{^}{\sigma }$ and $\overline{\sigma }$ are average values of $q$, $r$ and $\sigma$ over the time to maturity:
 $q^ = 1T-t ∫tT q(ζ)dζ, r^ = 1T-t ∫tT r(ζ)dζ, σ^ = 1T-t ∫tT σ(ζ)dζ, σ¯ = ( 1T-t ∫tTσ2(ζ)dζ) 1/2 .$
The Greeks are then calculated as follows:
 $Δ = ∂f ∂S =e-q^(T-t) N(d1)+ Se-q^(T-t) N′(d1)-Xe-r^(T-t) N′(d2) σ¯S⁢T-t , Γ = ∂2 f ∂S2 = Se-q^(T-t) N′(d1)+Xe-r^(T-t) N′(d2) σ¯S2T-t + Se-q^(T-t) N′(d1)-Xe-r^(T-t) N′(d2) σ¯2S2(T-t) , Θ = ∂f ∂t =rf+(q-r) SΔ- σ2 S22Γ, Λ = ∂f ∂σ =( X d1 e-r^(T-t) N′(d2)-S d2 e-q^(T-t) N′(d1) σ¯2 ) σ^, ρ = ∂f ∂r =X(T-t) e-r^(T-t) N(d2)+ (Se-q^(T-t)N′(d1)-Xe-r^(T-t)N′(d2)) T-tσ¯.$
Note: that $\Theta$ is obtained from substitution of other Greeks in the Black–Scholes partial differential equation, rather than differentiation of $f$. The values of $q$, $r$ and $\sigma$ appearing in its definition are the instantaneous values, not the averages. Note also that both the first-order average $\stackrel{^}{\sigma }$ and the second-order average $\overline{\sigma }$ appear in the expression for $\Lambda$. This results from the fact that $\Lambda$ is the derivative of $f$ with respect to $\sigma$, not $\stackrel{^}{\sigma }$.
For a European put option the equivalent equations are:
 $f = Xe-r^(T-t) N(-d2)-Se-q^(T-t) N(-d1), Δ = ∂f ∂S =-e-q^(T-t) N(-d1)+ Se-q^(T-t) N′(-d1)-Xe-r^(T-t) N′(-d2) σ¯S⁢T-t , Γ = ∂2f ∂S2 = Xe-r^(T-t) N′(-d2)+Se-q^(T-t) N′(-d1) σ¯S2T-t + Xe-r^(T-t) N′′(-d2)-Se-q^(T-t) N′′(-d1) σ¯2S2(T-t) , Θ = ∂f ∂t =rf+(q-r)SΔ- σ2S22Γ, Λ = ∂f ∂σ =( Xd1 e-r^(T-t) N′(-d2)-S d2 e-q^(T-t) N′(-d1) σ¯2 ) σ^, ρ = ∂f ∂r =-X(T-t) e-r^(T-t) N(-d2)+ (Se-q^(T-t)N′(-d1)-Xe-r^(T-t)N′(-d2)) T-tσ^.$
The analytic solution for an American call option with $q=0$ is identical to that for a European call, since early exercise is never optimal in this case. For all other cases no analytic solution is known.

## 10Example

This example solves the Black–Scholes equation for valuation of a $5$-month American call 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.
The option is valued at a range of times and stock prices.

### 10.1Program Text

Program Text (d03ndfe.f90)

### 10.2Program Data

Program Data (d03ndfe.d)

### 10.3Program Results

Program Results (d03ndfe.r)