How to debug Bad initial energy: nan?


I have a (complicated) Poisson model that perfectly works when I don’t have an offset variable (e.g. y=exp(a+Bx)). On the contrary, when I specify an offset (y = exp(a+Bx+logE)) this is returned (at around 3% of the NUTS using jitter+adapt_diag process):

ValueError: Bad initial energy: nan. The model might be misspecified.

How can I debug it? I tried to print the test_points but I don’t see anything strange


Have you try the solution in FAQ?

Trying to diagnose a `-inf` initial energy error: theano problem

yes and I didn’t find anything strange. BUT sampling with init=‘advi+adapt_diag’ instead of the default one, seems to solve the problem. What kind of problem might have I encountered?


In that case there must be some areas in your posterior is undefined and sometimes a large step size take the parameters outside of the supported domain. You can try to use a more informative prior to cut out those part of your parameter space.


Another explanation might be that some parameters don’t change at all, maybe because a gradient is zero. In that case, the mass matrix adaptation will try to use a variance of 0 at some point, which can lead to a nan. We should probably print a better error message in cases like that though.


I try to use more informative priors. I keep you updated


It happens only when I put the offset (AKA exposure) in the model. It is a list of scalars.