# Add x offset to poisson mixture model?

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:

``````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:

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? ) 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 !

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

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?

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

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

``````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:

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.

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?

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.

1 Like