Skip to content

Commit

Permalink
Rename ZeroInflatedPoisson theta parameter to mu
Browse files Browse the repository at this point in the history
For consistency with the base Poisson distribution
  • Loading branch information
ricardoV94 committed Mar 14, 2022
1 parent e89b594 commit 7cc570a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ All of the above apply to:
- The function `replace_with_values` function has been added to `gp.utils`.
- `MarginalSparse` has been renamed `MarginalApprox`.
- Removed `MixtureSameFamily`. `Mixture` is now capable of handling batched multivariate components (see [#5438](https://github.com/pymc-devs/pymc/pull/5438)).
- `ZeroInflatedPoisson` `theta` parameter was renamed to `mu` (see [#5584](https://github.com/pymc-devs/pymc/pull/5584)).
- ...

### Expected breaks
Expand Down
28 changes: 14 additions & 14 deletions pymc/distributions/discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -1414,8 +1414,8 @@ class ZeroInflatedPoisson:
.. math::
f(x \mid \psi, \theta) = \left\{ \begin{array}{l}
(1-\psi) + \psi e^{-\theta}, \text{if } x = 0 \\
f(x \mid \psi, \mu) = \left\{ \begin{array}{l}
(1-\psi) + \psi e^{-\mu}, \text{if } x = 0 \\
\psi \frac{e^{-\theta}\theta^x}{x!}, \text{if } x=1,2,3,\ldots
\end{array} \right.
Expand All @@ -1428,42 +1428,42 @@ class ZeroInflatedPoisson:
plt.style.use('arviz-darkgrid')
x = np.arange(0, 22)
psis = [0.7, 0.4]
thetas = [8, 4]
for psi, theta in zip(psis, thetas):
pmf = st.poisson.pmf(x, theta)
mus = [8, 4]
for psi, mu in zip(psis, mus):
pmf = st.poisson.pmf(x, mu)
pmf[0] = (1 - psi) + pmf[0]
pmf[1:] = psi * pmf[1:]
pmf /= pmf.sum()
plt.plot(x, pmf, '-o', label='$\\psi$ = {}, $\\theta$ = {}'.format(psi, theta))
plt.plot(x, pmf, '-o', label='$\\psi$ = {}, $\\mu$ = {}'.format(psi, mu))
plt.xlabel('x', fontsize=12)
plt.ylabel('f(x)', fontsize=12)
plt.legend(loc=1)
plt.show()
======== ==========================
Support :math:`x \in \mathbb{N}_0`
Mean :math:`\psi\theta`
Variance :math:`\theta + \frac{1-\psi}{\psi}\theta^2`
Mean :math:`\psi\mu`
Variance :math:`\mu + \frac{1-\psi}{\psi}\mu^2`
======== ==========================
Parameters
----------
psi: float
Expected proportion of Poisson variates (0 < psi < 1)
theta: float
mu: float
Expected number of occurrences during the given interval
(theta >= 0).
(mu >= 0).
"""

def __new__(cls, name, psi, theta, **kwargs):
def __new__(cls, name, psi, mu, **kwargs):
return _zero_inflated_mixture(
name=name, nonzero_p=psi, nonzero_dist=Poisson.dist(mu=theta), **kwargs
name=name, nonzero_p=psi, nonzero_dist=Poisson.dist(mu=mu), **kwargs
)

@classmethod
def dist(cls, psi, theta, **kwargs):
def dist(cls, psi, mu, **kwargs):
return _zero_inflated_mixture(
name=None, nonzero_p=psi, nonzero_dist=Poisson.dist(mu=theta), **kwargs
name=None, nonzero_p=psi, nonzero_dist=Poisson.dist(mu=mu), **kwargs
)


Expand Down
16 changes: 8 additions & 8 deletions pymc/tests/test_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1747,33 +1747,33 @@ def test_constantdist(self):
self.check_logcdf(Constant, I, {"c": I}, lambda value, c: np.log(value >= c))

def test_zeroinflatedpoisson(self):
def logp_fn(value, psi, theta):
def logp_fn(value, psi, mu):
if value == 0:
return np.log((1 - psi) * sp.poisson.pmf(0, theta))
return np.log((1 - psi) * sp.poisson.pmf(0, mu))
else:
return np.log(psi * sp.poisson.pmf(value, theta))
return np.log(psi * sp.poisson.pmf(value, mu))

def logcdf_fn(value, psi, theta):
return np.log((1 - psi) + psi * sp.poisson.cdf(value, theta))
def logcdf_fn(value, psi, mu):
return np.log((1 - psi) + psi * sp.poisson.cdf(value, mu))

self.check_logp(
ZeroInflatedPoisson,
Nat,
{"psi": Unit, "theta": Rplus},
{"psi": Unit, "mu": Rplus},
logp_fn,
)

self.check_logcdf(
ZeroInflatedPoisson,
Nat,
{"psi": Unit, "theta": Rplus},
{"psi": Unit, "mu": Rplus},
logcdf_fn,
)

self.check_selfconsistency_discrete_logcdf(
ZeroInflatedPoisson,
Nat,
{"theta": Rplus, "psi": Unit},
{"mu": Rplus, "psi": Unit},
)

def test_zeroinflatednegativebinomial(self):
Expand Down
6 changes: 3 additions & 3 deletions pymc/tests/test_distributions_moments.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,17 +624,17 @@ def test_constant_moment(c, size, expected):


@pytest.mark.parametrize(
"psi, theta, size, expected",
"psi, mu, size, expected",
[
(0.9, 3.0, None, 3),
(0.8, 2.9, 5, np.full(5, 2)),
(0.2, np.arange(1, 5) * 5, None, np.arange(1, 5)),
(0.2, np.arange(1, 5) * 5, (2, 4), np.full((2, 4), np.arange(1, 5))),
],
)
def test_zero_inflated_poisson_moment(psi, theta, size, expected):
def test_zero_inflated_poisson_moment(psi, mu, size, expected):
with Model() as model:
ZeroInflatedPoisson("x", psi=psi, theta=theta, size=size)
ZeroInflatedPoisson("x", psi=psi, mu=mu, size=size)
assert_moment_is_expected(model, expected)


Expand Down
6 changes: 3 additions & 3 deletions pymc/tests/test_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1128,11 +1128,11 @@ def test_shape_edgecase(self):

def test_zeroinflatedpoisson(self):
with pm.Model():
theta = pm.Beta("theta", alpha=1, beta=1)
mu = pm.Beta("mu", alpha=1, beta=1)
psi = pm.HalfNormal("psi", sd=1)
pm.ZeroInflatedPoisson("suppliers", psi=psi, theta=theta, size=20)
pm.ZeroInflatedPoisson("suppliers", psi=psi, mu=mu, size=20)
gen_data = pm.sample_prior_predictive(samples=5000)
assert gen_data.prior["theta"].shape == (1, 5000)
assert gen_data.prior["mu"].shape == (1, 5000)
assert gen_data.prior["psi"].shape == (1, 5000)
assert gen_data.prior["suppliers"].shape == (1, 5000, 20)

Expand Down

0 comments on commit 7cc570a

Please sign in to comment.