1

Approximating for the Error function $\text{erf}(x)$ through an Hyperbolic tangent function $\text{tanh}\left(\dfrac{4x}{4-x^2}\right)$

I was plotting some functions and I found that the function $$f(x) = \begin{cases} -1,\quad x\leq -2\\ 1, \quad x\geq 2 \\ \text{tanh}\left(\dfrac{4x}{4-x^2}\right),\, -2<x<2\end{cases}$$ "looks" very similar to the graph of the Error function as it is shown in Wolfram-Alpha:

Wolfram Alpha plot

But looking into the wikipedia page for the Error function this approximation is not listed, so I guess that regardless from the similarity in the plot, $f(x)$ it is considered as a "bad approximation":

Why it is considered a poor approximation?

Also, a simpler version of the Hyperbolic tangent function could fit even better as approximation: $$g(x) = \text{tanh}\left(\dfrac{11}{9}x\right)$$

But no relation with the Hyperbolic tangent function is listed in Wikipedia, so

Why hyperbolic tangents are considered bad approximations for the error function?

Here I left the plots in Desmos:

Desmos' graph


Added later (after some answers)

After 2 interesting answers, I got the idea of testing the series expansion of $\tanh^{-1}(\text{erf}(x))$ shown in Wolfram-Alpha, and just the first 2 terms makes a 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 it in Desmos where the maximum aplitude difference is lower than $0.0007$. Also note that don't requires to be defined as a piecewise function.

Does this approx. good enough for approximating probabilities?

best attempt so far

Even since after these 2 first terms the Taylor expansion start to converge more slowly, by sacrificing accuracy near $x=0$ (since is symmetric), one could find approximations that reduce the maximum amplitude differences, and also, for making it having fewer terms I have choosen the following value (arbitrarily by trial and error):

$$g(x)=\tanh\left(\frac{2}{\sqrt{\pi}}\left(x+\frac{\pi}{35}x^3\right)\right)$$

which keeps the amplitude differences below $0.0005$.

I don't know How to measure if it will made too much mistakes if I use $g(x)$ instead of the Standard Gaussian CDF for taking probabilities, What do you think?

another good attempt


my last attempt

By trial and error I found that: $$z(x)=\tanh\left(\frac{2}{\sqrt{\pi}}\left(x+\frac{11}{123}x^3\right)\right)$$

keeps the difference $|\text{erf}(x)-z(x)|<0.00036$. Maybe someone could find an optimal $\hat{a}$ such it makes the best fit possible for $\text{erf}(x)$ through $\tanh\left(\frac{2}{\sqrt{\pi}}\left(x+\hat{a}x^3\right)\right)$

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!.

Joako
  • 2,237
  • @TravisWillse Thanks for commenting. So far I understand, as is defined $f(x)$ (defining within the constant-value domains the problematic points at the edges), the function is class $C^\infty$ since is a displaced version of a smooth transition function – Joako Oct 26 '23 at 03:06
  • Oops, you're right---I misread the function. (My first comment applies all the same, of course.) – Travis Willse Oct 26 '23 at 04:04
  • Its absence does not mean that it is a "bad approximation"---there are simply many approximations for many functions, and some have context-dependent advantages over others. – Travis Willse Oct 26 '23 at 04:05
  • 2
    Have a look at https://math.stackexchange.com/questions/4203484/simple-approximations-for-operatornameerfx – Claude Leibovici Oct 26 '23 at 05:21
  • If you have time to waste, play with the formula in my edit and put the curve on Desmos (refer to the plot in the body of your post). – Claude Leibovici Oct 26 '23 at 12:39
  • @ClaudeLeibovici I have tried it but now those constants' values are too complicated to be considered an "easy" approximation: I take their values up the first decimal leading to $\text{tanh}\left(\dfrac{200.7 x + 6.3 x^3}{177.9 - 10.6 x^2 + x^4}\right)$ and it improves a lot the difference as shown in Desmos, but now I don't know where to split the domain since those constant makes hard to find the value of the edges. Maybe it is possible to find simpler constants without decreasing accuracy too much. – Joako Oct 26 '23 at 13:58
  • You can extend the domain – Claude Leibovici Oct 26 '23 at 14:16
  • @ClaudeLeibovici Have you tried other hyperbolic tangents such the numerator is of higher order than the numerator? This trying to avoid the domain split required in my version and in your last attempts, like in your example $\tanh(2/\sqrt{\pi}, x)$ where no domain split was required. – Joako Oct 26 '23 at 15:09
  • @njuffa I guess is because for the general public is more intuitive using things with close form than abstractions. I don't know what else could be (it is for me at least) – Joako Oct 27 '23 at 01:38
  • @Joako A nice closed-form approximation is $$\mathrm{erf}(x) \approx \mathrm{sgn}(x)\left(1-e^{-x^2}\left(\frac{a}{\left(a-1\right)\sqrt{\pi x^2}+\sqrt {\pi x^2 +a^2}}\right)\right)$$ where $a=\sqrt{7.7}$. See C. Ren and A. R. MacKenzie, "Closed-form approximations to the Error and Complementary Error Functions and their applications in atmospheric science" (online) – njuffa Oct 27 '23 at 02:13
  • 1
    Have a look at my second answer – Claude Leibovici Oct 27 '23 at 02:32
  • 1
    I deleted bye mistake Remember that I am blind – Claude Leibovici Oct 27 '23 at 14:39
  • 1
    Have a look at my edit for the infinite norm – Claude Leibovici Oct 28 '23 at 06:19
  • @ClaudeLeibovici I found an edit with the Quadratic nom (the result you have uploaded previously in the comment now erased)... Did I miss the $|\cdot|_\infty$ norm result? – Joako Oct 28 '23 at 12:34
  • @ClaudeLeibovici Finally I have asked here about the supremum norm scenario, in case you get interested. – Joako Oct 29 '23 at 09:54

3 Answers3

2

Your idea is good but I think that we can do a bit better using it.

Starting from scratch, if you want to write $$\text{erf}(x)\sim\tanh \left(\frac{a x}{b+c x+d x^2}\right)$$ use a series expansion around $x=0$.

For sure, $c=0$ is a requirement. $$\text{erf}(x)-\tanh \left(\frac{a x}{b+d x^2}\right)=$$ $$x \left(\frac{2}{\sqrt{\pi }}-\frac{a}{b}\right)+x^3 \left(\frac{a^3+3 a b d}{3 b^3}-\frac{2}{3 \sqrt{\pi }}\right)+O\left(x^5\right)$$ which gives $$b=\frac{\sqrt{\pi } }{2}a \qquad \text{and} \qquad d=-\frac{(4-\pi ) }{6 \sqrt{\pi }}a$$ So, from Taylor, $$\text{erf}(x)\sim \tanh\left(\frac {6 \sqrt{\pi } x } {3 \pi -(4-\pi ) x^2 } \right)$$ $$\int_{-\pi}^{+\pi} \Big( \text{erf}(x)-\tanh\left(\frac {6 \sqrt{\pi } x } {3 \pi -(4-\pi ) x^2 } \right) \Big)^2\,dx=5.52\times 10^{-5}$$

The error of your formula is $$\left(\frac{2}{\sqrt{\pi }}-1\right) x$$ to be compared to $$\frac{\left(128-40 \pi -\pi ^2\right) }{45 \pi^{5/2}}x^5$$

The one I wrote gives a maximum absolute error of $0.0060$

Doing the same with the simplest formula, $$\text{erf}(x)\sim\tanh \left(\frac{2 x}{\sqrt{\pi }}\right) $$ which is very close to the $\frac {11}9$ that you proposed.

The advantage of all of that is that we can approximate* the inverse of the error function in a very simple manner..

Edit (just for the fun)

In the same spirit, the next level would be $$\tanh\left(\frac{20 \sqrt{\pi } x(a+ b x^2)}{c+d x^2+f x^4 } \right)$$ where

$$a=-2688 \pi +840 \pi ^2+21 \pi ^3$$ $$ b=5632-2688 \pi +252 \pi ^2+11 \pi ^3$$ $$c=-26880 \pi ^2+8400 \pi ^3+210 \pi ^4$$ $$ d=92160 \pi -47040 \pi ^2+5040 \pi ^3+180 \pi ^4$$ $$f=-36864+30720 \pi -9184 \pi ^2+880 \pi ^3+39 \pi ^4$$

This gives for the same norm $3.37\times 10^{-7}$ ($164$ times smaller than the previous) and a maximum error of $0.0004$ (around $x=2$) and the error is $\sim \frac {x^9}{10000}$.

  • Thanks for the detailed answer. Yor function $$f(x) = \begin{cases} -1,\quad x\leq -\pi\ 1, \quad x\geq \pi \ \text{tanh}\left(\dfrac{6\sqrt{\pi}x}{3\pi-(4-\pi)x^2}\right),, -\pi<x<\pi \end{cases}$$ is really closed to the error function as could be seen in Desmos – Joako Oct 26 '23 at 11:33
  • 1
    @Joako. Even if not perfect, I liked your idea and we can even do better. I shall try the next level. Cheers :-) – Claude Leibovici Oct 26 '23 at 11:38
  • Are you using something like the series expansion of $\tanh^{-1}(\text{erf}(x))$? Wolfram-Alpha gives something similar to your results, but with worst accuracy. – Joako Oct 26 '23 at 15:56
  • Truns out that $$g(x)=\tanh\left(\frac{2}{\sqrt{\pi}}\left(x+\frac{(4-\pi)x^3}{3\pi}\right)\right)$$ makes a wondefull job without requiring bounding the domain (check it in desmos) – Joako Oct 26 '23 at 16:18
  • I have added another attempt $g(x)$ which is simpler, and gives me an idea through your method: which would be the better constant value $\hat{a}$ for fitting the Error function with $q(x)=\tanh\left(\frac{2}{\sqrt{\pi}}\left(x+\hat{a}x^3\right)\right)$?... maybe checking $\hat{a}_1$ such as minimized the Norm, and also $\hat{a}_2$ minimizing the maximum amplitud difference. – Joako Oct 26 '23 at 18:23
  • Experimentally I think $\hat{a}\sim 0.08964$ but I was not able to found so far how to calculate it properly. – Joako Oct 26 '23 at 23:34
2

The approximation is bad because the tails are totally different.

The standard normal density is proportional to $e^{-x^2}$. The hyperbolic tangent, whose derivative is the square of the hyperbolic secant, is proportional to $(e^x + e^{-x})^{-2}$. For "large" $x$, this means the latter will be about $e^{-2x}$, but the former will be $e^{-x^2} \ll e^{-2x}$. As a result, the tails are much heavier for a density based on the hyperbolic tangent.

Let's denote $F(x) = \operatorname{erf}(x)$ and $G(x) = \tanh \frac{2x}{\sqrt{\pi}}$. Then $$f(x) = F'(x) = \frac{2}{\sqrt{\pi}}e^{-x^2}, \quad g(x) = G'(x) = \frac{2}{\sqrt{\pi}} \operatorname{sech}^2 \frac{2x}{\sqrt{\pi}}.$$

Here is a plot of $f/g$: enter image description here The error is between $1$ and $1.040703873959\ldots$. Note that it is always an overestimate; i.e., $F > G$ for all $x > 0$. You might look at this and think, "a maximum of 4% error is not that bad."

But here is a plot of $f/g$: enter image description here And this is much, much worse. As mentioned earlier, the tails are not comparable, which is why $\lim_{x \to \infty} \frac{f}{g} = 0$. In order to be a "good" approximation, the ratio of densities should be close to $1$ across the support. To see how much better we can do, consider the Bürmann series $$B(x) = \frac{2}{\sqrt{\pi}} \operatorname{sgn}(x) \sqrt{1-e^{-x^2}} \left(1 - \frac{1}{12}(1 - e^{-x^2}) - \frac{7}{480}(1 - e^{-x^2})^2 - \frac{5}{896} (1 - e^{-x^2})^3 - \cdots \right)$$ for which the first four terms yields the following plot of $F/B$: enter image description here And you can easily tell this is far superior. The plot of the ratio of derivatives $f/b$ is enter image description here It's not perfect by any means but it is clearly superior to the hyperbolic tangent. With more terms, it will improve further.

heropup
  • 146,967
  • For sure, I fully agree with you but the idea was not bad. If I may ask a favor : since I cannot produce decent plots, would you mind to add somewhere the plot of $\text{erf}(x), \coth\left(\frac {6 \sqrt{\pi } x } {3 \pi -(4-\pi ) x^2 }\right)$ for $-\pi \leq x \leq \pi$. Thanks in advance. – Claude Leibovici Oct 26 '23 at 09:02
  • Thanks for the detailed answer. @ClaudeLeibovici have found a closest version $$f(x) = \begin{cases} -1,\quad x\leq -\pi\ 1, \quad x\geq \pi \ \text{tanh}\left(\dfrac{6\sqrt{\pi}x}{3\pi-(4-\pi)x^2}\right),, -\pi<x<\pi \end{cases}$$ which is really close to the error function as could be seen in Desmos, Does it compare with the accuracy of the Bürman series? – Joako Oct 26 '23 at 11:39
  • Tbh I don't fully get your plots since comparisson quotients goes from $0$ to $1$, but from the series expansion of $\tanh^{-1}(erf(x))$ shown in Wolfram-Alpha, just the first 2 terms makes a 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)$$. I hope you could compare them with your analysis and comment. – Joako Oct 26 '23 at 17:00
  • I have updated the question with a simple approx with the hyperbolic tangent that looks better then the first $3$ terms of the Bürman series shown in Wikipedia, at least compared in Desmos... Do you believe this new attempt $g(x)$ fulfills being a good approximation? – Joako Oct 26 '23 at 18:17
1

In my first answer, I tried to stay as close as possible to your initial attempf.

Restarting from scratch, what we have is $$\tanh ^{-1}(\text{erf}(x))=t\sum_{n=0}^\infty a_n\,t^{2n} \qquad \text{where}\qquad t=\frac{2 }{\sqrt{\pi }}x$$ where the first coefficients are $$\left( \begin{array}{cc} n & a_n \\ 0 & 1 \\ 1 & \frac{4-\pi }{12} \\ 2 & \frac{96-40 \pi +3 \pi ^2}{480} \\ 3 & \frac{5760-3360 \pi +532 \pi ^2-15 \pi ^3}{40320} \\ 4 & \frac{645120-483840 \pi +116928 \pi ^2-9328 \pi ^3+105 \pi ^4}{5806080} \\ \end{array} \right)$$

for which the errors $$R_n=| \text{erf}(x)-\tanh (S_n)|$$ are $$R_1\sim\frac{t^5}{8744}\qquad R_2\sim \frac{t^7}{3946}\qquad R_3\sim \frac{t^9}{45963}\qquad R_4\sim \frac{t^{11}}{1082270}$$ For the already arbitrary bounds, consider the norms $$\Phi_n=\int_{-\pi}^{+\pi} \Big( \text{erf}(x)-\tanh\left(S_n \right) \Big)^2\,dx$$ $$\Phi_1=6.3\times 10^{-7}\quad \Phi_2=4.4\times 10^{-7}\quad \Phi_3=1.7\times 10^{-7}\quad \Phi_4=1.5\times 10^{-9}$$

To improve the model, for sure, adding more terms is a solution. But, making the series as the $[2n+1,2n]$ Padé approximant $P_n$ is better. For example $$P_1=\tanh\left(t \,\frac{a_1+(a_1^2-a_2)\,t^2 } {a_1- a_2\,t^2 }\right)$$ leads to a maximum error of $0.00053$.

Edit

$$\Phi(\hat a)=\int_{-\infty}^{+\infty} \Bigg(\text{erf}(x)-\tanh\left(\frac{2}{\sqrt{\pi}}\left(x+\hat{a}x^3\right)\right)\Bigg)^2\, dx$$ is minimum for $\hat{a}=0.0896929$ and its value is $2.73 \times 10^{-7}$.

  • If I want to keep the approximation simple as $\tanh(2/\sqrt{\pi}(x+ax^3))$, then "$a$" looks to be near $11/123$ which is greater than $a_1$ and lower than $(4-\pi)/(3\pi)$ which rises from the Taylor expansion of $\sqrt{\pi}/2,\tanh^{-1}(\text{erf}(x))$... Do you think it is possible to find an optimal "$a$" such it minimizes the squared differences? (as you did for $\Phi_n$) – Joako Oct 27 '23 at 03:30
  • Don't know why you erase the last comment. After thinking for a while, I think a best metric is finding the optimal $\hat{a}$ for $$z(t)=\tanh\left(\frac{2}{\sqrt{\pi}}\left(x+\hat{a}x^3\right)\right)$$ such it minimizes the maximum absolute difference $\text{min}{\hat{a}}\left{\text{max}{\forall x}\left{|\text{erf}(x)-z(x)|\right}\right}$, by trial and error I think is near $11/123$... Did you tried it? – Joako Oct 27 '23 at 14:23