3

How to minimize the maximum absolute difference between 2 functions?: example $\min_a\{\|\text{erf}(x)-\tanh(\frac2{\sqrt{\pi}}(x+a x^3))\|_\infty\}$

Intro_______________

In this other question I found by trial and error that the Error function could be approximated decently with the following Hyperbolic tangent function: $$z(x)=\tanh\left(\frac{2}{\sqrt{\pi}}\left(x+\frac{11}{123}x^3\right)\right)$$

it keeps the absolute difference $|\text{erf}(x)-z(x)|<0.000358$. 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\%$.

The formula $z(x)$ comes from the series expansion of $\tanh^{-1}(\text{erf}(x))$ shown in Wolfram-Alpha, and just the first two 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.

Desmos plot

The final formula is a modification by trial and error so the last fraction $11/123 \approx 0.0894308943$ surely could be improved. Numerically I have tried it to search it manually and I think the value is near $a \approx 0.089429822$ (caution here, this could be awfully wrong).

But by doing this, I figure out I have no clue how to solve it theoretically: trying some derivatives to match some first order conditions don't worked, and I don't know if there exists a method of equations in order to solve these kind of problems (like Euler-Lagrange Equations are used in calculus of Variations, as an example of a method of solving a minimization problem).

Main question___________

So if I have these two functions $f(x) = \text{erf}(x)$ and $g(x,a) = \tanh\left(\frac{2}{\sqrt{\pi}}\left(x+ax^3\right)\right)$ and I want to find some parameter "$a$" such it minimizes the maximum absolute difference between these two functions: $$\min_{a}\left\{ \|f(x)-g(x,a)\|_\infty\right\} \equiv \min_{a}\left\{ \left\|\text{erf}(x)-\tanh\left(\frac{2}{\sqrt{\pi}}\left(x+ax^3\right)\right)\right\|_\infty\right\}$$

  • How do I solve theoretically these kind of problems? There is a classic framework to use? I Googled for it but I found little about it (maybe I used the wrong terms), Does this problem have a specific name?
  • How do I solve theoretically this specific example? Please be detailed if possible.
  • Which is the value of the optimal $\hat{a}$ in this specific example? Hopefully theoretically obtained, like having a closed-form through known constants if possible.

Added later

After the comment by @Claude looks like the problem is not trivial at all, and there are only numerical approximations to solve it (which is sad). So I am willing to give the answer to the best numerical result of the approximate of the optimal $\hat{a}$.

So far, my best attempt is $\hat{a}=\frac{302\pi}{10609}$. Please take into account I am looking for simple formulas, everything more complicated than typing the example equation will be dismissed for the formula for this optimal constant $\hat{a}$ (I know beforehand this is very subjective, so I hope you get for what I am aimmimg for).


2nd Added later

As a reference, by playing with Desmos I have some insight about the optimal value should be: $$\frac{302\pi}{10609}<\hat{a}<\frac{11}{123} $$

As example, $a=\frac{86\pi}{3021}$ gives an error of $0.000356924$.


Best attempt so far:

$\hat{a}\approx \frac{314}{3511}$ which gives a figure of $0.000356851$

(Caution here: Desmos show different results from those shown in Wolfram-Alpha. I am using Wolfram-Alpha as benchmark, but I don't have any good reason supporting one from another).


Out of question

I found also by trial and error than: $$\tanh\left(\frac{2}{\sqrt{\pi}}\left(\frac{841}{840}x+\frac{16}{181}x^3\right)\right)\to \text{max error}=0.000292772$$

and also that $$\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$$

which have figures similar or better than many of the more complex numerical approximations shown in Wikipedia page for the Error function: Numerical approximations.

I think is interesting since the coefficients are debiations from the traditional series expansions like Taylor series, but also since the similarity between the $\text{erf}(x)$ and $\tanh(x)$ functions, I have doubts about their absence of the Wikipedia approximations (the simple "$11/123$" version was uploaded by me a few days ago): Does anybody knows why is that so?, Are there any reasons that discard the hyperbolic tangents for approximating the Error function?


Final update

After the comment by @njuffa I have started realizing that due numerical issues on accuracy I don't have an objective way for comparing solutions near $11/123$ since Desmos and Wolfram-Alpha gives different results, as also the optimization figures used by everyone that have shared their interesting answers or attempts as comments. Maybe it is as so because on how each software makes their numerical implementations of the Error and Hyperbolic tangent functions, on how they numerically integrate, numerical optimization methods considered, floating point issues, or all of above - but I don't know for sure.

I got tired of making failed attempts for solving it in closed-form, so I have choosen @JaiB answer as correct just because I learned there about the existence of the "Rationalize" function in Wolfram-Alpha.

Since I don't have a objectively way to do it, I want to thank also @ClaudeLeivovici for his detailed answer, as also everyone else who participated in the comments (@Claude for explaining it was a hard theoretical problem, @RiverLi for correcting my typo at the beginning, and @njuffa for also sharing an answer in the comments).

Joako
  • 2,237
  • 1
    Do you mean $\tanh\left(\frac{2}{\sqrt{\pi}}\left(x+ax^3\right)\right)$ rather than $\tanh\left(\frac2\pi\left(x+ax^3\right)\right)$? – River Li Oct 29 '23 at 06:04
  • 1
    @RiverLi yes it was a typo. thanks – Joako Oct 29 '23 at 09:36
  • 1
    there are examples of minimax approximations, often using Chebyshev polynomials, not sure if there is a general algorithm for your case – Claude Oct 29 '23 at 16:56
  • @Claude Thanks for commenting. I don't going to lie, its dissapointing to find out it is not a problem with a classic way of solving. I don't think I gonna have an answer now. Thanks anyway for your time. – Joako Oct 29 '23 at 19:28
  • Is a maximum deviation of 0.000356895 adequate for your objective? That's a subject matter issue rather than a math problem. – JimB Oct 31 '23 at 19:52
  • @JimB indeed it is, but since I started asking for the theoretical optimum I think is valid to stretch what could be done, under that line of thought I just could kept on $11/123$, but with your answer I found a better figure of $336/3757$. Even more, through the "Rationalize" function I found that $$\tanh\left(\frac{2}{\sqrt{\pi}}\left(\frac{841}{840}x+\frac{16}{181}x^3\right)\right)$$ has a figure of $0.000292772$, which is lower than some of the more complicated approximation formulas shown in Wikipedia page for the Error Function. – Joako Nov 01 '23 at 05:00
  • If there is no closed-form solution, then at some point you'll need to say what is an adequate maximum deviation and it would be even nicer if you could justify that those largest deviations near +/-1 and +/-2 aren't bothersome (assuming that's the case). It goes from Math to Applied Statistics when you start including your overall objective. Even Excel can do complicated arithmetic so why not use 7 to 10 terms in a Taylor series? I'm not suggesting that you do that but only that when to stop is not a math issue as that information is not an intrinsic characteristic of your question. – JimB Nov 02 '23 at 16:22

2 Answers2

3

Minimizing the maximum absolute value of an error is feasible but it is closer to a nightmare than to a piece of cake. I made it for your problem and faced a lot of problems because the function is far to be continuous.

Probably the simplest is to run by cases and use polynomial interpolation around the minimum.

For example $$\left( \begin{array}{cc} a & \text{maximum absolute error} \\ 0.0893 & 0.000396148 \\ 0.0894 & 0.000366592 \\ 0.0895 & 0.000366851 \\ 0.0896 & 0.000380030 \\ 0.0897 & 0.000393616 \\ \end{array} \right)$$

gives $a=0.0894454$.

Zooming around $$\left( \begin{array}{cc} a & \text{maximum absolute error} \\ 0.089432 & 0.000357205 \\ 0.089433 & 0.000356912 \\ 0.089434 & 0.000358373 \\ \end{array} \right)$$

gives $a=0.0894327$.

Repeating leads to $$\large\color{blue}{a_{\text{opt}}=0.08942824312}$$

  • Thanks for taking the time to answer. In Desmos I can see that your estimate performs worst than $11/123$... Do you think it is a Desmos mistake?. By the way, what I have tried is to match both spikes seen in the link since looks like there is a trade off on their heights. Maybe searching for "$y$" on $$\tanh(2/\sqrt{\pi}(x+y/1000,x^3))$$ and then taking then $a=y/1000$ could help with the accuracy (it have help me when using Wolfram-Alpha). – Joako Oct 30 '23 at 12:27
  • 1
    @Joako $0.08942824312-\frac {11}{123}=-2.65119\times 10^{-6}$. As I wrote, the problem is very difficult because the function is not continuous.. I shall try again. Cheers :-) – Claude Leibovici Oct 30 '23 at 12:32
  • Wolfram-Alpha also says your estimate is performing worst than $11/123$ as seen here. – Joako Oct 30 '23 at 12:34
  • 1
    @Joako. Working with very high precision, my new best value is now $a=0.0894343861$ for which the maximum error is $0.00035651$ – Claude Leibovici Oct 30 '23 at 13:00
  • For $a=11/122$ the error is already lower $0.000357529$, and with the already mentioned guess $a=0.089429822$ the error is $0.000357529$. But checking with your value $\hat{a}=0.0894343861$ it gives an error of $0.000358423$ and not mentioned $0.00035651$. I think that maybe there is a typo in your last comment. – Joako Oct 30 '23 at 14:33
  • @Joako. As I told, I restarted everything from scratch. The maximum error was computed for $50$ exact figures and the optimization for $20$. – Claude Leibovici Oct 30 '23 at 14:52
  • I have uploaded the question with the interval where the optimal value looks like it lives, everything outside will give worst figures. I recomend you to test their errors in Wolfram-Alpha website as on the previous links, they have already made the math for testing the figures. – Joako Oct 30 '23 at 14:58
  • 1
    @Joako From what I can see, Wolfram Alpha does not appear to compute the maximum error entirely accurately. FWIW, my own optimizer converged to $a = 384098163 / 2^{32} \approx 0.0894298225$ for which the error is $0.00035787237$. – njuffa Nov 04 '23 at 07:43
  • @njuffa nice!... I tested in Desmos and at least there something near your number like $676/7559$ behave better than my best attempt through Wolfram-Alpha $314/3511$. Maybe Desmos is a better Benchmark. – Joako Nov 04 '23 at 14:25
3

First we look at a plot of the deviations for values of $x$ given that $a=11/123$:

Plot[Abs[Erf[x] - Tanh[2 x (1 + (11/123) x^2)/Sqrt[π]]], {x, -3, 3},Frame -> True, 
 FrameLabel -> (Style[#, 18, Bold] &) /@ {"x", "Absolute error"},
 PlotRange -> {All, {0, 0.0004}}, 
 PlotLabel -> Style["a = 11/123", 24, Bold]]

Deviations for values of x given that a=11/123

So the absolute error is symmetric around 0 and there are just the two peaks around x = 0.8 and x = 1.8 that we need to deal with. A function can be written (I' ve used Mathematica but I' m sure that Maple and MATLAB work fine, too) that allows you to control the precision of the numerical values.

For a given value of $a$ near 11/123 the curves have similar shapes and the two peaks around the same values. A function with a precision value of "50" (i.e., roughly 50 significant digits) one can use the following:

f[x_, a_] := Abs[Erf[x] - Tanh[2 x (1 + a x^2)/Sqrt[π]]]
maxDeviation[a_?NumericQ] := 
 Max[FindMaximum[f[x, a], {x, 9/10}, WorkingPrecision -> 50][[1]],
  FindMaximum[f[x, a], {x, 18/10}, WorkingPrecision -> 50][[1]]]
sol = FindMinimum[maxDeviation[a], {a, 894/10000}, 
  WorkingPrecision -> 50]
(* {0.00035784262189043944704023263051447980100361414499527, 
   {a -> 0.089429822455264989912596353788697817245378987726119}} *)

If you desire a "rational" number for $a$ to make inputting that constant easier, then the Rationalize function can do that for you. If you want the rational number to be accurate to 10^-8, then the following will work

Rationalize[a /. sol[[2]], 10^-8]
(* 676/7559 *)
JimB
  • 2,385
  • My answer differs slightly from @ClaudeLeibovici so that tells me I need to check things again. – JimB Oct 30 '23 at 20:20
  • Thanks for taking the time of a detailed answer. As you can see in the question I am not fully aware of numerical methods to solve this problem, so I don't know precisely what ClaudeLeibovici is doing, and neither I understand what are the numerical precisions you have use neither which ones Wolfram-Alpha uses. By observation by playing in Desmos and changing decimal values, I think there is a trade off between the first and the second lobe, so the minimum is going to be attained in principle, when both values matches each others. – Joako Oct 30 '23 at 22:28
  • With this said, I tested your result in Wolfram-Alpha and it do shows your result in the error (same it shows for $11/223$ and $302\pi/10609$ I think), (...) – Joako Oct 30 '23 at 22:34
  • I don't know anything about Desmos. But I would ask their technical support folks if the maximum precision available is "double precision" or does Desmos offer "multiple precision" numbers. – JimB Oct 30 '23 at 22:36
  • (...) but also I told in the last update if you use values in between you do have better results, like $86\pi/3021$ as you could see in Wolfram-Alpha, so for obvious reasons I expect values with better performances (I don't want to sound mean here, but I believe they are sensefull expectations). – Joako Oct 30 '23 at 22:36
  • due the higly sharp figures you show maybe there is an accuracy issue due the numbers being so small (it have happened to me before due float representation). Maybe try to use $$\tanh(2/\sqrt{\pi}(x+y/1000000,x^3))$$ searching for "$y$" and later set the result as $a=y/1000000$ (as an idea, maybe it don't help neither) – Joako Oct 30 '23 at 22:40
  • 1
    Mathematica considers $86\pi/3021$ an exact number and 0.089433 a "floating point" number. The latter in calculations can quickly lose numerical precision. – JimB Oct 30 '23 at 22:40
  • Also I worked with $10000\ |\text{erf}(x)-\tanh(%)|$ in order to have numbers Desmos could manage its differences. – Joako Oct 30 '23 at 22:46
  • 1
    I've simplified the code and put in how to obtain an estimate of $a$ that is a rational number with any desired closeness to the actual value of $a$. – JimB Oct 30 '23 at 23:02
  • If you use "Rationalize" function with $86\pi/3021$ it gives you the same rational number $676/7559$? – Joako Oct 30 '23 at 23:13
  • I didn't know the rationalize command, it worked in Wolfram-Alpha for my last question and it gives $a\approx 336/3757$ which result in a figure of $0.000356895$, even better than my previous results! – Joako Oct 30 '23 at 23:20
  • If I know a close interval where the optimal value lives, just programming a "for" loop changing decimal positions should increase the accuracy right? – Joako Oct 30 '23 at 23:22