Skip to content
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

Zero inflated binomial #2251

Merged
merged 12 commits into from
Jun 2, 2017
Merged

Zero inflated binomial #2251

merged 12 commits into from
Jun 2, 2017

Conversation

fonnesbeck
Copy link
Member

Adds zero-inflated binomial distribution, following the other zero-inflated distributions in discrete.py. Will eventually be superseded by #2246 but that PR is not yet working. This will make the main zero-inflated distributions available for 3.1.

p : float
Probability of success in each trial (0 < p < 1).
psi : float
Expected proportion of Poisson variates (0 < psi < 1)
Copy link
Member

Choose a reason for hiding this comment

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

Poisson -> Binomial

g = generate_samples(stats.binom.rvs, n, p,
dist_shape=self.shape,
size=size)
sampled = g * (np.random.random(np.squeeze(g.shape)) < psi)
Copy link
Member

Choose a reason for hiding this comment

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

What is the squeeze about? Doesn't this break broadcasting with g?

Copy link
Member Author

Choose a reason for hiding this comment

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

Doesn't appear to. I'm just mirroring what is happening in the other ZI dists.

Copy link
Member Author

Choose a reason for hiding this comment

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

It passes the test_distribution_random test.

Copy link
Member

Choose a reason for hiding this comment

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

ok then

def logp(self, value):
return tt.switch(value > 0,
tt.log(self.psi) + self.bin.logp(value),
tt.log((1. - self.psi) + self.psi * tt.pow(1 - self.p, self.n)))
Copy link
Member

Choose a reason for hiding this comment

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

Does this work numerically for the gradient? Maybe a test that this is reasonable for corner cases for p and psi and large n would be nice.

Copy link
Member Author

Choose a reason for hiding this comment

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

Isnt that what the tests do?

Copy link
Member

Choose a reason for hiding this comment

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

I think this work for the gradient but is numerically unstable. Why not use the log-sum-exp trick as in pm.Mixture? https://github.com/pymc-devs/pymc3/blob/master/pymc3/distributions/mixture.py#L110-L115

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, right. I was just following the implementations of the other ZI distributions, but they should be "robustified", yes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Should be good to go now

n = self.n

logp_val = tt.switch(value > 0,
logsumexp(tt.log(psi) + self.bin.logp(value)),
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this still be just tt.log(self.psi) + self.bin.logp(value)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Doesn't logsumexp help here?

Copy link
Member

Choose a reason for hiding this comment

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

The exp is missing :-)
It helps if you have an expression like log(exp(a) + exp(b)). But we are only doing log(a + b) here.


logp_val = tt.switch(value > 0,
logsumexp(tt.log(psi) + self.bin.logp(value)),
logsumexp(tt.log((1. - psi) + psi * tt.pow(1 - p, n))))
Copy link
Member

Choose a reason for hiding this comment

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

I think this should be
logaddexp(tt.log1p(-psi), tt.log(psi) + n * tt.log1p(-p))
where

def logaddexp(a, b):
    diff = b - a
    return tt.switch(diff > 0, b + tt.log1p(tt.exp(-diff)), a + tt.log1p(tt.exp(diff)))

Copy link
Member Author

Choose a reason for hiding this comment

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

You are correct.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, our logsumexp doesn't have the same signature, but you are right in principle.

Copy link
Member

Choose a reason for hiding this comment

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

I think we should just add the logaddexp function. This is nicer (and I think faster) if we have only two variables and comes in handy quite often. I'm surprised that I can't find this in theano already....

Copy link
Member Author

Choose a reason for hiding this comment

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

In fact, I think logsumexp is used incorrectly in Mixture, if I am reading it correctly.

Copy link
Member

Choose a reason for hiding this comment

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

logsumexp is getting a weight vector [psi, 1.-psi], maybe that's why?

"""

def __init__(self, theta, psi, *args, **kwargs):
def __init__(self, psi, theta, *args, **kwargs):
Copy link
Member

Choose a reason for hiding this comment

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

isn't this a backward incompatible change?

Copy link
Member Author

Choose a reason for hiding this comment

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

This harmonizes them with the convention in Mixture, which is where they will eventually end up. So, a break is coming in one place or the other.

Copy link
Member

Choose a reason for hiding this comment

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

Then we should definitely put that in the release notes. And maybe also print a warning until 3.2?

@junpenglao
Copy link
Member

Agree with @aseyboldt, the logp is off, for example:

x = np.concatenate([np.random.poisson(4, size=180), np.zeros(20)])
with pm.Model() as model0:
    ψ = pm.Beta('ψ', 1., 1.)
    θ = pm.Gamma('θ', 1., 1.)
    like = ZeroInflatedPoisson('like', psi=ψ, theta=θ, observed=x)
    tr0 = pm.sample(3000, init=None, njobs=2)
pm.traceplot(tr0);

gives:
image

@fonnesbeck
Copy link
Member Author

OK, getting something more reasonable now:

unknown

@aseyboldt
Copy link
Member

LGTM

@junpenglao
Copy link
Member

Tested locally, works perfect (even the case i showed you few days ago in a nb @fonnesbeck)

@fonnesbeck fonnesbeck merged commit 0974ecc into master Jun 2, 2017
@fonnesbeck fonnesbeck deleted the zero_inflated_binomial branch June 2, 2017 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants