Skip to content

Fixed log posterior of bounded variables when the value lies near the boundary #4418

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

Merged
merged 1 commit into from
Jan 19, 2021

Conversation

kc611
Copy link
Contributor

@kc611 kc611 commented Jan 15, 2021

Fix for #3938

Fix sugested by @lcontento over here #3938 (comment)

The following code :

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()

Now returns :

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

Instead of -inf.

@kc611 kc611 closed this Jan 15, 2021
@kc611 kc611 reopened this Jan 15, 2021
@codecov
Copy link

codecov bot commented Jan 15, 2021

Codecov Report

Merging #4418 (6da3475) into master (4e2c099) will decrease coverage by 0.03%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4418      +/-   ##
==========================================
- Coverage   88.05%   88.01%   -0.04%     
==========================================
  Files          88       88              
  Lines       14399    14343      -56     
==========================================
- Hits        12679    12624      -55     
+ Misses       1720     1719       -1     
Impacted Files Coverage Δ
pymc3/distributions/transforms.py 97.71% <100.00%> (+<0.01%) ⬆️
pymc3/distributions/multivariate.py 83.54% <0.00%> (-1.05%) ⬇️
pymc3/sampling.py 89.62% <0.00%> (-0.11%) ⬇️
pymc3/distributions/posterior_predictive.py 89.08% <0.00%> (-0.07%) ⬇️
pymc3/distributions/__init__.py 100.00% <0.00%> (ø)
pymc3/distributions/distribution.py 94.87% <0.00%> (+0.25%) ⬆️

@michaelosthege michaelosthege added this to the vNext (3.11.0) milestone Jan 15, 2021
@AlexAndorra AlexAndorra changed the title Fixed log posterior when bounded variables when the value lies near the boundary Fixed log posterior of bounded variables when the value lies near the boundary Jan 15, 2021
Copy link
Contributor

@AlexAndorra AlexAndorra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @kc611 ! Giving it a few hours before merging, to see what @ColCarroll thinks

@MarcoGorelli
Copy link
Contributor

Hi @kc611 ,

Seeing as you have a reproducible snippet of code which fails on master but passes here, would it be possible to add it as a test case to ensure this doesn't break again in the future?

@ColCarroll
Copy link
Member

This solution looks great -- +1 to @MarcoGorelli's suggestion of adding the example as a test. Probably around https://github.com/pymc-devs/pymc3/blob/master/pymc3/tests/test_transforms.py#L197, you would add something like

def test_interval_near_boundary():
   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)
  
  log_prob = model.check_test_point()
  np.testing.assert_allclose(log_prob.values, np.array([-52.68]))

@kc611
Copy link
Contributor Author

kc611 commented Jan 19, 2021

@MarcoGorelli @ColCarroll Sorry for late reply. I added the tests as requested.

@michaelosthege
Copy link
Member

@kc611 it looks like the tests failed in the CI for float32. You probably are on float64 locally.. You can probably add a @pytest.mark.skipif for the float32 case. There are probably other tests from which you can copy..

Copy link
Contributor

@AlexAndorra AlexAndorra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @kc611 ! Merging now 🍾

@AlexAndorra AlexAndorra merged commit 32b5c94 into pymc-devs:master Jan 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Problem with bounded variables when the value lies near the boundary
5 participants