When $a, b \gg 1$ and $x \approx a / (a+b)$, the incomplete beta function (ibeta) will throw an error because the series expansion doesn't converge. This was first found in scipy/scipy#24566
typedef double T;
T a = 3.1622776601699636e+16;
T b = 3.130654883566682e+18;
T x = 0.010000000000005001;
std::cout << boost::math::ibeta(a, b, x + 1e-12) << std::endl; // 0.50713
std::cout << boost::math::ibeta(a, b, x - 1e-12) << std::endl; // 0.49287
// Error in function boost::math::ibeta: Series evaluation exceeded 1000000 iterations, giving up now.
std::cout << std::setprecision(64) << boost::math::ibeta(a, b, x + 1e-13)<< std::endl;
return 0;
The issue is with continued fraction in here. I've tried using the erf asymptotic expansion here, but it gave horrible results.
I came up with a tentative solution in Python that is somewhat accurate, but it feels quite hacky. In the asymptotic expansion above (here), it solves for
$$\eta^2 = -2 \left( x_0 \ln\left(\frac{x}{x_0}\right) + (1-x_0) \ln\left(\frac{1-x}{1-x_0}\right) \right)$$
where $x_0 = a/(a+b)$. Some care needs to be taken to see if $\eta$ is positive or negative, but this is already handled in the implementation. When $x \approx x_0$, we can Taylor expand about $x_0$ to find the approximate solution
$$\eta \approx \frac{x - x_0}{\sqrt{x_0(1-x_0)}}.$$
This taylor approximation actually works fairly well for the example above. When more terms are include in the continued fraction, it seems to be converging to the result in the function ibeta_large_ab with the taylor approximation for $\eta$.
I only see two issues in regards to using the taylor expansion for $\eta$. First, why does the taylor expansion of $\eta$ give a more accurate result for ibeta than using the entire equation for $\eta$? Is it some sort of precision issue with the logs? Second, if we should taylor expand $\eta$, in what regime is this accurate? I found that for $x_0 \pm \delta$ where $\delta < 10^{-13}$ the series fails to converge and the expansion works well. Should $10^{-13}$ just be hard coded into the function here?
When$a, b \gg 1$ and $x \approx a / (a+b)$ , the incomplete beta function (
ibeta) will throw an error because the series expansion doesn't converge. This was first found in scipy/scipy#24566The issue is with continued fraction in here. I've tried using the
erfasymptotic expansion here, but it gave horrible results.I came up with a tentative solution in Python that is somewhat accurate, but it feels quite hacky. In the asymptotic expansion above (here), it solves for
where$x_0 = a/(a+b)$ . Some care needs to be taken to see if $\eta$ is positive or negative, but this is already handled in the implementation. When $x \approx x_0$ , we can Taylor expand about $x_0$ to find the approximate solution
This taylor approximation actually works fairly well for the example above. When more terms are include in the continued fraction, it seems to be converging to the result in the function$\eta$ .
ibeta_large_abwith the taylor approximation forI only see two issues in regards to using the taylor expansion for$\eta$ . First, why does the taylor expansion of $\eta$ give a more accurate result for $\eta$ ? Is it some sort of precision issue with the logs? Second, if we should taylor expand $\eta$ , in what regime is this accurate? I found that for $x_0 \pm \delta$ where $\delta < 10^{-13}$ the series fails to converge and the expansion works well. Should $10^{-13}$ just be hard coded into the function here?
ibetathan using the entire equation for