Skip to content

Problem with bounded variables when the value lies near the boundary #3938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lcontento opened this issue May 31, 2020 · 2 comments · Fixed by #4418
Closed

Problem with bounded variables when the value lies near the boundary #3938

lcontento opened this issue May 31, 2020 · 2 comments · Fixed by #4418

Comments

@lcontento
Copy link

When the value of a bounded variable lies near the boundary, the log-posterior becomes inf. For example,

import numpy as np
import pymc3 as pm

lb = -1.0
ub = 1e-7
x0 = np.nextafter(ub, lb)

with pm.Model() as model:
    pm.Uniform('x', testval=x0, lower=lb, upper=ub)

model.check_test_point()

prints

x_interval__   -inf
Name: Log-probability of test_point, dtype: float64

This is due to the formula used for linear interpolation in the backward transform of Interval
https://github.com/pymc-devs/pymc3/blob/b6a88f066e4d694a4cdafb1b7696772f045c9b07/pymc3/distributions/transforms.py#L273
which, when a and b are of different orders of magnitude, may return values outside of the closed interval [a, b].
To see this, run

x_interval = model.basic_RVs[0]
tau = x_interval.distribution.transform_used
tau.backward(tau.forward(x0)).eval() > ub

which gives True.

If such formula is substituted with e.g.

f = tt.nnet.sigmoid(x)
r = f *b + (1 - f) * a

(which is better behaved when the difference of magnitude is large), the problem disappears. Not being very familiar with PyMC3 I am not sure which linear interpolation formula would be the best choice.

@ColCarroll
Copy link
Member

This looks like a fine solution to me -- would you be willing to write a patch?

@lcontento
Copy link
Author

Sure. I will try to add a test for this problem too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants