# Add x offset to poisson mixture model?

**URL:** <https://discourse.pymc.io/t/add-x-offset-to-poisson-mixture-model/2625>\
**Category:** Questions\
**Created:** [January 31, 2019, 8:29pm UTC](https://discourse.pymc.io/t/add-x-offset-to-poisson-mixture-model/2625 "2019-01-31T20:29:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![NatPRoach](https://avatars.discourse-cdn.com/v4/letter/n/b487fb/32.png) [@NatPRoach](https://discourse.pymc.io/u/NatPRoach)\
**Post date:** [January 31, 2019, 8:29pm UTC](https://discourse.pymc.io/t/add-x-offset-to-poisson-mixture-model/2625/1 "2019-01-31T20:29:09Z")

</div>

Hello,  
I am very new to pymc3, and I’m trying to produce a poisson mixture model  
The code I have so far is the following:

```auto
with pm.Model() as model:
    lam1 = pm.Exponential('lam1', lam=1)
    lam2 = pm.Exponential('lam2', lam=1)
    pois1 = pm.Poisson.dist(mu=lam1)
    pois2 = pm.Poisson.dist(mu=lam2)
    w = pm.Dirichlet('w',a=np.array([1.,1.]))
    like = pm.Mixture('like',w=w, comp_dists=[pois1,pois2],observed=new_stop_pos)
with model:
    trace = pm.sample(50000,n_init=10000,tune=10000)

```

I have two problems that I’m trying to solve. The first is that my model isn’t converging at all. When I produce a traceplot I see the following:

 ![traceplot](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/1/1c5f415d8b8c97243c6ac080552cf7e70992bd85.png)

The second is that I’m hoping to add x offsets to the model, such that instead of starting at 0 each of the poisson distributions are offset by some x value that we model. I suspect that these two problems might be related.  
I found the following post ( [How to specify offset in poisson regression?](https://discourse.pymc.io/t/how-to-specify-offset-in-poisson-regression/177) ) that seems to be looking at how to add an offset, but I’m a little confused about whats actually being done there, and I’m not sure it’s solving the same problem. I’d appreciate any feedback or help about getting the model to converge or adding an offset to the data. Thank you 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:** [January 31, 2019, 9:16pm UTC](https://discourse.pymc.io/t/add-x-offset-to-poisson-mixture-model/2625/2 "2019-01-31T21:16:38Z")

</div>

Poisson regression is not the same as a poisson mixture model - for adding a offset I think doing `pois1 = pm.Poisson.dist(mu=lam1+offset)` is enough

---

<div class="post-metadata">

**Author:** ![NatPRoach](https://avatars.discourse-cdn.com/v4/letter/n/b487fb/32.png) [@NatPRoach](https://discourse.pymc.io/u/NatPRoach)\
**Post date:** [January 31, 2019, 9:48pm UTC](https://discourse.pymc.io/t/add-x-offset-to-poisson-mixture-model/2625/3 "2019-01-31T21:48:34Z")

</div>

Thank you for your response. I’m afraid I don’t understand though. Won’t adding the offset directly to the mu of the Poisson distribution be equivalent to adding to the lambda of the Poisson distribution and not the same as offsetting by some x? or am I not understanding what the mu parameter in the Poisson.dist() function is doing?

---

<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:** [January 31, 2019, 10:22pm UTC](https://discourse.pymc.io/t/add-x-offset-to-poisson-mixture-model/2625/4 "2019-01-31T22:22:35Z")

</div>

> [@NatPRoach](#):
>
> Won’t adding the offset directly to the mu of the Poisson distribution be equivalent to adding to the lambda of the Poisson distribution and not the same as offsetting by some x?

Well, in a Poisson regression, the offset is added to your linear prediction which parameterized as `mu` and then transformed to the parameter for Poission (ie. lambda), so yes, they are some what equivalent. But again, here you have a mixture model without linear prediction. You can have a look at a model that is a mixture regression model: [Gaussian Mixture of regression - #2 by junpenglao](https://discourse.pymc.io/t/gaussian-mixture-of-regression/537/2)

---

<div class="post-metadata">

**Author:** ![NatPRoach](https://avatars.discourse-cdn.com/v4/letter/n/b487fb/32.png) [@NatPRoach](https://discourse.pymc.io/u/NatPRoach)\
**Post date:** [January 31, 2019, 11:22pm UTC](https://discourse.pymc.io/t/add-x-offset-to-poisson-mixture-model/2625/5 "2019-01-31T23:22:07Z")

</div>

Okay, I think I understand. I changed my code to the following:

```auto
with pm.Model() as model:
    lam1 = pm.Exponential('lam1', lam=1.)
    offset1 = pm.Categorical('offset1',p=[0.1 for x in range(max(new_stop_pos))])
    lam2 = pm.Exponential('lam2', lam=1.)
    offset2 = pm.Categorical('offset2',p=[0.1 for x in range(max(new_stop_pos))])
    pois1 = pm.Poisson.dist(mu=lam1+offset1)
    pois2 = pm.Poisson.dist(mu=lam2+offset2)
    w = pm.Dirichlet('w',a=np.array([1.,1.]))
    like = pm.Mixture('like',w=w,comp_dists=[pois1,pois2],observed=new_stop_pos)
with model:
    trace = pm.sample(50000,n_init=10000,tune=100000)

```

I’m still not seeing convergence though. The new traceplot looks like this:

 ![Figure_1](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/9/9e843e3ae9483d60c32de8bbd3cc407cafafcaa0.png)

Should I be doing something different in order to get better convergence?

---

<div class="post-metadata">

**Author:** ![NatPRoach](https://avatars.discourse-cdn.com/v4/letter/n/b487fb/32.png) [@NatPRoach](https://discourse.pymc.io/u/NatPRoach)\
**Post date:** [January 31, 2019, 11:51pm UTC](https://discourse.pymc.io/t/add-x-offset-to-poisson-mixture-model/2625/6 "2019-01-31T23:51:14Z")

</div>

> [@NatPRoach](#):
>
> Should I be doing something different in order to get better convergence?

I think I figured it out, the model wasn’t converging because the offsets were occasionally changing by one or two and the lambdas had to adjust when that happened. If I model once to get the approximate offsets, then model again with the offsets fixed, I get good convergence.

---

<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:** [February 1, 2019, 5:44am UTC](https://discourse.pymc.io/t/add-x-offset-to-poisson-mixture-model/2625/7 "2019-02-01T05:44:39Z")

</div>

1. You should change the pm.Categorical to a continuous variable (e.g., Uniform), it helps convergence.
2. Mixture model is difficult to sample, have a look at [How to improve fit of Poisson Mixture Model in PyMC3?](https://discourse.pymc.io/t/how-to-improve-fit-of-poisson-mixture-model-in-pymc3/1253/21)

---

<div class="post-metadata">

**Author:** ![NatPRoach](https://avatars.discourse-cdn.com/v4/letter/n/b487fb/32.png) [@NatPRoach](https://discourse.pymc.io/u/NatPRoach)\
**Post date:** [February 1, 2019, 3:45pm UTC](https://discourse.pymc.io/t/add-x-offset-to-poisson-mixture-model/2625/8 "2019-02-01T15:45:39Z")

</div>

Sounds good, I have switched to a Uniform distribution rather than a Categorical one for the offsets. Thank you so much you’ve been very helpful.
