-
-
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
Create Ordered Multinomial distribution #4773
Conversation
This would also be nice for the other |
Just added that and addressed your other comments 🥳 |
Maybe it's easier to keep the two things separate. Have a helper class that creates the raw Ordered distribution and then, optionally, creates the deterministic p. You can retrieve class _OrderedMultinomial(Multinomial):
# standard implementation without the helper Deterministic
class OrderedMultinomial:
def __new__(cls, name, *args, save_p=True, **kwargs):
out_rv = _OrderedMultinomial(name, *args, **kwargs)
if save_p:
pm.Deterministic(f'{name}_p', out_rv.owner.inputs[4]) # I think this is the p vector in the multinomial
return out_rv
@classmethod
def dist(cls, *args, **kwargs):
return _OrderedMultinomial.dist(*args, **kwargs) No need to even check if it's inside the model context, that's done by the default |
|
Agreed, I was thinking the same -- that's actually how I did it for |
|
The RVs inputs are always of the form import aesara.tensor as at
x = at.random.multinomial(5, np.ones(3)/3, size=2)
print(x.owner.inputs)
|
Ooooh, ok, thanks! What does a |
It's a numerical code for different dtypes ("float32", "float64", "int32", and so on). I don't know where they are defined though. 4 is probably for "int64" since that's the default dtype of discrete RVs |
Ah ok, I wouldn't have guessed that 😅 |
The logp shouldn't have to be tested as it's just the Plus some tests for the helper Deterministic |
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!
Some conflicts in the files it seems. Other than that it looks good.
Codecov Report
@@ Coverage Diff @@
## main #4773 +/- ##
==========================================
+ Coverage 71.97% 72.05% +0.07%
==========================================
Files 85 85
Lines 13839 13872 +33
==========================================
+ Hits 9961 9995 +34
+ Misses 3878 3877 -1
|
The failing tests in |
I just added back the rv_op in the wrapper class. Tests pass locally. Is that a good solution? |
Actually I think testing the wrapped distribution might the best. Just test the _Ordered* classes instead. This shouldn't require any changes to the wrapper classes |
All tests are green 🥳 |
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 good! Left a minor suggestion that can be ignored.
Co-authored-by: Ricardo Vieira <[email protected]>
The failing test comes from #4771. It can be ignored.
|
Merging 🍾 Thanks for your help and reviews @ricardoV94 ! |
This PR enables the ordered logistic constraint on multinomial observed data (i.e aggregated by trial).
Currently,
OrderedLogistic
only accepts data in a disaggregated format (i.eCategorical
observed data).I also added the option of having the inferred probabilities in the trace (not only the cutpoints, which are hard to interpret anyway). As I'm guessing that's mostly what people care about when using this likelihood, the option is
True
by default (it's only useful to disable it when memory usage is an issue).This probably needs a few tests (inspired by those of
OrderedLogistic
), but here is already a proof of concept:Which does recover the true probabilities:
RELEASE-NOTES.md