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
-
1https://en.wikipedia.org/wiki/Error_function#Numerical_approximations – Andrei Jul 20 '21 at 19:43
-
1Have a look at my edit. – Claude Leibovici Jul 22 '21 at 02:46
-
1https://arxiv.org/ftp/arxiv/papers/2012/2012.04466.pdf is an interesting paper – Claude Leibovici Jul 23 '21 at 06:33
-
@ClaudeLeibovici that brings all new thoughts to me of how methods may change depending on whether you are approximating a real or complex function, and how $\operatorname{erf}(z)$ looks – Henry Lee Jul 23 '21 at 19:19
2 Answers
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)$.
- 299,370
-
Ahhh I see, I hadn't heard of the Pade approximant before but it seems like an interesting method – Henry Lee Jul 21 '21 at 13:09
-
1@HenryLee. It is fascinating ! On the search bar, just type Pade : 675 entries – Claude Leibovici Jul 21 '21 at 14:37
-
Absolutely, I like how for any order it seems it is more accurate than the equivalent Taylor/Maclaurin series – Henry Lee Jul 21 '21 at 23:10
-
@HenryLee. Almost by definition, $P_1(x)$ is better that the series to $O(x^6)$. – Claude Leibovici Jul 22 '21 at 01:47
I had made a similar question here, and I found experimentally this simple approximation to the $\text{erf}(x)$ function:
$$z(x)=\tanh\left(\frac{2}{\sqrt{\pi}}\left(x+\frac{11}{123}x^3\right)\right)$$
it keeps the difference $|\text{erf}(x)-z(x)|<0.00036$.
Also I compared in Wolfram-Alpha using $z(x)$ for taking probabilities of the standard Gaussian distribution, and the max mistake looks its below $0.018\%$, quite accurate in my opinion.
The formula is a modification by trial and error so the last fraction $11/123$ surely could be improved. It comes from the series expansion of $\tanh^{-1}(\text{erf}(x))$ shown in Wolfram-Alpha, and just the first 2 terms makes already simple approximation than works quite good: $$f(x)=\tanh\left(\frac{2}{\sqrt{\pi}}\left(x+\frac{(4-\pi)x^3}{3\pi}\right)\right)$$
Here you could see them in Desmos.
Added later
In the following question I found another approximate much better than the previous, a little more complicated but still simpler than many of the approximations displayed on Wikipedia page for the Error function: Numerical approximations:
$$\tanh\left(\frac{2}{\sqrt{\pi}}\left(x+\frac{28}{305}x^3\sqrt{1-\frac{x^2}{33}}\right)\right)\to \text{max error}=0.0000551322$$
Again, be aware the constants were find by trial and error in Desmos, there nothing special about them a surely could be improved.
- 2,237
-
1Do not post the same answer to multiple questions. If one answer is appropriate for several questions, answer only one of those questions, and flag the rest of them as duplicates. – Xander Henderson Dec 01 '23 at 16:16
-
@XanderHenderson How I do that? Using the "flag" link for moderator's intervention? – Joako Dec 01 '23 at 18:38
-
1In the flagging menu, it should be possible to flag a question as a duplicate. This is a standard flag, and requires no moderator intervention. – Xander Henderson Dec 01 '23 at 18:52