Post · 5 min read· pp. 638–640

What Is the Complex Step Approximation?

In many situations we need to evaluate the derivative of a function but we do not have an explicit formula for the derivative. The complex step approximation approximates the derivative (and the function value itself) from a single function evaluation. The catch is that it involves complex arithmetic.

For an analytic function ff we have the Taylor expansion

f(x+ih)=f(x)+ihf′(x)−h2f′′(x)2+O(h3),(∗)\notag \qquad\qquad\qquad\qquad f(x + \mathrm{i}h) = f(x) + \mathrm{i}h f'(x) - h^2\displaystyle\frac{f''(x)}{2} + O(h^3), \qquad\qquad\qquad\qquad(*)

where i=−1\mathrm{i} = \sqrt{-1} is the imaginary unit. Assume that ff maps the real line to the real line and that xx and hh are real. Then equating real and imaginary parts in (∗)(*) gives Ref(x+ih)=f(x)+O(h2)\mathrm{Re} f(x+\mathrm{i}h) = f(x) + O(h^2) and Imf(x+ih)=hf′(x)+O(h3)\mathrm{Im} f(x+\mathrm{i}h) = hf'(x) + O(h^3). This means that for small hh, the approximations

f(x)≈Ref(x+ih),f′(x)≈Imf(x+ih)h\notag f(x) \approx \mathrm{Re} f(x+\mathrm{i}h), \quad f'(x) \approx \mathrm{Im} \displaystyle\frac{f(x+\mathrm{i}h)}{h}

both have error O(h2)O(h^2). So a single evaluation of ff at a complex argument gives, for small hh, a good approximation to f′(x)f'(x), as well as a good approximation to f(x)f(x) if we need it.

The usual way to approximate derivatives is with finite differences, for example by the forward difference approximation

f′(x)≈f(x+h)−f(x)h.\notag f'(x) \approx \displaystyle\frac{f(x+h) - f(x)}{h}.

This approximation has error O(h)O(h) so it is less accurate than the complex step approximation for a given hh, but more importantly it is prone to numerical cancellation. For small hh, f(x+h)f(x+h) and f(x)f(x) agree to many significant digits and so in floating-point arithmetic the difference approximation suffers a loss of significant digits. Consequently, as hh decreases the error in the computed approximation eventually starts to increase. As numerical analysis textbooks explain, the optimal choice of hh that balances truncation error and rounding errors is approximately

hopt=2∣uf(x)f′′(x))∣1/2,\notag h_{\mathrm{opt}} = 2\Bigl|\displaystyle\frac{u f(x)}{f''(x))} \Bigr|^{1/2},

where uu is the unit roundoff. The optimal error is therefore of order u1/2u^{1/2}.

A simple example illustrate these ideas. For the function f(x)=exf(x) = \mathrm{e}^x with x=1x = 1, we plot in the figure below the relative error for the finite difference, in blue, and the relative error for the complex step approximation, in orange, for hh ranging from about 10−510^{-5} to 10−1110^{-11}. The dotted lines show uu and u1/2u^{1/2}. The computations are in double precision (u≈1.1×10−16u \approx 1.1\times 10^{-16}). The finite difference error decreases with hh until it reaches about hopt=2.1×10−8h_{\mathrm{opt}} = 2.1\times 10^{-8}; thereafter the error grows, giving the characteristic V-shaped error curve. The complex step error decreases steadily until it is of order uu for h≈u1/2h \approx u^{1/2}, and for each hh it is about the square of the finite difference error, as expected from the theory.

cstep_ex.jpg

Remarkably, one can take hh extremely small in the complex step approximation (e.g., h=10−100h = 10^{-100}) without any ill effects from roundoff.

The complex step approximation carries out a form of approximate automatic differentiation, with the variable hh functioning like a symbolic variable that propagates through the computations in the imaginary parts.

The complex step approximation applies to gradient vectors and it can be extended to matrix functions. If ff is analytic and maps real n×nn\times n matrices to real n×nn\times n matrices and AA and EE are real then (Al-Mohy and Higham, 2010)

Lf(A,E)≈Imf(A+ihE)h,\notag L_f(A,E) \approx \mathrm{Im} \displaystyle\frac{f(A+\mathrm{i}hE)}{h},

where Lf(A,E)L_f(A,E) is the Fréchet derivative of ff at AA in the direction EE. It is important to note that the method used to evaluate ff must not itself use complex arithmetic (as methods based on the Schur decomposition do); if it does, then the interaction of those complex terms with the much smaller ihE\mathrm{i}hE term can lead to damaging subtractive cancellation.

The complex step approximation has also been extended to higher derivatives by using “different imaginary units” in different components (Lantoine et al., 2012).

Here are some applications where the complex step approximation has been used.

  • Sensitivity analysis in engineering applications (Giles et al., 2003).
  • Approximating gradients in deep learning (Goodfellow et al., 2016).
  • Approximating the exponential of an operator in option pricing (Ackerer and Filipović, 2019).

Software has been developed for automatically carrying out the complex step method—for example, by Shampine (2007).

The complex step approximation has been rediscovered many times. The earliest published appearance that we are aware of is in a paper by Squire and Trapp (1998), who acknowledge earlier work of Lyness and Moler on the use of complex variables to approximate derivatives.

References

This is a minimal set of references, which contain further useful references within.

This article is part of the “What Is” series, available from https://nhigham.com/category/what-is and in PDF form from the GitHub repository https://github.com/higham/what-is.