Skip to content

adds beta-binomial mean and test cases #5175

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 3 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
6 changes: 6 additions & 0 deletions pymc/distributions/discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ def dist(cls, alpha, beta, n, *args, **kwargs):
n = at.as_tensor_variable(intX(n))
return super().dist([n, alpha, beta], **kwargs)

def get_moment(rv, size, n, alpha, beta):
mean = at.round((n * alpha) / (alpha + beta))
if not rv_size_is_none(size):
mean = at.full(size, mean)
return mean

def logp(value, n, alpha, beta):
r"""
Calculate log-probability of BetaBinomial distribution at specified value.
Expand Down
16 changes: 16 additions & 0 deletions pymc/tests/test_distributions_moments.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pymc.distributions import (
Bernoulli,
Beta,
BetaBinomial,
Binomial,
Cauchy,
ChiSquared,
Expand Down Expand Up @@ -209,6 +210,21 @@ def test_beta_moment(alpha, beta, size, expected):
assert_moment_is_expected(model, expected)


@pytest.mark.parametrize(
"n, alpha, beta, size, expected",
[
(10, 1, 1, None, 5),
(10, 1, 1, 5, np.full(5, 5)),
(10, 1, np.arange(1, 6), None, np.round(10 / np.arange(2, 7))),
(10, 1, np.arange(1, 6), (2, 5), np.full((2, 5), np.round(10 / np.arange(2, 7)))),
],
)
def test_beta_binomial_moment(alpha, beta, n, size, expected):
with Model() as model:
BetaBinomial("x", alpha=alpha, beta=beta, n=n, size=size)
assert_moment_is_expected(model, expected)


@pytest.mark.parametrize(
"nu, size, expected",
[
Expand Down