Skip to content

Commit a6e79f2

Browse files
committed
Restore Scipy-like precision for betainc
1 parent 2effaf1 commit a6e79f2

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

pytensor/scalar/c_code/incbet.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ Copyright 1984, 1995, 2000 by Stephen L. Moshier
1616
#include <numpy/npy_math.h>
1717

1818

19-
#define MINLOG -170.0
20-
#define MAXLOG +170.0
19+
// Constants borrowed from Scipy
20+
// https://github.com/scipy/scipy/blob/81c53d48a290b604ec5faa34c0a7d48537b487d6/scipy/special/special/cephes/const.h#L65-L78
21+
#define MINLOG -7.451332191019412076235E2 // log 2**-1022
22+
#define MAXLOG 7.09782712893383996732E2 // log(DBL_MAX)
2123
#define MAXGAM 171.624376956302725
22-
#define EPSILON 2.2204460492503131e-16
24+
#define EPSILON 1.11022302462515654042e-16 // 2**-53
2325

2426
DEVICE static double pseries(double, double, double);
2527
DEVICE static double incbcf(double, double, double);

pytensor/scalar/math.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1497,7 +1497,7 @@ def c_code(self, node, name, inp, out, sub):
14971497
raise NotImplementedError("type not supported", type)
14981498

14991499
def c_code_cache_version(self):
1500-
return (1,)
1500+
return (2,)
15011501

15021502

15031503
betainc = BetaInc(upgrade_to_float_no_complex, name="betainc")

tests/scalar/test_math.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,17 @@ def test_gammau_nan_c():
9999
assert np.isnan(test_func(-1, -1))
100100

101101

102-
def test_betainc():
102+
@pytest.mark.parametrize("linker", ["py", "c"])
103+
def test_betainc(linker):
103104
a, b, x = pt.scalars("a", "b", "x")
104105
res = betainc(a, b, x)
105-
test_func = function([a, b, x], res, mode=Mode("py"))
106+
test_func = function([a, b, x], res, mode=Mode(linker=linker, optimizer="fast_run"))
106107
assert np.isclose(test_func(15, 10, 0.7), sp.betainc(15, 10, 0.7))
107108

109+
# Regression test for https://github.com/pymc-devs/pytensor/issues/906
110+
if res.dtype == "float64":
111+
assert test_func(100, 1.0, 0.1) > 0
112+
108113

109114
def test_betainc_derivative_nan():
110115
a, b, x = pt.scalars("a", "b", "x")

0 commit comments

Comments
 (0)