Skip to content

WIP Implement ZeroInflatedPoisson as a subclass of Mixture #1459

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

Closed
wants to merge 2 commits into from
Closed
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
25 changes: 25 additions & 0 deletions pymc3/distributions/mixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from .distribution import Discrete, Distribution, draw_values, generate_samples
from .continuous import get_tau_sd, Normal

__all__ = ['Mixture', 'NormalMixture']


def all_discrete(comp_dists):
"""
Expand Down Expand Up @@ -167,3 +169,26 @@ def __init__(self, w, mu, *args, **kwargs):

super(NormalMixture, self).__init__(w, Normal.dist(mu, sd=sd),
*args, **kwargs)


class Zero(Discrete):
def __init__(self, *args, **kwargs):
super(Zero, self).__init__(*args, **kwargs)

def logp(self, value):
return tt.switch(tt.eq(value, 0), 0., -np.inf)

def random(self, point=None, size=None, repeat=None):
def _random(dtype=self.dtype, size=None):
return np.full(size, fill_value=0, dtype=dtype)

return generate_samples(_random, dist_shape=self.shape,
size=size).astype(self.dtype)


class ZeroInflatedPoisson(Mixture):
def __init__(self, theta, psi, *args, **kwargs):
w = tt.stack([psi, 1 - psi])
comp_dists = [Zero.dist(), Poisson.dist(theta)]

super(ZeroInflatedPoisson, self).__init__(w, comp_dists, *args, **kwargs)