Skip to content

Commit 62c6326

Browse files
committed
Evaluate only desired function in log1mexp_numpy
1 parent aedc8e9 commit 62c6326

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

pymc3/math.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,12 @@ def log1mexp_numpy(x):
243243
For details, see
244244
https://cran.r-project.org/web/packages/Rmpfr/vignettes/log1mexp-note.pdf
245245
"""
246-
return np.where(x < 0.6931471805599453, np.log(-np.expm1(-x)), np.log1p(-np.exp(-x)))
246+
x = np.asarray(x)
247+
mask = x < 0.6931471805599453
248+
x[mask] = np.log(-np.expm1(-x[mask]))
249+
mask = ~mask
250+
x[mask] = np.log1p(-np.exp(-x[mask]))
251+
return x
247252

248253

249254
def flatten_list(tensors):

pymc3/tests/test_math.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ def test_log1mexp():
153153
npt.assert_allclose(actual_, expected)
154154

155155

156+
def test_log1mexp_numpy_no_warning():
157+
"""Assert RuntimeWarning is not raised for very small numbers"""
158+
with pytest.warns(None) as record:
159+
log1mexp_numpy(1e-25)
160+
assert not record
161+
162+
156163
class TestLogDet(SeededTest):
157164
def setup_method(self):
158165
super().setup_method()

0 commit comments

Comments
 (0)