Error Function

This page was generated with the assistance of AI. Please report any inaccuracies.

The Erf class implements the error function erf and the complementary error function erfc, together with their inverses. As in Chapter 6 of Numerical Recipes, 3rd Edition (section 6.2.2 — see Reference), the class is presented alongside the incomplete gamma function because both erf and erfc are special cases of it.

Definitions

Both functions are odd around their midpoint and satisfy simple symmetries:

Plot of erf(x) and erfc(x) over x from -3 to 3
Figure 1. The error function erf(x) and the complementary error function erfc(x) = 1 - erf(x).

They relate to the regularized incomplete gamma function documented in Gamma Function by

NormalDist uses exactly this connection: the normal p.d.f. is a rescaled Gaussian kernel , so its cumulative distribution function reduces to erfc — see Normal Distribution Algorithm.

Chebyshev approximation

Rather than evaluating erf/erfc through the more expensive general incomplete-gamma machinery, Erf uses a dedicated Chebyshev approximation that is faster and specific to this special function. For , with

the complementary error function is approximated as

where is a degree-28 Chebyshev polynomial whose coefficients are precomputed and stored in the COF array. The private method erfccheb evaluates this polynomial using Clenshaw’s recurrence (the loop that accumulates d/dd over the coefficients) and combines it with the exponential prefactor. erf and erfc then dispatch to erfccheb depending on the sign of their argument, using the symmetries above to cover negative values:

flowchart TD ErfCall["erf(x)"] ErfcCall["erfc(x)"] ErfSign{"x >= 0?"} ErfcSign{"x >= 0?"} Cheb["erfccheb(|x|)\n(Chebyshev series)"] ErfCall --> ErfSign ErfSign -- yes --> ErfPos["1 - erfccheb(x)"] ErfSign -- no --> ErfNeg["erfccheb(-x) - 1"] ErfPos --> Cheb ErfNeg --> Cheb ErfcCall --> ErfcSign ErfcSign -- yes --> ErfcPos["erfccheb(x)"] ErfcSign -- no --> ErfcNeg["2 - erfccheb(-x)"] ErfcPos --> Cheb ErfcNeg --> Cheb
import com.irurueta.statistics.Erf;

double errorFunction = Erf.erf(1.0);
double complementaryErrorFunction = Erf.erfc(1.0);

Inverse functions

Erf.inverfc(p) returns the value such that for , and Erf.inverf(p) reuses it as . As with the inverse incomplete gamma function, the implementation (following a suggestion by P.J. Acklam cited in the book) starts from a rational approximation of the inverse normal cumulative distribution function and refines it with two iterations of Halley’s method, using

as the derivative needed by the Halley correction.

import com.irurueta.statistics.Erf;

double xSuchThatErfcIsOneHalf = Erf.inverfc(0.5);
double xSuchThatErfIsOneHalf = Erf.inverf(0.5);

NormalDist.invcdf calls Erf.inverfc to compute quantiles of the normal distribution — see Normal Distribution Algorithm.