Skip to content

Commit 823906a

Browse files
authored
Evaluate only desired branch in log1mexp_numpy (#4428)
* Evaluate only desired function in log1mexp_numpy * Do not change input inplace * Add release note
1 parent da76320 commit 823906a

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

RELEASE-NOTES.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Release Notes
22

3+
## PyMC3 vNext (on deck)
4+
5+
### Breaking Changes
6+
7+
### New Features
8+
9+
### Maintenance
10+
- `math.log1mexp_numpy` no longer raises RuntimeWarning when given very small inputs. These were commonly observed during NUTS sampling (see [#4428](https://github.com/pymc-devs/pymc3/pull/4428)).
11+
312
## PyMC3 3.11.0 (21 January 2021)
413

514
This release breaks some APIs w.r.t. `3.10.0`. It also brings some dreadfully awaited fixes, so be sure to go through the (breaking) changes below.

pymc3/math.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,13 @@ 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+
out = np.empty_like(x)
248+
mask = x < 0.6931471805599453 # log(2)
249+
out[mask] = np.log(-np.expm1(-x[mask]))
250+
mask = ~mask
251+
out[mask] = np.log1p(-np.exp(-x[mask]))
252+
return out
247253

248254

249255
def flatten_list(tensors):

pymc3/tests/test_math.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def test_log1pexp():
133133

134134
def test_log1mexp():
135135
vals = np.array([-1, 0, 1e-20, 1e-4, 10, 100, 1e20])
136+
vals_ = vals.copy()
136137
# import mpmath
137138
# mpmath.mp.dps = 1000
138139
# [float(mpmath.log(1 - mpmath.exp(-x))) for x in vals]
@@ -151,6 +152,15 @@ def test_log1mexp():
151152
npt.assert_allclose(actual, expected)
152153
actual_ = log1mexp_numpy(vals)
153154
npt.assert_allclose(actual_, expected)
155+
# Check that input was not changed in place
156+
npt.assert_allclose(vals, vals_)
157+
158+
159+
def test_log1mexp_numpy_no_warning():
160+
"""Assert RuntimeWarning is not raised for very small numbers"""
161+
with pytest.warns(None) as record:
162+
log1mexp_numpy(1e-25)
163+
assert not record
154164

155165

156166
class TestLogDet(SeededTest):

0 commit comments

Comments
 (0)