Skip to content

Commit e9ddb20

Browse files
committed
Restore Scipy-like precision for betainc
1 parent 56c30e0 commit e9ddb20

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-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
@@ -1513,7 +1513,7 @@ def c_code(self, node, name, inp, out, sub):
15131513
raise NotImplementedError("type not supported", type)
15141514

15151515
def c_code_cache_version(self):
1516-
return (1,)
1516+
return (2,)
15171517

15181518

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

tests/scalar/test_math.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,14 @@ 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))
108+
# Regression test for https://github.com/pymc-devs/pytensor/issues/906
109+
assert test_func(100, 1.0, 0.1) > 0
107110

108111

109112
def test_betainc_derivative_nan():

0 commit comments

Comments
 (0)