![]() |
Stan Math Library
5.4.0
Automatic Differentiation
|
|
inline |
Return the scaled complementary error function of the argument.
\(\mbox{erfcx}(x) = \exp(x^2)\,\mbox{erfc}(x)\)
The scaling removes the Gaussian factor, so erfcx stays in a narrow range (it decays like \(1/(x\sqrt{\pi})\) for large positive x) where the two factors individually overflow and underflow: erfc(x) underflows to zero above x = 27 while exp(x*x) overflows above x = 26.6, so their product cannot be formed directly in the upper tail at all. It is the natural building block for normal tail quantities, e.g. the standard normal log CDF is LOG_HALF + log(erfcx(-x * INV_SQRT_TWO)) - x * x / 2 and the inverse Mills ratio is SQRT_TWO_OVER_SQRT_PI / erfcx(-x * INV_SQRT_TWO), both without cancellation.
Four branches. The whole positive axis is covered without calling any library function: a direct approximation of erfcx needs no erfc.
x >= 4: Cody (1969) third-interval rational.0.46875 <= x < 4: Cody (1969) second-interval rational. Cody writes this interval as erfc(x) = exp(-x*x) * R(x), so erfcx is R(x) and the exponential is cancelled analytically rather than computed and divided out.|x| < 0.46875: a degree-18 Chebyshev-economized expansion about zero, covering both signs with no branch and no library call.x <= -0.46875: the reflection 2*exp(x*x) - erfcx(-x), which is the one place a library call is unavoidable, since erfcx grows like 2*exp(x*x) as x -> -infinity. The rounding error of x * x is recovered with fma and folded back in, because exp amplifies it into roughly x * x * eps. Below x = -6.1, erfcx(-x) is under eps/2 of 2*exp(x*x), so the subtraction is skipped.W. J. Cody, Math. Comp. 23(107):631-638 (1969). https://doi.org/10.1090/S0025-5718-1969-0247736-4
The crossover at 4 is the same one R's pnorm uses: it switches to the Cody tail form at |y| > M_SQRT_32, and since the argument here is the erfc argument y / sqrt(2), that is sqrt(32) / sqrt(2) = 4 exactly.
The derivative is \(\frac{d}{dx}\mbox{erfcx}(x) = 2x\,\mbox{erfcx}(x) - \frac{2}{\sqrt{\pi}}\).
\[ \mbox{erfcx}(x) = \begin{cases} \exp(x^2)\operatorname{erfc}(x) & \mbox{if } -\infty\leq x \leq \infty \\[6pt] \textrm{NaN} & \mbox{if } x = \textrm{NaN} \end{cases} \]
| T | An arithmetic type. |
| xx | argument |