Skip to content

Adding Dirichlet moment and tests #5174

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 4 commits into from
Nov 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ docker exec -it pymc jupyter notebook list
## Style guide

We have configured a pre-commit hook that checks for `black`-compliant code style.
We encourage you to configure the pre-commit hook as described in the [PyMC Python Code Style Wiki Page](https://github.com/pymc-devs/pymc/wiki/PyMC-Python-Code-Style), because it will automatically enforce the code style on your commits.
We encourage you to configure the pre-commit hook as described in the [PyMC Python Code Style Wiki Page](https://github.com/pymc-devs/pymc/wiki/Python-Code-Style), because it will automatically enforce the code style on your commits.

Similarly, consult the [PyMC's Jupyter Notebook Style](https://github.com/pymc-devs/pymc/wiki/PyMC-Jupyter-Notebook-Style-Guide) guides for notebooks.

Expand Down
15 changes: 14 additions & 1 deletion pymc/distributions/multivariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@
from pymc.distributions.continuous import ChiSquared, Normal, assert_negative_support
from pymc.distributions.dist_math import bound, factln, logpow, multigammaln
from pymc.distributions.distribution import Continuous, Discrete
from pymc.distributions.shape_utils import broadcast_dist_samples_to, to_tuple
from pymc.distributions.shape_utils import (
broadcast_dist_samples_to,
rv_size_is_none,
to_tuple,
)
from pymc.math import kron_diag, kron_dot

__all__ = [
Expand Down Expand Up @@ -405,6 +409,15 @@ def dist(cls, a, **kwargs):

return super().dist([a], **kwargs)

def get_moment(rv, size, a):
norm_constant = at.sum(a, axis=-1)[..., None]
moment = a / norm_constant
if not rv_size_is_none(size):
if isinstance(size, int):
Copy link
Member

@ricardoV94 ricardoV94 Nov 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we may need to revisit this. When size is a number it is still inside a TensorConstant. Not sure what isinstance(size, int) will do. Maybe we should check for size.ndim?

Size may also be a symbolic shape. So the same reasoning applies.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great Larry! Thanks a lot

As always, thanks to you for all your help.

Should we change isinstance(size, int) to if isinstance(size, int) or size.ndim > 0? Would that be a correct approach?

Copy link
Member

@ricardoV94 ricardoV94 Nov 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked again and I think that checks will never evaluate true. Even when size is an integer, it gets converted to a TensorConstant([size]):

size = x.owner.inputs[1]
isinstance(size, int)  # False
size.data  # array([2])

If I remove the isinstance block, all tests still pass

size = (size,)
moment = at.full((*size, *a.shape), moment)
return moment

def logp(value, a):
"""
Calculate log-probability of Dirichlet distribution
Expand Down
38 changes: 38 additions & 0 deletions pymc/tests/test_distributions_moments.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Cauchy,
ChiSquared,
Constant,
Dirichlet,
DiscreteUniform,
ExGaussian,
Exponential,
Expand Down Expand Up @@ -611,4 +612,41 @@ def test_hyper_geometric_moment(N, k, n, size, expected):
def test_discrete_uniform_moment(lower, upper, size, expected):
with Model() as model:
DiscreteUniform("x", lower=lower, upper=upper, size=size)


@pytest.mark.parametrize(
"a, size, expected",
[
(
np.array([2, 3, 5, 7, 11]),
None,
np.array([2, 3, 5, 7, 11]) / 28,
),
(
np.array([[1, 2, 3], [5, 6, 7]]),
None,
np.array([[1, 2, 3], [5, 6, 7]]) / np.array([6, 18])[..., np.newaxis],
),
(
np.array([[1, 2, 3], [5, 6, 7]]),
7,
np.apply_along_axis(
lambda x: np.divide(x, np.array([6, 18])),
1,
np.broadcast_to([[1, 2, 3], [5, 6, 7]], shape=[7, 2, 3]),
),
),
(
np.full(shape=np.array([7, 3]), fill_value=np.array([13, 17, 19])),
(
11,
5,
),
np.broadcast_to([13, 17, 19], shape=[11, 5, 7, 3]) / 49,
),
],
)
def test_dirichlet_moment(a, size, expected):
with Model() as model:
Dirichlet("x", a=a, size=size)
assert_moment_is_expected(model, expected)