Skip to content

n and p parametrization on Zero Inflated Negative Binomial #5212

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 5 commits into from
Nov 21, 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
19 changes: 16 additions & 3 deletions pymc/distributions/discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -1629,6 +1629,15 @@ def ZeroInfNegBinom(a, m, psi, x):
Var :math:`\psi\mu + \left (1 + \frac{\mu}{\alpha} + \frac{1-\psi}{\mu} \right)`
======== ==========================

The zero inflated negative binomial distribution can be parametrized
either in terms of mu or p, and either in terms of alpha or n.
The link between the parametrizations is given by

.. math::

\mu &= \frac{n(1-p)}{p} \\
\alpha &= n

Parameters
----------
psi: float
Expand All @@ -1637,15 +1646,18 @@ def ZeroInfNegBinom(a, m, psi, x):
Poission distribution parameter (mu > 0).
alpha: float
Gamma distribution parameter (alpha > 0).

p: float
Alternative probability of success in each trial (0 < p < 1).
n: float
Alternative number of target success trials (n > 0)
"""

rv_op = zero_inflated_neg_binomial

@classmethod
def dist(cls, psi, mu, alpha, *args, **kwargs):
def dist(cls, psi, mu=None, alpha=None, p=None, n=None, *args, **kwargs):
psi = at.as_tensor_variable(floatX(psi))
n, p = NegativeBinomial.get_n_p(mu=mu, alpha=alpha)
n, p = NegativeBinomial.get_n_p(mu=mu, alpha=alpha, p=p, n=n)
n = at.as_tensor_variable(floatX(n))
p = at.as_tensor_variable(floatX(p))
return super().dist([psi, n, p], *args, **kwargs)
Expand Down Expand Up @@ -1707,6 +1719,7 @@ def logcdf(value, psi, n, p):
0 <= value,
0 <= psi,
psi <= 1,
0 < n,
0 < p,
p <= 1,
)
Expand Down
16 changes: 16 additions & 0 deletions pymc/tests/test_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1790,13 +1790,29 @@ def logcdf_fn(value, psi, mu, alpha):
logp_fn,
)

self.check_logp(
ZeroInflatedNegativeBinomial,
Nat,
{"psi": Unit, "p": Unit, "n": NatSmall},
lambda value, psi, p, n: np.log((1 - psi) * sp.nbinom.pmf(0, n, p))
if value == 0
else np.log(psi * sp.nbinom.pmf(value, n, p)),
)

self.check_logcdf(
ZeroInflatedNegativeBinomial,
Nat,
{"psi": Unit, "mu": Rplusbig, "alpha": Rplusbig},
logcdf_fn,
)

self.check_logcdf(
ZeroInflatedNegativeBinomial,
Nat,
{"psi": Unit, "p": Unit, "n": NatSmall},
lambda value, psi, p, n: np.log((1 - psi) + psi * sp.nbinom.cdf(value, n, p)),
)

self.check_selfconsistency_discrete_logcdf(
ZeroInflatedNegativeBinomial,
Nat,
Expand Down
10 changes: 9 additions & 1 deletion pymc/tests/test_distributions_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -1467,7 +1467,7 @@ def seeded_zero_inflated_binomial_rng_fn(self):
]


class TestZeroInflatedNegativeBinomial(BaseTestDistribution):
class TestZeroInflatedNegativeBinomialMuSigma(BaseTestDistribution):
def zero_inflated_negbinomial_rng_fn(
self, size, psi, n, p, negbinomial_rng_fct, random_rng_fct
):
Expand Down Expand Up @@ -1502,6 +1502,14 @@ def seeded_zero_inflated_negbinomial_rng_fn(self):
]


class TestZeroInflatedNegativeBinomial(BaseTestDistribution):
Copy link
Member

Choose a reason for hiding this comment

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

This test can be much smaller. Check other tests for alternative parametrizations in this module. Like NormalTau

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After a quick glance on NormalTau, we only need to test on check_pymc_params_match_rv_op?

Copy link
Member

Choose a reason for hiding this comment

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

Yes

pymc_dist = pm.ZeroInflatedNegativeBinomial
pymc_dist_params = {"psi": 0.9, "n": 12, "p": 0.7}
expected_rv_op_params = {"psi": 0.9, "n": 12, "p": 0.7}
reference_dist_params = {"psi": 0.9, "n": 12, "p": 0.7}
tests_to_run = ["check_pymc_params_match_rv_op"]


class TestOrderedLogistic(BaseTestDistribution):
pymc_dist = _OrderedLogistic
pymc_dist_params = {"eta": 0, "cutpoints": np.array([-2, 0, 2])}
Expand Down