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

Conversation

Sayam753
Copy link
Member

@Sayam753 Sayam753 commented Dec 6, 2020

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:

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,

ndim = 50
with pm.Model() as model:
    a = pm.Normal("a", sd=100, shape=ndim)
    c = pm.MvNormal("c", mu=a, chol=np.linalg.cholesky(np.eye(ndim)), shape=ndim)
    samples = pm.sample_prior_predictive(1000)

Variable a is drawn as ('a ~ Normal', 1000) and stored in drawn_vars. But when looking for MvNormal, the condition is evaluated as ('a ~ Normal', (1000, )) with size as tuple and there is a mismatch. So, I converted the size beforehand using to_tuple to ensure a common base sample_shape across contexts.

Another approach could have been the use to_tuple before passing size to draw_values. But this needs to be done for all distributions. So, I opted for the first approach.

@codecov
Copy link

codecov bot commented Dec 6, 2020

Codecov Report

Merging #4305 (da81fd8) into master (4fd56fd) will decrease coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
pymc3/distributions/distribution.py 95.03% <100.00%> (+0.75%) ⬆️
pymc3/distributions/simulator.py 82.43% <100.00%> (+0.24%) ⬆️
pymc3/distributions/multivariate.py 82.75% <0.00%> (-0.58%) ⬇️
pymc3/distributions/posterior_predictive.py 89.01% <0.00%> (-0.29%) ⬇️

Comment on lines 114 to 121
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])])
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. :)

@Sayam753
Copy link
Member Author

I rebased to current master. So, the PR is ready for review.

@@ -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.

@twiecki
Copy link
Member

twiecki commented Dec 10, 2020

@Sayam753 Looks great, can you add this to the release-notes?

RELEASE-NOTES.md Outdated Show resolved Hide resolved
@twiecki twiecki merged commit b953c40 into pymc-devs:master Dec 11, 2020
@twiecki
Copy link
Member

twiecki commented Dec 11, 2020

Thanks @Sayam753!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Issue with prior_predictive_sample and MvNormal
3 participants