GSL.jl icon indicating copy to clipboard operation
GSL.jl copied to clipboard

Incorrect values in hypergeometric_2F1

Open amellnik opened this issue 6 years ago • 5 comments

My coworker @nicolasdidier noticed this strange issue with sf_hyperg_2F1 on half of the domain:

image

(I wasn't able to test it on 1.0 due to unsatisfiable package requirements.) Is there any way this could be an issue with this package or is this definitely due to GSL?

amellnik avatar Oct 03 '18 17:10 amellnik

I'm reasonably sure that it's the same thing as this bug in GSL itself: http://lists.gnu.org/archive/html/bug-gsl/2015-09/msg00003.html . I'll keep this issue open to track when it's fixed upstream.

amellnik avatar Oct 03 '18 17:10 amellnik

It looks like it has been fixed upstream. This package is still using a quite old version of GSL, so should be upgraded.

simonbyrne avatar Oct 03 '18 17:10 simonbyrne

This still appears to be an issue, even after switching to GSL 2.5. Interestingly, the error estimate provided by GSL is also completely off

using GSL, PyPlot, PyCall

x = 0:0.01:1
a, b, c = -1/4, 1/4, 1
out = sf_hyperg_2F1_e.(a, b, c, x)
gslval = [r.val for r in out]
gslerr = [r.err for r in out]
pyval = pyimport("scipy.special")[:hyp2f1](a, b, c, x)

subplot(2,1,1);
plot(x, gslval, ".", label="GSL value")
plot(x, pyval, "--", label="SciPy value")
legend(); grid(); title("sf_hyperg_2F1_e(-1/4, 1/4, 1, x)")
subplot(2,1,2);
semilogy(x, gslerr, ".", label="GSL error est")
legend(); grid()

image

ludvigak avatar Nov 08 '18 19:11 ludvigak

It seems to be the second bug mentioned in #21835:

The function gsl_sf_hyperg_2F1_e calls hyperg_2F1_series for x<0.5, but hyperg_2F1_reflect for x>0.5. The latter function checks for c-a-b being an integer (which it is in my case). If I change one of the parameters a,b,c by a small amount, the other branch of the function is taken and the results are correct again. Unfortunately I know too little about the numerics of 2F1 to suggest a patch at the moment.

simonbyrne avatar Nov 09 '18 05:11 simonbyrne

That's too bad. There actually seems to be quite a few bug reports on gsl_sf_hyperg_2F1. Let's just hope they get fixed in a future version.

ludvigak avatar Nov 09 '18 23:11 ludvigak