Site menu:
R functions for complex-step differentiation
complex-step method is a clever way of obtaining a numerical approximation to the first derivative of a function, avoiding the round-off error that plagues standard first difference approximations [Ref 1]. An extension of the method allows second derivatives to be calculated with reduced round-off error. [Ref 2] gives an overview of the method with statistical examples.
Here are the R programs for the examples in that article. The examples are all from the book by Brazzale, Davison and Reid [Ref 3].
bdr23.R
bdr35exp.R
bdr35ln.R
bdr43.R
bdr46.R
bdr52.R
bdr52t4.R
bdr54.R
You will also need the files CStepDiff_functions.R and examples.R.
Put all the files in a single directory and run R with this directory as the working directory.
Typing
source("examples.R")
will generate the results in Table 3 of [Ref 2].
Since [Ref 2] was written, I have had to make two changes to the code:
- The original code used the function cgamma from package fOptions to evaluate the gamma function for complex argument. The fOptions package was subsequently removed from CRAN. The code now uses the function complex_gamma from the hypergeo package by Robin Hankin.
- There appears to have been a change at some point in the way that R evaluates x^z when x=0 and z is complex with Re(z)>0. Previously this returned 0, now it returns NaN+NaNi. This affects BDR examples 4.6 and 5.4. I have modified the coding of the log-likelihood function for these examples so that the original behaviour is restored.
References
[1] Squire, W. and Trapp, G. (1998). Using complex variables to estimate derivatives of real functions. SIAM Review, 40, 110-112. doi: 10.1137/S003614459631241X [pdf]
[2] Ridout M.S. (2009). Statistical applications of the complex-step method of numerical differentiation American Statistician, 63, 66-74. doi: 10.1198/tast.2009.0013
[3] Brazzale, A.R., Davison, A.C. and Reid, N. (2007). Applied Asymptotics: Case Studies in Small-Sample Statistics, Cambridge: Cambridge University Press. doi: 10.1017/CBO9780511611131