I want to know how I can compute the first and second derivatives of logarithm of modified Bessel function of the second kind.
For instance, I'm interested in finding the following derivatives with respect to x:
- ∂/∂x log(K_(x))
- ∂^2/∂x^2 log(K_(x))
The first derivative is equivalent to (∂/∂x(K_(x))) / K_(x). I could use both R functions 'BesselK' (can be found in Bessel and fOptions packages) and 'BesselDK' (fOptions package) to calculate this. Is there a better alternative?
Particularly, how to calculate the second derivative above in R? I couldn't find anything specific anywhere.
Breaking out Python's
sympylibrary (because R doesn't have native symbolic computation facilities):Translating the results into R code (for convenience I'm defining a
besselkthat swaps the order of the arguments to match Python)Check results:
I was lazy and cut-and-pasted the output from
sympy. These functions would be made more efficient by vectorizing the calls tobesselK(e.g.bvec <- besselK(x, nu = nu + (-2:2))) and plugging the values in to the formula (as it is,besselKis called more times than necessary, especially in the second-derivative calculation; vectorization might not matter much, but not callingbesselKmore times than necessary would make a big difference).PS
BesselK,BesselDKare in the now-removed-from-CRAN fAsianOptions package