How many Ways are there to compute Derivatives of a Function?
Differentiation, Sensitivities (greeks), gradients, Jacobians etc.
The computation of derivatives and gradients of scalar and vector-valued functions is a pervasive and ongoing activity in many areas of computational finance, optimisation, engineering and Machine Learning (ML), to name just a few. In general, we may need to compute derivatives as part of some algorithm or software program. Some specific applications are:
. Computing the Black Scholes greeks/sensitivities.
. Hedge ratios in fixed income and credit risk applications (for example, vega, duration and complexity).
. Optimisation of design shape parameters.
. Computing derivatives in Neural Network (NN) algorithms, for example (Stochastic) Gradient Descent Method (SGD).
. Hedge ratios for stochastic differential equations (SDE), using Automatic Differentiation (AD), for example.
. Global optimisation of (Langevin) SDEs and gradient ODE systems.
. Applications (many) in which gradients, Jacobians and Hessians need to be calculated.
. Robust calibration and volatility surfaces.
There are several established methods to address the problem of computing the derivatives of functions:
. Analytically (using calculus mainly).
. Using finite differences (or to be more precise, divided differences) to approximate derivatives (this is a well-known approach in PDE models).
. Automatic (Algorithmic) Differentiation (AD).
. Continuous Sensitivity Equation (CSE). In this case we view the derivative of a function (with respect to a parameter) as a quantity that satisfies an ordinary or partial differential equation with auxiliary initial and boundary conditions.
. Approximating the function whose derivatives we wish to compute by an ‘easier’ function, for example a cubic spline whose derivatives are easily computed.
. The Complex Step Method (CSM). This little known but robust method to compute derivatives using function values alone (and no need to take divided differences). The method employs complex numbers and complex arithmetic.
There is no golden rule as to which of the above methods to use in general because the choice depends on the requirements and context. In general, we wish a given method to be suitable for the problem at hand and ideally it should be efficient, robust and the resulting code should be easy to use or to create. Some questions to address are: exact or approximate values of the derivative and do we wish to have a discrete or continuous approximation.
The above methods are readily supported in C++, C#, Python and several (open-source) libraries.
In this note we give two examples of using The Complex Step Method in C++ and Python. The first example is generic to show how the method works and the second example shows how to compute option ‘greeks’ in derivative pricing.
The CSM is discussed in the links attached to my source code. I give two C++ programs and a Python proof-of-concept code to make the method more accessible. In this case we used and modified the C++ code to get a working version in Python up and running.
The rationale for the code is to show how CSM works. Extensions to more complex cases are possible and will be discussed elsewhere.