Skip to content

Fix rejection-based truncation of scalar variables #6923

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions pymc/distributions/truncated.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,14 @@ def rv_op(cls, dist, lower, upper, max_n_steps, size=None):
# Fallback to rejection sampling
def loop_fn(truncated_rv, reject_draws, lower, upper, rng, *rv_inputs):
next_rng, new_truncated_rv = dist.owner.op.make_node(rng, *rv_inputs).outputs
truncated_rv = pt.set_subtensor(
truncated_rv[reject_draws],
new_truncated_rv[reject_draws],
)
# Avoid scalar boolean indexing
if truncated_rv.type.ndim == 0:
truncated_rv = new_truncated_rv
else:
truncated_rv = pt.set_subtensor(
truncated_rv[reject_draws],
new_truncated_rv[reject_draws],
)
reject_draws = pt.or_((truncated_rv < lower), (truncated_rv > upper))
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this also have some kind of pt.and_(not(reject_draws), ... so that the draws that were already accepted don't get resampled? I fail to see where that is happening.

Copy link
Member Author

Choose a reason for hiding this comment

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

The draws that were already accepted will always be within upper and lower. The set_subtensor only changes the indexes that were not already valid.


return (
Expand Down
7 changes: 4 additions & 3 deletions tests/distributions/test_truncated.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ def test_truncation_specialized_op(shape_info):

@pytest.mark.parametrize("lower, upper", [(-1, np.inf), (-1, 1.5), (-np.inf, 1.5)])
@pytest.mark.parametrize("op_type", ["icdf", "rejection"])
def test_truncation_continuous_random(op_type, lower, upper):
@pytest.mark.parametrize("scalar", [True, False])
def test_truncation_continuous_random(op_type, lower, upper, scalar):
loc = 0.15
scale = 10
normal_op = icdf_normal if op_type == "icdf" else rejection_normal
x = normal_op(loc, scale, name="x", size=100)
x = normal_op(loc, scale, name="x", size=() if scalar else (100,))

xt = Truncated.dist(x, lower=lower, upper=upper)
assert isinstance(xt.owner.op, TruncatedRV)
Expand Down Expand Up @@ -134,7 +135,7 @@ def test_truncation_continuous_random(op_type, lower, upper):
assert np.unique(xt_draws).size == xt_draws.size
else:
with pytest.raises(TruncationError, match="^Truncation did not converge"):
draw(xt)
draw(xt, draws=100 if scalar else 1)


@pytest.mark.parametrize("lower, upper", [(-1, np.inf), (-1, 1.5), (-np.inf, 1.5)])
Expand Down