# Zero-Inflated Poisson Example

**URL:** <https://discourse.pymc.io/t/zero-inflated-poisson-example/3862>\
**Category:** Questions\
**Created:** [September 16, 2019, 5:27pm UTC](https://discourse.pymc.io/t/zero-inflated-poisson-example/3862 "2019-09-16T17:27:15Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![amg119](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@amg119](https://discourse.pymc.io/u/amg119)\
**Post date:** [September 16, 2019, 5:27pm UTC](https://discourse.pymc.io/t/zero-inflated-poisson-example/3862/1 "2019-09-16T17:27:15Z")

</div>

Hi All,

I was going through Bill Engels zero-inflated Poisson Likelihood GP example here:

[https://bwengals.github.io/gps-with-non-normal-likelihoods-in-pymc3.html](https://bwengals.github.io/gps-with-non-normal-likelihoods-in-pymc3.html)

I see the following code:

```auto
with pm.Model() as model:
    ℓ = pm.Gamma("ℓ", alpha=2, beta=2)
    cov = pm.gp.cov.ExpQuad(1, ℓ)

    c = pm.Gamma("c", alpha=2, beta=1)
    mu = pm.gp.mean.Constant(c)

    f = pm.gp.GP("f", X=x[:,None], mean_func=mu, cov_func=cov)

    p = pm.Beta("p", alpha=1, beta=1)
    likelihood = pm.ZeroInflatedPoisson("likelihood", psi=p, theta=tt.exp(f), observed=y)

    tr = pm.sample(2000)

```

Forgive me if this is obvious, but `pm.gp.GP` doesn’t seem to be anything that exists in the PyMC3 version I just installed. What is the correct format expected here?

I presume you specify a gp with `gp = pm.gp.Latent(mean_func=mu, cov_func=cov)` and then use `f = gp.prior("f", X=x[:,None])`. Is this correct? Thanks in advance!

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [September 17, 2019, 8:32am UTC](https://discourse.pymc.io/t/zero-inflated-poisson-example/3862/2 "2019-09-17T08:32:46Z")

</div>

I think you should use gp.Latent for that, see [https://docs.pymc.io/notebooks/GP-Latent.html](https://docs.pymc.io/notebooks/GP-Latent.html) for more information.

Specifically, you replace `f = pm.gp.GP("f", X=x[:,None], mean_func=mu, cov_func=cov)` with:

```auto
    gp = pm.gp.Latent(mean_func=mu, cov_func=cov)
    f = gp.prior("f", X=x[:,None])

```

cc @bwengals

---

<div class="post-metadata">

**Author:** ![amg119](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@amg119](https://discourse.pymc.io/u/amg119)\
**Post date:** [September 24, 2019, 4:38pm UTC](https://discourse.pymc.io/t/zero-inflated-poisson-example/3862/3 "2019-09-24T16:38:40Z")

</div>

This indeed solved it.

On a related note, is it possible to design your own likelihood? I’d like to add Zero-Inflated Negative Binomial (ZINB)… models the same kind of thing as ZIP but the mean is not constrained to be the same as the variance as in Poisson likelihoods.

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [September 24, 2019, 7:54pm UTC](https://discourse.pymc.io/t/zero-inflated-poisson-example/3862/4 "2019-09-24T19:54:28Z")

</div>

Yes, custom log likelihood could be implemented using pm.DensityDist

---

<div class="post-metadata">

**Author:** ![Gon\_F](https://avatars.discourse-cdn.com/v4/letter/g/f14d63/32.png) [@Gon\_F](https://discourse.pymc.io/u/Gon_F)\
**Post date:** [September 24, 2019, 9:34pm UTC](https://discourse.pymc.io/t/zero-inflated-poisson-example/3862/5 "2019-09-24T21:34:15Z")

</div>

For the Zero-inflated Poisson model itself, you can see a good example of someone having coded it manually himself in this thread [here](https://discourse.pymc.io/t/zero-inflated-poisson-log-lik/2664), amg119.

---

<div class="post-metadata">

**Author:** ![bwengals](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/bwengals/32/1237_2.png) [@bwengals](https://discourse.pymc.io/u/bwengals)\
**Post date:** [October 8, 2019, 7:28pm UTC](https://discourse.pymc.io/t/zero-inflated-poisson-example/3862/6 "2019-10-08T19:28:00Z")

</div>

I should probably take those posts down, or edit them. They used an older and very different syntax for PyMC3 GPs.

---

<div class="post-metadata">

**Author:** ![Gon\_F](https://avatars.discourse-cdn.com/v4/letter/g/f14d63/32.png) [@Gon\_F](https://discourse.pymc.io/u/Gon_F)\
**Post date:** [October 8, 2019, 8:13pm UTC](https://discourse.pymc.io/t/zero-inflated-poisson-example/3862/7 "2019-10-08T20:13:23Z")

</div>

I will throw in my absolutely pithy two cents here, and confirm that I have had to learn to look at the age of a PyMC3 notebook - relative to how complex the modelling example is - and know when to ignore their syntax if they’re too old.

Which is not to say I’m complaining here, since you guys take the time to write amazing notebooks with very intuitive statistical explanations that help so many people, I’m just confirming what bwengals mentioned above ^^; .

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [October 9, 2019, 7:57am UTC](https://discourse.pymc.io/t/zero-inflated-poisson-example/3862/8 "2019-10-09T07:57:26Z")

</div>

We love breaking user’s code without giving warnings 😅

---

<div class="post-metadata">

**Author:** ![Gon\_F](https://avatars.discourse-cdn.com/v4/letter/g/f14d63/32.png) [@Gon\_F](https://discourse.pymc.io/u/Gon_F)\
**Post date:** [October 10, 2019, 12:19am UTC](https://discourse.pymc.io/t/zero-inflated-poisson-example/3862/9 "2019-10-10T00:19:58Z")

</div>

It’s better for things to change in the first place, rather than otherwise like in many complex projects that span years, and people are afraid of breaking backward compatibility 😉 .

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [October 10, 2019, 8:09am UTC](https://discourse.pymc.io/t/zero-inflated-poisson-example/3862/10 "2019-10-10T08:09:57Z")

</div>

We do promised that the changes are meaningful and reflect the current best practice. In a way this is to push downstream user to change their code accordingly. 😄

---

<div class="post-metadata">

**Author:** ![bwengals](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/bwengals/32/1237_2.png) [@bwengals](https://discourse.pymc.io/u/bwengals)\
**Post date:** [October 28, 2019, 2:54pm UTC](https://discourse.pymc.io/t/zero-inflated-poisson-example/3862/11 "2019-10-28T14:54:13Z")

</div>

To be fair to PyMC3 devs though, that code was from my personal blog which has absolutely zero guarantees of being correct. So I’m solely the one to blame there… I don’t think the pm.gp.gp.GP syntax ever made it into a release.
