-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Fix bug in which TruncatedNormal returns -inf for all values if any value is out of bounds #6128
Conversation
Co-authored-by: Ricardo Vieira <28983449+ricardoV94@users.noreply.github.com>
Co-authored-by: Ricardo Vieira <28983449+ricardoV94@users.noreply.github.com>
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #6128 +/- ##
==========================================
+ Coverage 90.90% 92.05% +1.14%
==========================================
Files 99 102 +3
Lines 20543 21299 +756
==========================================
+ Hits 18675 19607 +932
+ Misses 1868 1692 -176
|
pymc/distributions/continuous.py
Outdated
@@ -777,11 +777,13 @@ def logp( | |||
norm = 0.0 | |||
|
|||
logp = _logprob(normal, (value,), None, None, None, mu, sigma) - norm | |||
logp = at.switch( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually to be equivalent to what we had before we should do something like this:
pymc/pymc/distributions/truncated.py
Lines 316 to 320 in c53cd2f
if is_lower_bounded: | |
logp = at.switch(value < lower, -np.inf, logp) | |
if is_upper_bounded: | |
logp = at.switch(value <= upper, logp, -np.inf) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't that equivalent to what is implemented here because of the default values for lower and upper?
pymc/pymc/distributions/continuous.py
Lines 714 to 715 in 91cbebd
lower = at.as_tensor_variable(floatX(lower)) if lower is not None else at.constant(-np.inf) | |
upper = at.as_tensor_variable(floatX(upper)) if upper is not None else at.constant(np.inf) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We retrieve the None case here:
pymc/pymc/distributions/continuous.py
Lines 763 to 764 in 91cbebd
unbounded_lower = isinstance(lower, TensorConstant) and np.all(lower.value == -np.inf) | |
unbounded_upper = isinstance(upper, TensorConstant) and np.all(upper.value == np.inf) |
So in those cases we avoid introducing the useless switch. It's a small optimization but I don't see any reason yo modify it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see. OK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The last commit makes the implementation here more analogous to the general truncated case you linked above - thanks for that pointer!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @ricardoV94 for taking a look and helping out already! Let me know if the implementation in the latest few commits looks ok. Also, it looks like this PR is waiting for approval to run the full test suite.
Pre-commit is complaining, otherwise looks good |
05c5981
to
8c9dbd2
Compare
Cool - I forgot to |
Thanks @adrn ! |
What is this PR about?
With pymc v4.1.7, I found that evaluating
TruncatedNormal
'slogp
with an array of values was returning all-inf
values -- for example:Output:
In the output above, the first line should not be
-inf
everywhere, as the grid we evaluate on includes values in the allowed range of values.With @ricardoV94's help, we tracked this down to the way that
TruncatedNormal.logp
was enforcing the value bounds:https://github.com/pymc-devs/pymc/blob/main/pymc/distributions/continuous.py#L779
I noticed this comment in the
check_parameters()
docstring: "Note that check_parameter should not be used to enforce the logic of the logp expression under the normal parameter support as it can be disabled by the user via check_bounds = False in pm.Model()" and indeed the above example works as expected withcheck_bounds=False
.This PR instead follows the implementation in other truncated distributions, for example, HalfStudentT to use a
switch
statement instead. I also added a regression test for the example case above.See https://discourse.pymc.io/t/truncatednormal-logp-returning-all-inf/10398 for more context.
Checklist
Major / Breaking Changes
Bugfixes / New features
TruncatedNormal
would return -inf for all logp values if any input value was outside of the bounds.Docs / Maintenance