We need to compute this function:
$f(x) = x + \sqrt {\frac 2 \pi} \cdot \sigma_x \cdot \frac {e^{- ( \frac {x} {\sqrt 2 \cdot \sigma_x} )^2}} {1 + erf(\frac {x} {\sqrt 2 \cdot \sigma_x})}$
We noticed that, for negative values of $\frac {x} {\sqrt 2 \cdot \sigma_x}$ lower than a certain value (and different depending on which software we use to compute it, say about $-5$ for instance), there is numerical instability (oscillations).
I saw this post suggesting how to achieve better stability, although on a rather different function:
Numerical Instability with this cumulate
Do you think a similar method would work here? And if so, should I express the $erf$ as an integral?
Or would you suggest some other method? Taylor expansion? Anything else...?
Thanks
EDIT after evaluation of the responses
Thank you all very much for your feedback!
I think there is a complication here that I had not explicitly foreseen.
First I will make the following substitution, so your alternative expressions of $erf$ can be used directly:
$x = y \cdot \sqrt 2 \cdot \sigma_x$
$f(y) = \sqrt 2 \cdot \sigma_x \cdot y + \sqrt {\frac 2 \pi} \cdot \sigma_x \cdot \frac {e^{- y^2}} {1 + erf(y)}$
Now, the theory we used to derive this equation requires that:
$f(y) > 0, \forall y$
$\lim_{y \to -\infty} f(y) = 0$
So, we expect $f(y)$ to be very slowly approaching $0$ as $y$ becomes more negative; but $f(y)$ should never be negative.
In fact, if I plot the second term of $f(y)$, it looks like, as $y$ becomes more and more negative:
$\sqrt {\frac 2 \pi} \cdot \sigma_x \cdot \frac {e^{- y^2}} {1 + erf(y)} \approx - \sqrt 2 \cdot \sigma_x \cdot y$
This is in substantial agreement with Claude's approximation for the $erf$ part.
I.e., using:
$\frac{e^{-y^2}}{1+\text{erf}(y)} \approx -\sqrt{\pi } \,y\Bigg[1+\frac{1}{2 y^2}-\frac{1}{2 y^4}+\frac{5}{4 y^6}+O\left(\frac{1}{y^8}\right)\Bigg]$
I get:
$\sqrt {\frac 2 \pi} \cdot \sigma_x \cdot \frac {e^{- y^2}} {1 + erf(y)} \approx \sqrt {\frac 2 \pi} \cdot \sigma_x \cdot (-\sqrt{\pi }) \,y\Bigg[1+\frac{1}{2 y^2}-\frac{1}{2 y^4}+\frac{5}{4
y^6}+O\left(\frac{1}{y^8}\right)\Bigg] =$
$= - \sqrt {2} \cdot \sigma_x \cdot \,y\Bigg[1+\frac{1}{2 y^2}-\frac{1}{2 y^4}+\frac{5}{4
y^6}+O\left(\frac{1}{y^8}\right)\Bigg]$
which indeed approaches $- \sqrt 2 \cdot \sigma_x \cdot y$ when $y$ becomes very negative.
With the above approximation:
$f(y) \approx \sqrt 2 \cdot \sigma_x \cdot y - \sqrt {2} \cdot \sigma_x \cdot \,y\Bigg[1+\frac{1}{2 y^2}-\frac{1}{2 y^4}+\frac{5}{4 y^6}+O\left(\frac{1}{y^8}\right)\Bigg] = $ $= - \sqrt {2} \cdot \sigma_x \cdot \,y\Bigg[\frac{1}{2 y^2}-\frac{1}{2 y^4}+\frac{5}{4 y^6}+O\left(\frac{1}{y^8}\right)\Bigg] $
Numerically, this 'does the trick', i.e. for $y \le -4$ it seems really very close to the function I need to compute, as indicated by Claude. And in fact with the rational function version even up to $y \le -1$.
This seems to imply that only polynomial approximations are viable, not the $erfc$-based ones, or am I wrong?
Any thoughts?