Search code examples
rtrigonometrygaussiannumerical-methods

Does atan() provide any computational advantage over pnorm() in R?


This article describes an analytical approximation of normal CDF:

enter image description here

The approximation uses the arctangent function, which is also numerically approximated. I found some discussions about the algorithm of arctan functions in general, and it seems pretty convoluted. In comparison, the source code of pnorm() in R seems pretty straight forward, though it may not be as efficient.

Is there any computational advantage of using atan() instead of pnorm() in R, especially with large data and high parameter space when there is already a bunch of other numerical calculations based off the normal PDF already?

Thanks!


Solution

  • Tried to look at it out of curiosity

    First define the function

    PNORM <- function(x) { 1/(exp(-358/23*x + 111*atan(37*x/294)) + 1) }
    

    Then let us look at differences over the range of [-4, 4]

    x <- seq(-4, 4, .01)
    plot(x, pnorm(x)-PNORM(x), type="l", lwd=3, ylab="Difference")
    

    which results in this graph

    enter image description here

    So the difference is small but maybe not small enough to ignore in some applications. YMMV. If we look at computing time then they are roughly equal with the approximation appearing to be slightly faster

    > microbenchmark::microbenchmark(pnorm(x), PNORM(x))
    Unit: microseconds
         expr    min      lq     mean  median      uq    max neval cld
     pnorm(x) 34.703 34.8785 36.54254 35.1820 38.3150 47.786   100   b
     PNORM(x) 24.293 24.4625 27.07660 24.8875 28.9035 59.216   100  a