Approximating the error function erf by analytical functions

34.7k Views Asked by At

The Error function

$\mathrm{erf}(x)=\frac{2}{\sqrt{\pi}}\int_0^x e^{-t^2}\,dt$

shows up in many contexts, but can't be represented using elementary functions.

I compared it with another function $f$ which also starts linearly, has $f(0)=0$ and converges against the constant value 1 fast, namely

$\tanh{(x)} = \frac {e^x - e^{-x}} {e^x + e^{-x}}$.

Astoningishly to me, I found that they never differ by more than $|\Delta f|=0.0812$ and converge against each other exponentially fast!

I consider $\tanh{(x)}$ to be the somewhat prettyier function, and so I wanted to find an approximation to $\text{erf}$ with "nice functions" by a short expression. I "naturally" tried

$f(x)=A\cdot\tanh(k\cdot x^a-d)$

Changing $A=1$ or $d=0$ on it's own makes the approximation go bad and the exponent $a$ is a bit difficult to deal with. However, I found that for $k=\sqrt{\pi}\log{(2)}$ the situation gets "better". I obtained that $k$ value by the requirement that "norm" given by

$\int_0^\infty\text{erf}(x)-f(x)dx,$

i.e. the difference of the functions areas, should valish. With this value, the maximal value difference even falls under $|\Delta f| = 0.03$. And however you choose the integration bounds for an interval, the area difference is no more than $0.017$.

enter image description here

Numerically speaking and relative to a unit scale, the functions $\text{erf}$ and $\tanh{(\sqrt{\pi}\log{(2)}x)}$ are essentially the same.


My question is if I can find, or if there are known, substitutions for this non-elementary function in terms of elementary ones. In the sense above, i.e. the approximation is compact/rememberable while the values are even better, from a numerical point of view.

The purpose being for example, that if I see somewhere that for a computation I have to integrate erf, that I can think to myself "oh, yeah that's maybe complicated, but withing the bounds of $10^{-3}$ usign e.g. $\tanh(k\cdot x)$ is an incredible accurate approximation."

6

There are 6 best solutions below

5
On

It depends on how much accuracy you need and over what interval. It seems that you are happy with a few percent. There is an approximation in Abromowitz & Stegun that gives $\text{erf}$ in terms of a rational polynomial times a Gaussian over $[0,\infty)$ out to $\sim 10^{-5}$ accuracy.

In case you care, in the next column, there is a series for erf of a complex number that is accurate to $10^{-16}$ relative error! I have used this in my work and got incredible accuracy with just one term in the sum.

0
On

I suspect the reason the $\tanh x$ solution "works" so well is because it happens to be the second order Pade approximation in $e^x$. unfortunately, higher order Pade Approximations don't seem to work as well. One more thing you could due is try to approximate $\text{erf}(x)$ only on $(-3,3)$, and assume it to be $\pm 1$ everywhere else.

0
On

Transforming a non-elementary function with elementary functions into a form that is well approximated with a rational polynomial seems to be a good approach.

Notice that $$ 1-\operatorname{erf}\left(x\right)^{2} \approx \frac{1}{e^{x^{2}}} $$ Now there are two approaches you can take to turn this into an approximately rational polynomial. Taking the log of both sides yields $$ \ln\left(1-\operatorname{erf}\left(x\right)^{2}\right) \approx -x^{2} $$ Substituting $x\rightarrow \sqrt{\ln\left(x\right)}$ yields $$ 1-\operatorname{erf}\left(\sqrt{\ln\left(x\right)}\right)^{2} \approx \frac{1}{x} $$ Both of these transformations of erf are invertible. Finding the Pade Approximant for either of these will yield an astoundingly good approximation for very few terms. In the case of the first one $$ \ln\left(1-\operatorname{erf}\left(x\right)^{2}\right) \approx x^{2}\frac{-1.27324-0.074647x^{2}}{1+0.0886745x^{2}} $$ and so for $x \ge 0$ $$ \operatorname{erf}\left(x\right)\approx \sqrt{1-e^{x^{2}\frac{-1.27324-0.074647x^{2}}{1+0.0886745x^{2}}}} $$ whose accuracy is about 4 decimal places at worst, and converges at 0 and infinity.

Unfortunately this is not invertible.

2
On

Nice! I found one very efficient one with a maximum difference of 0.01747. It contains a grand total of 1 division, 1 addition, 1 power, and 2 absolutes

$$ aerf(x)=\frac x {|x|+0.187^{|x|}} $$

For reference: $$ d(x)=aerf(x)-erf(x) $$

And you can reverse it (using more than elementary functions (courtesy of njuffa)): $$ \text{But that's hard.} $$

The 3 most off values of x for peaks (less on both sides) are:

  1. 0.5982 x, 0.01747 more
  2. 1.5582 x, 0.01741 less
  3. 0.11439 x, 0.00681 less

It is 0.01251 less at an x of 2, 0.000045 less at 5, and $5.22 (10^{-9})$ less at 10.

As x approaches infinity or 0, $d(x)$ approaches 0.

$$ \frac{0}{0+0.187^0}=\frac{0}{1}=0 $$ $$ \lim_{x\rightarrow inf}\frac{x}{|x|+0.187^{|x|}}=1 $$

re: njuffa says that a slightly more accurate equation uses $\frac{289}{1545}$ which is $5.501*10^{-5}$ more than $0.187$ and well, it is more accurate, but only by ~0.00004 at the greatest difference, so it's not really that much.

If you don't know, $erf'(x)$ is a scaled normal distribution ($e^{-\pi x^2}$). $$erf'(\sqrt{\pi}x)\frac{\sqrt{\pi}}2=e^{-\pi x^2}$$

If you want accuracy approximating the normal distribution, you would use $$anorm(x) = \frac{\delta}{\delta x}\frac{x}{|x|+0.368^{|x|}}$$

0
On

There is this series expansion using Gamma Regularized function $Q(a,z)$:

$$1-Q\left(n+\frac12,z^2\right)\mathop=_{n\in\Bbb N}^{z\ge0}\text{erf}(z)-ze^{-z^2}\frac{(-1)^n}{\Gamma\left(n+\frac12\right)}\sum_{k=0}^{n-1}\left(\frac12-n\right)_{n-k-1}(-1)^k z^{2k}\iff\text{Error of Approximation}=\text{erf(z)}-\text{Approximation}$$

You can invert to solve within the range of the approximation error:

$$\text{Error of Approximation}(z)=x\implies z=\sqrt{Q^{-1}\left(n+\frac12,1-x\right)}$$

An application of this Inverse Gamma Regularized $Q^{-1}(a,z)$ formula is to find the value of $z$ where the error of the approximation of $\text{erf}(z)$ is $0\le x\le1$. In other words, you can find which values of $z$ needed to be some amount off from the approximation of the Error function. Please correct me and give me feedback!

0
On

Late comer to this question but I know several solutions that may be of interest, including one that I am in the process of publishing. You seek an approximation to erf(x) that is "compact/rememberable" in the context of integrating a function presumably involving erf(x). I have been looking at just such a problem and the solution I came up with involves only exponentials of quadratic functions of the form $$ {\rm erf}_M(x)=1-\sum_{i=1}^{M} c_i {\rm e}^{-a_i x^2+2b_i x},~x\in R $$ where $a_i$, $b_i$ and $c_i$ for i=1...M are parameters that are used to fit the function erf(x). I call this an exponential quadratic approximator (EQA) for the error function. Note that the approximation can also be used for negative arguments since erf(x) is an odd function, which makes it applicable to the entire real line.

In the paper, I give an example for $M=4$ that yields a maximum absolute error of around $1.65 \times 10^{-4}$ on the entire real line. The 12 parameters are obtained by gradient descent optimisation. The values that give the cited error are given in Table 1:

enter image description here A preprint of the paper is available here: A 4-Term Exponential-Quadratic Approximation for Gaussian Q or Error Functions.

Comparison of approximations to the error function

The paper also reviews a number of other approximations from other researchers, although they are typically for the Gaussian Q-function $Q(x)=\frac{1}{2}(1-{\rm erf}(x/\sqrt{2}))$ and accurate for large $x$ but not near $x=0$, whereas the aforementioned EQA is exact at $x=0$. In the above figure, the 4-term EQA (dashed cyan) lies atop the error function (in black) on the graph. The other approximators, some of which are also based on exponentials of quadratic arguments, are detailed in the paper.

The approximation is particularly useful for analytical approximation (in terms of elementary functions) of integrals of the form: $$ \int_0^{\infty}{\rm erf}^n(ax + b)\,x^r\,{\rm exp}(-f^2 x^2 + 2gx)\,dx $$ where $r\geq 0$ and $n>0$ are both integers and $a>0$. Such integrals arise in the development of quasi-analytical models of least-squares generative adversarial networks (GANs) as described in Convergence and Optimality Analysis of Low-Dimensional GANs using Error Function Integrals.