-
-
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
Dirichlet multinomial (continued) #4373
Dirichlet multinomial (continued) #4373
Conversation
Awesome, thanks for picking that up @ricardoV94 ! Let me know when I can review 😉 |
Sure @AlexAndorra, but it might still take a while ;) |
Take your time, I'll be there -- I can't go outside anyway 😜 |
@ricardoV94 I agree with all your skepticism of my initial implementation's shape management and tests. My intuition is that the API (and probably tests) should mirror Hopefully I'll be able to contribute to this PR. :) |
@bsmith89 It would be great to have your input, specially since you started everything! Is there a way we could chat? |
For sure! I started a topic on the Discourse, so we don't have to spam this PR too much. Also happy to find a more synchronous way to discuss if that's helpful. |
Any progress on this? |
@bsmith89 is working hard on this and I am helping with the unittests. We are waiting until we have some more progress before pushing new changes. |
@ricardoV94 Indeed, just checked the thread and love the deep thinking and discussions that go into this! |
Ported over the Multinomial tests and got most of them passing (and I'll push those commits to this PR momentarily) but right now I'm having issues with a DM version of the Multinomial test @lucianopaz, I think you wrote that test in #4169. Can you explain what it's supposed to check? Could we get the same assurances with something more analogous to |
@lucianopaz will certainly know better, but from a quick glance it seems to test that Multinomial batches (3d shape?) are correctly evaluating the logp and generating samples by initializing the distribution with sparse probability vectors where only one value/ category has a probability of 1 and everything else is 0. This makes it a deterministic function and hence easy to test, but it wouldn't work for the DM, since the Dirichlet component is never deterministic (and, more prosaically, because the alpha cannot be zero). I might also be completely wrong :) Are you sure you are passing valid alpha parameters to the DM copy of the test (i.e., not including any zero)? That should cause it to fail immediately. Whether fixing that would be enough to make it pass is another question :b Also are you failing in the logp assertion, the random sample or both? |
@bsmith89, what @ricardoV94 said was right. |
Checking in on that now. I'm on this PR branch (not my own) running |
pymc3/distributions/multivariate.py
Outdated
if len(self.shape) > 1: | ||
self.n = tt.shape_padright(n) | ||
self.alpha = tt.as_tensor_variable(alpha) if alpha.ndim > 1 else tt.shape_padleft(alpha) |
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.
@lucianopaz @ricardoV94, not sure if this is quite what you meant, but I do the exact same reshaping as in the Multinomial.
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.
No. I meant that in the Multinomial
Distribution, we used to have a line in __init__
that did: n = np.squeeze(n)
. That line caused all sorts of problems when we had n.ndim > 1
. This situation wasn't covered by existing tests so I wrote up test_batch_multinomial
. A better approach would be to either parametrize a single test with multiple n
and alpha
values that span many shapes, or add test fixtures for n
and alpha
to achieve the same result.
Gotta sign off for a bit, but I still haven't fully grokked what needs to happen with this test. Suggestions welcome! I'll take a look probably later today. |
As suggested in pymc-devs#3639 (comment) Also see: pymc-devs#3639 (comment) but this seems to be part of a broader discussion.
Thanks, that seems to have solved it! |
@bsmith89 Anything missing in this PR (not including the example Notebook)? |
LGTM! Should I rebase onto main and add a release note, or are those last items someone else's responsibility? |
Feel free to add the release note. It should work fine without rebasing. In the meanwhile I will open the PR for review |
@AlexAndorra If you are still interested in reviewing this one, we are ready :) |
@ricardoV94 not sure if I just messed up... 😕 There was conflict between my one line RELEASE-NOTES change and pymc3/master, so I followed GitHub's prompt to resolve that. Didn't realize it would merge everything else too... Is that a problem? |
I think it's fine. It's still only showing 5 files changed |
Awesome @ricardoV94 and @bsmith89 ! I'll take some time to review this week 🥳 |
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.
Well done @ricardoV94 and @bsmith89, I really love that 😍
I just had a few comments below, mainly slight improvements that should be quick to implement if they are appropriate.
There are also two failing tests: one in MvNormal's sample_prior_predictive
, so probably not related to this PR (cc @Sayam753); another in test_interval
(this one is more mysterious to me -- does it ring any bells?)
Thanks @AlexAndorra for the ping. The failing |
I re-ran that test locally and it passed without problems. Seems like it might be a rare failing. Don't know why it would be stochastic... |
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.
Some minor nitpicks below -
|
||
super().__init__(shape=shape, defaults=("_defaultval",), *args, **kwargs) |
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.
Dirichlet distribution makes use of get_test_value
function to compute its distribution shape. Can we use get_test_value
to determine shape here as well? Doing so, will even us help in #4379.
Ping @brandonwillard to ask how does get_test_value
function work?
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 don't think it is as simple, since the shape can be influenced by the n
parameter as well as the a
, whereas in the Dirichlet all information is necessarily contained in the a
(when shape is not specified)
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.
Also the Dirichlet functionality is wrapped in a DeprecationWarning (even though I don't seem to be able to trigger it), which suggests that they wanted to abandon that approach at some point.
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.
@ricardoV94 , just a follow up, it indeed makes sense to avoid the use of get_test_value
function as also discussed here #4000 (comment)
@AlexAndorra, I think we've addressed your comments now.
Was there anything else? |
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 thought they reviewed already. I see this indicator higher up in this thread: Did you want him to do a second review given @ricardoV94's two commits since then? |
I have seen |
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.
LGTM. Great work @ricardoV94 @bsmith89 🤩 . Last, there needs to be a mention in api source multivariate.rst for PyMC3 docs.
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.
Looks all good now 🤩 Well done guys for this big PR 👏
Update: @bsmith89 has been working hard and this PR is getting close to review point.
The current state of the TODO list is:
alpha
parameter toa
everywhere (to be consistent withDirichlet
, even thoughalpha
would probably have been better for both)DirichletMultinomial
andBetaBinomial
match when there are only two categories.test_batch_dirichlet_multinomial
whose logic is not directly transferable)test_batch_dirichlet_multinomial
..random()
Multinomial
. Future changes in this respect can be done in a separate PR that addresses both distributions..random()
failing when shape is not explicitly specified. Update: shape will be always required to avoid issues.self._defaultval
instead to avoid confusing users.