Skip to content
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

Made sample_shape same across all contexts in draw_values #4305

Merged
merged 6 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions pymc3/distributions/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ def draw_values(params, point=None, size=None):
"""
# The following check intercepts and redirects calls to
# draw_values in the context of sample_posterior_predictive
size = to_tuple(size)
ppc_sampler = vectorized_ppc.get(None)
if ppc_sampler is not None:
# this is being done inside new, vectorized sample_posterior_predictive
Expand Down
4 changes: 2 additions & 2 deletions pymc3/distributions/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ def random(self, point=None, size=None):
array
"""
params = draw_values([*self.params], point=point, size=size)
if size is None:
if not size:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if not size:
if len(size) != 0:

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, I am checking for size being empty tuple here. So, do you mean len(size) == 0?

Copy link
Member

Choose a reason for hiding this comment

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

Yes.

Copy link
Member Author

Choose a reason for hiding this comment

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

But if there is a call to this random method directly, then size will be None. I prefer to leave it as is, since it handles both cases (being None or empty tuple).

Copy link
Member

@twiecki twiecki Dec 10, 2020

Choose a reason for hiding this comment

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

Relying on the truthiness of an object can lead to really subtle and difficult to debug bugs. In that case I would be explicit, even if it doesn't look as clean:
if (size is None) or (len(size) == 0):

Copy link
Contributor

Choose a reason for hiding this comment

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

can lead to really subtle and difficult to debug bugs

Indeed

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. I like this idea. Now, I have opted to use to_tuple to better handle corner cases.

return self.function(*params)
else:
return np.array([self.function(*params) for _ in range(size)])
return np.array([self.function(*params) for _ in range(size[0])])
Copy link
Member Author

Choose a reason for hiding this comment

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

I handled size this way because now it is passed as a tuple. Ping @aloctavodia to review as this is related to SMC stuff.
Also, test suite passes. :)


def _str_repr(self, name=None, dist=None, formatting="plain"):
if dist is None:
Expand Down
4 changes: 4 additions & 0 deletions pymc3/tests/test_distributions_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,10 @@ def test_issue_3758(self):
for var in "abcd":
assert not np.isnan(np.std(samples[var]))

for var in "bcd":
std = np.std(samples[var] - samples["a"])
np.testing.assert_allclose(std, 1, rtol=1e-2)

def test_issue_3829(self):
with pm.Model() as model:
x = pm.MvNormal("x", mu=np.zeros(5), cov=np.eye(5), shape=(2, 5))
Expand Down