-
-
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
Made sample_shape same across all contexts in draw_values #4305
Conversation
Codecov Report
@@ Coverage Diff @@
## master #4305 +/- ##
==========================================
- Coverage 87.56% 87.54% -0.02%
==========================================
Files 88 88
Lines 14270 14272 +2
==========================================
Hits 12495 12495
- Misses 1775 1777 +2
|
pymc3/distributions/simulator.py
Outdated
params = draw_values([*self.params], point=point, size=size) | ||
if size is None: | ||
if not size: | ||
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])]) |
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.
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. :)
I rebased to current master. So, the PR is ready for review. |
pymc3/distributions/simulator.py
Outdated
@@ -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: |
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.
if not size: | |
if len(size) != 0: |
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 am checking for size
being empty tuple here. So, do you mean len(size) == 0
?
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.
Yes.
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.
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).
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.
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):
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.
can lead to really subtle and difficult to debug bugs
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.
Yes. I like this idea. Now, I have opted to use to_tuple
to better handle corner cases.
@Sayam753 Looks great, can you add this to the release-notes? |
Thanks @Sayam753! |
Thank your for opening a PR!
Depending on what your PR does, here are a few things you might want to address in the description:
Closes Issue with prior_predictive_sample and MvNormal #3758
There was a minor issue in this condition with respect to
size
.https://github.com/pymc-devs/pymc3/blob/de47253ce901ac9afac3a0fe33d3cb96f7f354db/pymc3/distributions/distribution.py#L703-L704
Coming to the code,
Variable
a
is drawn as('a ~ Normal', 1000)
and stored indrawn_vars
. But when looking forMvNormal
, the condition is evaluated as('a ~ Normal', (1000, ))
with size as tuple and there is a mismatch. So, I converted the size beforehand usingto_tuple
to ensure a common base sample_shape across contexts.Another approach could have been the use
to_tuple
before passing size todraw_values
. But this needs to be done for all distributions. So, I opted for the first approach.Yes. Earlier in Fix MvNormal.random #4207, I have written a test for first half of the issue Issue with prior_predictive_sample and MvNormal #3758. This PR just appends that test with the second half.