Simple approximations for $\operatorname{erf}(x)$

error functionnumerical methodssoft-question

I am looking for some potentially unexpected approximations of $\operatorname{erf}(x)$, by which I mean not a really long series expansion, and something that resembles the correct shape for an arbitrary amount of the real domain. Here are some I have come up with myself:
$$
\begin{matrix}
\tanh(1.182\cdot x) & \text{the constant is a trade-off}\\
\tanh(1.152\cdot x)+\dfrac{e^{-|x-1.5|}\cos(x-1.5)}{20} & \text{smaller error but not symmetrical}\\
\tanh\left(\frac{x|x|}{4}+x\right) & \text{constant not optimised for this yet}
\end{matrix}
$$

I also read this article which listed a method for assuming solutions of the form:
$$\tanh(f(x))$$
Just like I initially thought. My question is really, does anyone have any surprisingly accurate short approximations for it? Thanks

Best Answer

You can easily build your own approximations using as a basis $$\text{erf}(x) \sim \sqrt{1-\exp\Big[ -\frac {4x^2} {\pi} P_{n}(x) \Big]}$$ when $P_n(x)$ is the ratio of two polynomials of degree $n$ in $x^2$ or just a polynomial in $x^2$.

These are quite easy to build writing $$P=-\frac{\pi \log \left(1-\text{erf}(x)^2\right)}{4 x^2}$$ and developing as as series around $x=0$ $$P=1-\frac{2 (\pi -3) x^2}{3 \pi }+\frac{2 \left(120-60 \pi +7 \pi ^2\right) x^4}{45 \pi ^2}-\frac{4 \left(-420+280 \pi -56 \pi ^2+3 \pi ^3\right) x^6}{105 \pi ^3}+\frac{2 \left(120960-100800 \pi +28560 \pi ^2-3040 \pi ^3+83 \pi ^4\right) x^8}{4725 \pi ^4}+O\left(x^{10}\right)$$ which can easily be transformed into a Padé approximant.

For example $$P_0(x)=1 \qquad \qquad P_1(x)=\frac {1+\frac{\left(10-\pi ^2\right) }{5 (\pi -3) \pi }x^2 } {1+\frac{\left(120-60 \pi +7 \pi ^2\right) }{15 (\pi -3) \pi }x^2 }$$

To give you an idea, computing the norm $$\Phi_n=\int_0^\infty \left(\text{erf}(x)-\sqrt{1-\exp\Big[ -\frac {4x^2} {\pi} P_{n}(x) \Big]}\right) ^2\,dx $$ we have $$\Phi_0=3.23\times 10^{-5}\qquad \Phi_1=3.04\times 10^{-8}\qquad\Phi_2=1.20\times 10^{-10}\qquad\Phi_3=3.97\times 10^{-12}$$

Edit

Using the ideas from the linked paper, we could do basically the same expanding $$\tanh ^{-1}(\text{erf}(x))=\frac{2 x}{\sqrt{\pi }}-\frac{2 (\pi -4) x^3}{3 \pi ^{3/2}}+\frac{\left(96-40 \pi +3 \pi ^2\right) x^5}{15 \pi ^{5/2}}+\frac{\left(5760-3360 \pi +532 \pi ^2-15 \pi ^3\right) x^7}{315 \pi ^{7/2}}+O\left(x^9\right)$$ and use, as another approximation, $$\text{erf}(x)\sim \tanh \left(\frac{2 x}{\sqrt{\pi }} Q_n(x)\right)$$ and, similar to $P_1(x)$, write $$Q_1(x)=\frac{1+\frac{128-40 \pi -\pi ^2}{30 (\pi -4) \pi } x^2} {1+\frac{96-40 \pi +3 \pi ^2}{10 (\pi -4) \pi } x^2}$$ which is much better than what is given in the paper.

$$\Psi_1=\int_0^\infty\left(\text{erf}(x)-\tanh \left(\frac{2 x}{\sqrt{\pi }} Q_n(x)\right)\right) ^2 \,dx =2.22\times 10^{-7} $$

Even if the two formalisms look similar in terms of accuracy, I would favor the first one because it makes the inversion of the error function easier ; with $P_1(x)$ you just need to solve a quadratic equation in $x^2$ while with $Q_1(x)$ you just need to solve a cubic equation in $x$.

Comparing with Taylor series, the difference between $\text{erf}(x)$ and its approximations is $1.56\times 10^{-4}x^7$ for $P_1(x)$ and $5.916\times 10^{-4}x^7$ for $Q_1(x)$.