-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Tweak DirichletMultinomial logp and refactor some multivariate logp tests #5234
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cc3c675
Pin aeppl version
ricardoV94 eb925bc
Simplify DirichletMultinomial logp and remove restriction on dimensio…
ricardoV94 248131d
Remove legacy docstrings Multinomial restriction on dimensionality of…
ricardoV94 4ed1042
Refactor dirichlet vectorized logp tests
ricardoV94 82dc0f5
Harmonize Multinomial, Dirichlet and DirichletMultinomial docstrings
ricardoV94 0549f93
Remove stale/redundant random tests
ricardoV94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -406,8 +406,9 @@ class Dirichlet(Continuous): | |
|
||
Parameters | ||
---------- | ||
a: array | ||
Concentration parameters (a > 0). | ||
a: float array | ||
Concentration parameters (a > 0). The number of categories is given by the | ||
length of the last axis. | ||
""" | ||
rv_op = dirichlet | ||
|
||
|
@@ -507,13 +508,12 @@ class Multinomial(Discrete): | |
|
||
Parameters | ||
---------- | ||
n: int or array | ||
Number of trials (n > 0). If n is an array its shape must be (N,) with | ||
N = p.shape[0] | ||
p: one- or two-dimensional array | ||
Probability of each one of the different outcomes. Elements must | ||
be non-negative and sum to 1 along the last axis. They will be | ||
automatically rescaled otherwise. | ||
n: int | ||
Total counts in each replicate (n > 0). | ||
p: float array | ||
Probability of each one of the different outcomes (0 <= p <= 1). The number of | ||
categories is given by the length of the last axis. Elements are expected to sum | ||
to 1 along the last axis, and they will be automatically rescaled otherwise. | ||
""" | ||
rv_op = multinomial | ||
|
||
|
@@ -626,17 +626,12 @@ class DirichletMultinomial(Discrete): | |
|
||
Parameters | ||
---------- | ||
n : int or array | ||
Total counts in each replicate. If n is an array its shape must be (N,) | ||
with N = a.shape[0] | ||
|
||
a : one- or two-dimensional array | ||
Dirichlet parameter. Elements must be strictly positive. | ||
The number of categories is given by the length of the last axis. | ||
n : int | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same remark as for |
||
Total counts in each replicate (n > 0). | ||
|
||
shape : integer tuple | ||
Describes shape of distribution. For example if n=array([5, 10]), and | ||
a=array([1, 1, 1]), shape should be (2, 3). | ||
a : float array | ||
Dirichlet concentration parameters (a > 0). The number of categories is given by | ||
the length of the last axis. | ||
""" | ||
rv_op = dirichlet_multinomial | ||
|
||
|
@@ -661,15 +656,10 @@ def logp(value, n, a): | |
------- | ||
TensorVariable | ||
""" | ||
if value.ndim >= 1: | ||
n = at.shape_padright(n) | ||
if a.ndim > 1: | ||
a = at.shape_padleft(a) | ||
|
||
sum_a = a.sum(axis=-1, keepdims=True) | ||
sum_a = a.sum(axis=-1) | ||
const = (gammaln(n + 1) + gammaln(sum_a)) - gammaln(n + sum_a) | ||
series = gammaln(value + a) - (gammaln(value + 1) + gammaln(a)) | ||
result = const + series.sum(axis=-1, keepdims=True) | ||
result = const + series.sum(axis=-1) | ||
|
||
# Bounds checking to confirm parameters and data meet all constraints | ||
# and that each observation value_i sums to n_i. | ||
|
@@ -678,13 +668,10 @@ def logp(value, n, a): | |
value >= 0, | ||
a > 0, | ||
n >= 0, | ||
at.eq(value.sum(axis=-1, keepdims=True), n), | ||
at.eq(value.sum(axis=-1), n), | ||
broadcast_conditions=False, | ||
) | ||
|
||
def _distr_parameters_for_repr(self): | ||
return ["n", "a"] | ||
|
||
|
||
class _OrderedMultinomial(Multinomial): | ||
r""" | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
n
can be a vector too, can't it? E.g if the number of trials vary by observationThere 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.
Yeah n can be a vector or a matrix, anything as long as dimensions broadcast properly. I put the int to indicate the base case, but perhaps the best is to remove any information of shape and only mention the meaning of the parameters?
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.
Why not
int, vector, matrix
? That's more explicitThere 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 find that very helpful. Other multivariate distributions use an ambiguous "array" and most univariate distributions say only "int" or "float" even though they also support vectors and tensors of any shape.