Skip to content

Commit 32b5c94

Browse files
authored
Fix logp of bounded variables when value lies near the boundary (#4418)
Closes #3938
1 parent acb3261 commit 32b5c94

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

pymc3/distributions/transforms.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ def __init__(self, a, b):
278278

279279
def backward(self, x):
280280
a, b = self.a, self.b
281-
r = (b - a) * tt.nnet.sigmoid(x) + a
281+
sigmoid_x = tt.nnet.sigmoid(x)
282+
r = sigmoid_x * b + (1 - sigmoid_x) * a
282283
return r
283284

284285
def forward(self, x):

pymc3/tests/test_transforms.py

+13
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,19 @@ def test_interval():
196196
close_to_logical(vals < b, True, tol)
197197

198198

199+
@pytest.mark.skipif(theano.config.floatX == "float32", reason="Test fails on 32 bit")
200+
def test_interval_near_boundary():
201+
lb = -1.0
202+
ub = 1e-7
203+
x0 = np.nextafter(ub, lb)
204+
205+
with pm.Model() as model:
206+
pm.Uniform("x", testval=x0, lower=lb, upper=ub)
207+
208+
log_prob = model.check_test_point()
209+
np.testing.assert_allclose(log_prob.values, np.array([-52.68]))
210+
211+
199212
def test_circular():
200213
trans = tr.circular
201214
check_transform(trans, Circ)

0 commit comments

Comments
 (0)