NAG Library Chapter Introduction
S – Approximations of Special Functions
1 Scope of the Chapter
This chapter is concerned with the provision of some commonly occurring physical and mathematical functions.
2 Background to the Problems
The majority of the routines in this chapter approximate real-valued functions of a single real argument, and the techniques involved are described in
Section 2.1. In addition the chapter contains routines for elliptic integrals (see
Section 2.2), Bessel and Airy functions of a complex argument (see
Section 2.3), complementary error function of a complex argument, hypergeometric functions and various option pricing routines for use in financial applications.
2.1 Functions of a Single Real Argument
Most of the routines provided for functions of a single real argument have been based on truncated Chebyshev expansions. This method of approximation was adopted as a compromise between the conflicting requirements of efficiency and ease of implementation on many different machine ranges. For details of the reasons behind this choice and the production and testing procedures followed in constructing this chapter see
Schonfelder (1976).
Basically, if the function to be approximated is
, then for
an approximation of the form
is used (
denotes, according to the usual convention, a summation in which the first term is halved), where
is some suitable auxiliary function which extracts any singularities, asymptotes and, if possible, zeros of the function in the range in question and
is a mapping of the general range
to the specific range [
] required by the Chebyshev polynomials,
. For a detailed description of the properties of the Chebyshev polynomials see
Clenshaw (1962) and
Fox and Parker (1968).
The essential property of these polynomials for the purposes of function approximation is that
oscillates between
and it takes its extreme values
times in the interval [
]. Therefore, provided the coefficients
decrease in magnitude sufficiently rapidly the error made by truncating the Chebyshev expansion after
terms is approximately given by
That is, the error oscillates between
and takes its extreme value
times in the interval in question. Now this is just the condition that the approximation be a minimax representation, one which minimizes the maximum error. By suitable choice of the interval, [
], the auxiliary function,
, and the mapping of the independent variable,
, it is almost always possible to obtain a Chebyshev expansion with rapid convergence and hence truncations that provide near minimax polynomial approximations to the required function. The difference between the true minimax polynomial and the truncated Chebyshev expansion is seldom sufficiently great enough to be of significance.
The evaluation of the Chebyshev expansions follows one of two methods. The first and most efficient, and hence the most commonly used, works with the equivalent simple polynomial. The second method, which is used on the few occasions when the first method proves to be unstable, is based directly on the truncated Chebyshev series, and uses backward recursion to evaluate the sum. For the first method, a suitably truncated Chebyshev expansion (truncation is chosen so that the error is less than the
machine precision) is converted to the equivalent simple polynomial. That is, we evaluate the set of coefficients
such that
The polynomial can then be evaluated by the efficient Horner's method of nested multiplications,
This method of evaluation results in efficient routines but for some expansions there is considerable loss of accuracy due to cancellation effects. In these cases the second method is used. It is well known that if
then
and this is always stable. This method is most efficiently implemented by using three variables cyclically and explicitly constructing the recursion.
That is,
The auxiliary functions used are normally functions compounded of simple polynomial (usually linear) factors extracting zeros, and the primary compiler-provided functions, sin, cos, ln, exp, sqrt, which extract singularities and/or asymptotes or in some cases basic oscillatory behaviour, leaving a smooth well-behaved function to be approximated by the Chebyshev expansion which can therefore be rapidly convergent.
The mappings of [] to [] used range from simple linear mappings to the case when is infinite, and considerable improvement in convergence can be obtained by use of a bilinear form of mapping. Another common form of mapping is used when the function is even; that is, it involves only even powers in its expansion. In this case an approximation over the whole interval [] can be provided using a mapping . This embodies the evenness property but the expansion in involves all powers and hence removes the necessity of working with an expansion with half its coefficients zero.
For many of the routines an analysis of the error in principle is given, namely, if
and
are the absolute errors in function and argument and
and
are the corresponding relative errors, then
If we ignore errors that arise in the argument of the function by propagation of data errors, etc., and consider only those errors that result from the fact that a real number is being represented in the computer in floating point form with finite precision, then
is bounded and this bound is independent of the magnitude of
. For example, on an
-digit machine
(This of course implies that the absolute error
is also bounded but the bound is now dependent on
.) However, because of this the last two relations above are probably of more interest. If possible the relative error propagation is discussed; that is, the behaviour of the error amplification factor
is described, but in some cases, such as near zeros of the function which cannot be extracted explicitly, absolute error in the result is the quantity of significance and here the factor
is described. In general, testing of the functions has shown that their error behaviour follows fairly well these theoretical error behaviours. In regions where the error amplification factors are less than or of the order of one, the errors are slightly larger than the above predictions. The errors are here limited largely by the finite precision of arithmetic in the machine, but
is normally no more than a few times greater than the bound on
. In regions where the amplification factors are large, of order ten or greater, the theoretical analysis gives a good measure of the accuracy obtainable.
It should be noted that the definitions and notations used for the functions in this chapter are all taken from
Abramowitz and Stegun (1972). You are strongly recommended to consult this book for details before using the routines in this chapter.
2.2 Approximations to Elliptic Integrals
Four functions provided here are symmetrised variants of the classical (Legendre) elliptic integrals. These alternative definitions have been suggested by
Carlson (1965),
Carlson (1977b) and
Carlson (1977a) and he also developed the basic algorithms used in this chapter.
The symmetrised elliptic integral of the first kind is represented by
where
and at most one may be equal to zero.
The normalization factor,
, is chosen so as to make
If any two of the variables are equal,
degenerates into the second function
where the argument restrictions are now
and
.
This function is related to the logarithm or inverse hyperbolic functions if , and to the inverse circular functions if .
The symmetrised elliptic integral of the second kind is defined by
with
,
and
, but only one of
or
may be zero.
The function is a degenerate special case of the symmetrised elliptic integral of the third kind
with
and
with at most one equality holding. Thus
. The normalization of both these functions is chosen so that
The algorithms used for all these functions are based on duplication theorems. These allow a recursion system to be established which constructs a new set of arguments from the old using a combination of arithmetic and geometric means. The value of the function at the original arguments can then be simply related to the value at the new arguments. These recursive reductions are used until the arguments differ from the mean by an amount small enough for a Taylor series about the mean to give sufficient accuracy when retaining terms of order less than six. Each step of the recurrences reduces the difference from the mean by a factor of four, and as the truncation error is of order six, the truncation error goes like
, where
is the number of iterations.
The above forms can be related to the more traditional canonical forms (see Section 17.2 of
Abramowitz and Stegun (1972)), as follows.
If we write , where , we have
the classical elliptic integral of the first kind:
the classical elliptic integral of the second kind:
the classical elliptic integral of the third kind:
Also the classical complete elliptic integral of the first kind:
the classical complete elliptic integral of the second kind:
For convenience,
Chapter S contains routines to evaluate classical
and symmetrised elliptic integrals.
2.3 Bessel and Airy Functions of a Complex Argument
The routines for Bessel and Airy functions of a real argument are based on Chebyshev expansions, as described in
Section 2.1. The routines provided for functions of a complex argument, however, use different methods. These routines relate all functions to the modified Bessel functions
and
computed in the right-half complex plane, including their analytic continuations.
and
are computed by different methods according to the values of
and
. The methods include power series, asymptotic expansions and Wronskian evaluations. The relations between functions are based on well known formulae (see
Abramowitz and Stegun (1972)).
2.4 Option Pricing Routines
The option pricing routines evaluate the closed form solutions or approximations to the equations that define mathematical models for the prices of selected financial option contracts. These solutions can be viewed as special functions determined by the underlying equations. The terminology associated with these routines arises from their setting in financial markets and is briefly outlined below. See
Joshi (2003) for a comprehensive introduction to this subject. An option is a contract which gives the holder the right, but not the obligation, to buy (if it is a call) or sell (if it is a put) a particular asset,
. A European option can be exercised only at the specified expiry time,
, while an American option can be exercised at any time up to
. For Asian options the average underlying price over a pre-set time period determines the payoff.
The asset is bought (if a call) or sold (if a put) at a pre-specified strike price . Thus, an option contract has a payoff to the holder of for a call or , for a put, which depends on whether the asset price at the time of exercise is above (call) or below (put) the strike, . If at any moment in time a contract is currently showing a theoretical profit then it is deemed ‘in-the-money’; otherwise it is deemed ‘out-of-the-money’.
The option contract itself therefore has a value and, in many cases, can be traded in markets. Mathematical models, such as the Black–Scholes model, give theoretical prices for particular option contracts using a number of assumptions about the behaviour of financial markets. Typically, the price,
, of the underlying asset at time
, is modelled as the solution of a stochastic differential equation for the return,
, on the asset price over a time interval,
,
where
is a Brownian motion. The drift,
, defines the trend in the movements of
, while the volatility,
, measures the risk and may be taken to be the standard deviation of the returns on the asset price. In addition the model requires a riskless money market account or bond with value,
, at time
and risk-free rate,
, such that
This leads to the determination of the Black–Scholes option price,
, by a martingale method or via the derivation of the Black–Scholes partial differential equation,
For this case a closed form solution exists which is evaluated by
S30AAF.
A number of different option types where the solution exists in closed form or as a closed form approximation are presented in this chapter. See
Haug (2007) for an extensive listing of option pricing formulae.
2.5 Hypergeometric Functions
The confluent hypergeometric function (or ) requires a number of techniques to approximate it over the whole parameter space and for all argument values. For well within the unit circle (where say), and for relatively small parameter values, the function can be well approximated by Taylor expansions, continued functions or through the solution of the related ordinary differential equation by an explicit, adaptive integrator. For values of , one of several transformations can be performed (depending on the value of ) to reformulate the problem in terms of a new argument such that . If one or more of the parameters is relatively large (e.g., ) then recurrence relations can be used in combination to reformulate the problem in terms of parameter values of small size (e.g., ).
Approximations to the hypergeometric functions can therefore require all of the above techniques in sequence: a transformation to get an argument well inside the unit circle, a combination of recurrence relations to reduce the parameter sizes, and the approximation of the resulting hypergeometric function by one of a set of approximation techniques.
All the techniques described above are based on those described in
Pearson (2009).
3 Recommendations on Choice and Use of Available Routines
3.1 Vectorized Routine Variants
Many routines in
Chapter S which compute functions of a single real argument have variants which operate on vectors of arguments. For example,
S18AEF computes the value of the
Bessel function for a single argument, and
S18ASF computes the same function for multiple arguments. In general it should be more efficient to use vectorized routines where possible, though to some extent this will depend on the environment from which you call the routines. See
Section 4 for a complete list of vectorized routines.
3.2 Elliptic Integrals
IMPORTANT ADVICE: users who encounter elliptic integrals in the course of their work are strongly recommended to look at transforming their analysis directly to one of the Carlson forms, rather than to the traditional canonical Legendre forms. In general, the extra symmetry of the Carlson forms is likely to simplify the analysis, and these symmetric forms are much more stable to calculate.
The routine
S21BAF for
is largely included as an auxiliary to the other routines for elliptic integrals. This integral essentially calculates elementary functions, e.g.,
In general this method of calculating these elementary functions is not recommended as there are usually much more efficient specific routines available in the Library. However,
S21BAF may be used, for example, to compute
when
is close to
, without the loss of significant figures that occurs when
and
are computed separately.
3.3 Bessel and Airy Functions
For computing the Bessel functions , , and where is real and , special routines are provided, which are much faster than the more general routines that allow a complex argument and arbitrary real . Similarly, special routines are provided for computing the Airy functions and their derivatives , , , for a real argument which are much faster than the routines for complex arguments.
3.4 Confluent Hypergeometric Function
Two routines are provided for the confluent hypergeometric function
. Both return values for
where parameters
and
, and argument
, are all real, but one variant works in a scaled form designed to avoid unnecessary loss of precision. The unscaled routine
S22BAF is easier to use and should be chosen in the first instance, changing to the scaled routine
S22BBF only if problems are encountered.
4 Functionality Index
Ai or Ai ′ , complex argument, optionally scaled | | S17DGF |
Bi or Bi ′ , complex argument, optionally scaled | | S17DHF |
inverse circular cosine | | S09ABF |
inverse hyperbolic cosine | | S11ACF |
inverse hyperbolic sine | | S11ABF |
inverse hyperbolic tangent | | S11AAF |
Iν, complex argument, optionally scaled | | S18DEF |
Jα ± n(z), complex argument | | S18GKF |
Jν, complex argument, optionally scaled | | S17DEF |
Kν, complex argument, optionally scaled | | S18DCF |
Yν, complex argument, optionally scaled | | S17DCF |
Complement of the Cumulative Normal distribution | | S15ACF |
Complement of the Error function, | | |
complex argument, scaled | | S15DDF |
Cumulative Normal distribution function | | S15ABF |
Digamma function, scaled | | S14ADF |
Elliptic functions, Jacobian, sn, cn, dn, | | |
of 2nd kind, F(z , k ′ , a , b) | | S21DAF |
complete of 1st kind, K(m) | | S21BHF |
complete of 2nd kind, E (m) | | S21BJF |
of 3rd kind, Π (n ; ϕ ∣ m) | | S21BGF |
degenerate of 1st kind, RC | | S21BAF |
complex argument, scaled | | S15DDF |
Generalized factorial function | | S14AAF |
Hankel function Hν(1) or Hν(2), | | |
complex argument, optionally scaled | | S17DLF |
Hypergeometric functions, | | |
1F1
(a ; b ; x)
, confluent, real argument | | S22BAF |
1F1(a ; b ; x), confluent, real argument, scaled form | | S22BBF |
Jacobian theta functions θk(x , q), | | |
Legendre functions of 1st kind Pnm(x), Pnm(x) | | S22AAF |
Logarithm of beta function, | | |
Logarithm of gamma function, | | |
American option, Bjerksund and Stensland option price | | S30QCF |
Asian option, geometric continuous average rate price | | S30SAF |
Asian option, geometric continuous average rate price with Greeks | | S30SBF |
binary asset-or-nothing option price | | S30CCF |
binary asset-or-nothing option price with Greeks | | S30CDF |
binary cash-or-nothing option price | | S30CAF |
binary cash-or-nothing option price with Greeks | | S30CBF |
Black–Scholes–Merton option price | | S30AAF |
Black–Scholes–Merton option price with Greeks | | S30ABF |
European option, option prices, using Merton jump-diffusion model | | S30JAF |
European option, option price with Greeks, using Merton jump-diffusion model | | S30JBF |
floating-strike lookback option price | | S30BAF |
floating-strike lookback option price with Greeks | | S30BBF |
Heston's model option price | | S30NAF |
Heston's model option price with Greeks | | S30NBF |
standard barrier option price | | S30FAF |
Psi function derivatives, scaled | | S14ADF |
Scaled modified Bessel function(s), | | |
e − (x)I0(x), real argument, | | |
e − (x)I1(x), real argument, | | |
ex K0 (x), real argument, | | |
ex K1 (x), real argument, | | |
Trigamma function, scaled | | S14ADF |
Zeros of Bessel functions Jα(x), Jα ′ (x), Yα(x), Yα ′ (x), | | |
5 Auxiliary Routines Associated with Library Routine Parameters
None.
6 Routines Withdrawn or Scheduled for Withdrawal
None.
7 References
Abramowitz M and Stegun I A (1972) Handbook of Mathematical Functions (3rd Edition) Dover Publications
Carlson B C (1965) On computing elliptic integrals and functions J. Math. Phys. 44 36–51
Carlson B C (1977a) Special Functions of Applied Mathematics Academic Press
Carlson B C (1977b) Elliptic integrals of the first kind SIAM J. Math. Anal. 8 231–242
Clenshaw C W (1962) Chebyshev Series for Mathematical Functions Mathematical tables HMSO
Fox L and Parker I B (1968) Chebyshev Polynomials in Numerical Analysis Oxford University Press
Haug E G (2007) The Complete Guide to Option Pricing Formulas (2nd Edition) McGraw-Hill
Joshi M S (2003) The Concepts and Practice of Mathematical Finance Cambridge University Press
Pearson J (2009) Computation of hypergeometric functions MSc Dissertation, Mathematical Institute, University of Oxford
Schonfelder J L (1976) The production of special function routines for a multi-machine library Softw. Pract. Exper. 6(1)