I'm trying to find the fastest form for a function. I have a few approaches with test results.The function I'm working with is the 3rd derivative of the incomplete beta function with respect to its first argument. The second two arguments will always be greater than 1. All arguments are reals.

I start with what is to me the most straightforward option

fun1[x_, alpha_, beta_] = Derivative[3][(Beta[#, alpha, beta] &)][x];

Then I see if FullSimplify will generate a faster form.

fun2[x_, alpha_, beta_] = 
   Derivative[3][(Beta[#, alpha, beta] &)][x]

Next, I try using a pure function.

fun3 = Function[{x, alpha, beta},
   Derivative[3][(Beta[#, alpha, beta] &)][x]

And then I try using FullSimplify and a pure function

fun4 = Function[{x, alpha, beta},
     Derivative[3][(Beta[#, alpha, beta] &)][x]

My tests show fun2, the FullSimplify (but not pure function) approach as the fastest.

testData = {RandomReal[{0, 1}, 50], RandomInteger[{1, 30}, 5], 
  RandomInteger[{1, 30}, 5]}
Part[RepeatedTiming[Outer[#, Sequence @@ testData]] & /@ {fun1, fun2, 
   fun3, fun4}, All, 1]
(*{0.0172, 0.00875, 0.0174, 0.010}*)

Without restricting results to machine precision (e.g., Compile), are any other methods faster?


As I noted in the comments, one can use Derivative[] to take the partial derivative of a multi-argument function; thus, for the incomplete beta function, you can do this:

fun1[x_, alpha_, beta_] = Derivative[3, 0, 0][Beta][x, alpha, beta]

Alternatively, you can use one of a number of explicit formulae listed in the Wolfram Special Functions website; in particular, using the formula here, we have

With[{n = 3},
     (-1)^(n - 1) (1 - z)^(b - n) z^(a - 1) Gamma[b]
     Hypergeometric2F1Regularized[1 - a, 1 - n, 1 + b - n, 1 - 1/z] // FullSimplify]
   (1 - z)^(-3 + b) z^(-3 + a) (2 - 3 a + a^2 - 2 (-1 + a) (-3 + a + b) z +
   (-3 + a + b) (-2 + a + b) z^2)
| improve this answer | |
  • $\begingroup$ That form of derivative is much cleaner. Thanks for the tip. $\endgroup$ – FalafelPita Aug 17 '17 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.