Skip to content

Add Constant Moment #5156

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 6 commits into from
Nov 8, 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
5 changes: 5 additions & 0 deletions pymc/distributions/discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,11 @@ def dist(cls, c, *args, **kwargs):
c = at.as_tensor_variable(floatX(c))
return super().dist([c], **kwargs)

def get_moment(rv, size, c):
if not rv_size_is_none(size):
c = at.full(size, c)
return c

def logp(value, c):
r"""
Calculate log-probability of Constant distribution at specified value.
Expand Down
15 changes: 15 additions & 0 deletions pymc/tests/test_distributions_moments.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Binomial,
Cauchy,
ChiSquared,
Constant,
Exponential,
Gamma,
HalfCauchy,
Expand Down Expand Up @@ -382,3 +383,17 @@ def test_poisson_moment(mu, size, expected):
with Model() as model:
Poisson("x", mu=mu, size=size)
assert_moment_is_expected(model, expected)


@pytest.mark.parametrize(
"c, size, expected",
[
(1, None, 1),
(1, 5, np.full(5, 1)),
(np.arange(1, 6), None, np.arange(1, 6)),
],
)
def test_constant_moment(c, size, expected):
with Model() as model:
Constant("x", c=c, size=size)
assert_moment_is_expected(model, expected)