D03NCF (PDF version)
D03 Chapter Contents
D03 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

D03NCF

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.

+ Contents

    1  Purpose
    7  Accuracy

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<S<Smax,  tmin<t<tmax, (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 σ is the stock volatility. According to the values in the array TDPAR, the parameters r, q and σ 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 ALPHA=0.0 and implicit for nonzero values of ALPHA. Second order time accuracy can be obtained by setting 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.
KOPT=1
A European call option.
KOPT=2
An American call option.
KOPT=3
A European put option.
KOPT=4
An American put option.
Constraint: 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:
MESH='U'
Uniform mesh.
MESH='C'
Custom mesh supplied by you.
Constraint: MESH='U' or 'C'.
4:     NS – INTEGERInput
On entry: the number of stock prices to be used in the finite difference mesh.
Constraint: NS2.
5:     S(NS) – REAL (KIND=nag_wp) arrayInput/Output
On entry: if MESH='C', Si must contain the ith stock price in the mesh, for i=1,2,,NS. These values should be in increasing order, with S1=Smin and SNS=Smax.
If MESH='U', S1 must be set to Smin and SNS to Smax, but S2,S3,,SNS-1 need not be initialized, as they will be set internally by the routine in order to define a uniform mesh.
On exit: if MESH='U', the elements of S define a uniform mesh over Smin,Smax.
If MESH='C', the elements of S are unchanged.
Constraints:
  • if MESH='C', S10.0 and Si<Si+1, for i=1,2,,NS-1;
  • if MESH='U', 0.0S1<SNS.
6:     NT – INTEGERInput
On entry: the number of time-steps to be used in the finite difference method.
Constraint: NT2.
7:     T(NT) – REAL (KIND=nag_wp) arrayInput/Output
On entry: if MESH='C' then Tj must contain the jth time in the mesh, for j=1,2,,NT. These values should be in increasing order, with T1=tmin and TNT=tmax.
If MESH='U' then T1 must be set to tmin and TNT to tmax, but T2,T3,,TNT-1 need not be initialized, as they will be set internally by the routine in order to define a uniform mesh.
On exit: if MESH='U', the elements of T define a uniform mesh over tmin,tmax.
If MESH='C', the elements of T are unchanged.
Constraints:
  • if MESH='C', T10.0 and Tj<Tj+1, for j=1,2,,NT-1;
  • if MESH='U', 0.0T1<TNT.
8:     TDPAR(3) – LOGICAL arrayInput
On entry: specifies whether or not various parameters are time-dependent. More precisely, r is time-dependent if TDPAR1=.TRUE. and constant otherwise. Similarly, TDPAR2 specifies whether q is time-dependent and TDPAR3 specifies whether σ is time-dependent.
9:     R(*) – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array R must be at least NT if TDPAR1=.TRUE., and at least 1 otherwise.
On entry: if TDPAR1=.TRUE. then Rj must contain the value of the risk-free interest rate rt at the jth time in the mesh, for j=1,2,,NT.
If TDPAR1=.FALSE. then R1 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 NT if TDPAR2=.TRUE., and at least 1 otherwise.
On entry: if TDPAR2=.TRUE. then Qj must contain the value of the continuous dividend qt at the jth time in the mesh, for j=1,2,,NT.
If TDPAR2=.FALSE. then Q1 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 NT if TDPAR3=.TRUE., and at least 1 otherwise.
On entry: if TDPAR3=.TRUE. then SIGMAj must contain the value of the volatility σt at the jth time in the mesh, for j=1,2,,NT.
If TDPAR3=.FALSE. then SIGMA1 must contain the constant value of the volatility σ. The remaining elements need not be set.
12:   ALPHA – REAL (KIND=nag_wp)Input
On entry: the value of λ to be used in the time-stepping scheme. Typical values include:
ALPHA=0.0
Explicit forward Euler scheme.
ALPHA=0.5
Implicit Crank–Nicolson scheme.
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: ALPHA=0.55.
Constraint: 0.0ALPHA1.0.
13:   NTKEEP – INTEGERInput
On entry: the number of solutions to be stored in the time direction. The routine calculates the solution backwards from TNT to T1 at all times in the mesh. These time solutions and the corresponding Greeks will be stored at times Ti, for i=1,2,,NTKEEP, in the arrays F, THETA, DELTA, GAMMA, LAMBDA and RHO. Other time solutions will be discarded. To store all time solutions set NTKEEP=NT.
Constraint: 1NTKEEPNT.
14:   F(LDF,NTKEEP) – REAL (KIND=nag_wp) arrayOutput
On exit: Fij, for i=1,2,,NS and j=1,2,,NTKEEP, contains the value f of the option at the ith mesh point Si at time Tj.
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 ith mesh point Si at time Tj, 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: LDFNS.
21:   WORK(4×NS) – REAL (KIND=nag_wp) arrayWorkspace
22:   IWORK(NS) – INTEGER arrayWorkspace
23:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ 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​ 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 -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry 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:
IFAIL=1
On entry,KOPT<1,
orKOPT>4,
orMESH'U' or 'C',
orNS<2,
orNT<2,
orS1<0.0,
orT1<0.0,
orALPHA<0.0,
orALPHA>1.0,
orNTKEEP<1,
orNTKEEP>NT,
orLDF<NS.
IFAIL=2
MESH='U' and the constraints:
  • S1<SNS,
  • T1<TNT
are violated. Thus the end points of the uniform mesh are not in order.
IFAIL=3
MESH='C' and the constraints:
  • Si<Si+1, for i=1,2,,NS-1,
  • Ti<Ti+1, for i=1,2,,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 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  Further Comments

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 NS×NT.

8.2  Algorithmic Details

D03NCF solves equation (1) using a finite difference method. The solution is computed backwards in time from tmax to tmin using a λ scheme, which is implicit for all nonzero values of λ, and is unconditionally stable for values of λ>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 (λ=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 σ 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)

Produced by GNUPLOT 4.4 patchlevel 0 -40 -20 0 20 40 0 20 40 60 80 100 -10 -5 0 5 10 15 Option Values Derivatives Stock Price Example Program Option Values and Derivatives at 5 Months to Maturity option values q d g l r
Produced by GNUPLOT 4.4 patchlevel 0 -40 -20 0 20 40 0 20 40 60 80 100 -8 -6 -4 -2 0 2 4 6 8 10 12 Option Values Derivatives Stock Price Option Values and Derivatives at 3.5 Months to Maturity option values q d g l r

D03NCF (PDF version)
D03 Chapter Contents
D03 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2012