# How to debug Bad initial energy: nan?

**URL:** <https://discourse.pymc.io/t/how-to-debug-bad-initial-energy-nan/492>\
**Category:** Questions\
**Created:** [October 25, 2017, 2:52pm UTC](https://discourse.pymc.io/t/how-to-debug-bad-initial-energy-nan/492 "2017-10-25T14:52:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcodena](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/marcodena/32/41_2.png) [@marcodena](https://discourse.pymc.io/u/marcodena)\
**Post date:** [October 25, 2017, 2:52pm UTC](https://discourse.pymc.io/t/how-to-debug-bad-initial-energy-nan/492/1 "2017-10-25T14:52:21Z")

</div>

Hello,  
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

---

<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 25, 2017, 3:11pm UTC](https://discourse.pymc.io/t/how-to-debug-bad-initial-energy-nan/492/2 "2017-10-25T15:11:21Z")

</div>

Have you try the solution in FAQ?

> [@Frequently Asked Questions](https://discourse.pymc.io/t/frequently-asked-questions/74/5):
>
> Getting NaN or Inf during NUTS or VI (e.g., 'Bad initial energy: inf. The model might be misspecified., NaN occurred in optimization.)

> [@Getting 'Bad initial energy: inf' when trying to sample simple model](https://discourse.pymc.io/t/getting-bad-initial-energy-inf-when-trying-to-sample-simple-model/364/2):
>
> Hi @Stargrazer82301, this is actually one of our FAQs. You can have a look at the answer here: For example, in your case, running: for RV in model.basic\_RVs: print(RV.name, RV.logp(model.test\_point)) gives: Intercept 1.1370228148748596 Gradient -inf Psi\_log\_\_ -0.7257913526447277 z\_likelihood 73.49976264100106 And looking closer at gradient: gradient = pm.Normal('Gradient', mu=leastsq\_fit[0], sd=leastsq\_fit[0], testval=leastsq\_fit[0]) You can see that sd is defined as leastsq\_fit[0], …

---

<div class="post-metadata">

**Author:** ![marcodena](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/marcodena/32/41_2.png) [@marcodena](https://discourse.pymc.io/u/marcodena)\
**Post date:** [October 25, 2017, 6:11pm UTC](https://discourse.pymc.io/t/how-to-debug-bad-initial-energy-nan/492/3 "2017-10-25T18:11:27Z")

</div>

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?

---

<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 25, 2017, 6:39pm UTC](https://discourse.pymc.io/t/how-to-debug-bad-initial-energy-nan/492/4 "2017-10-25T18:39:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aseyboldt](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aseyboldt/32/5795_2.png) [@aseyboldt](https://discourse.pymc.io/u/aseyboldt)\
**Post date:** [October 25, 2017, 6:45pm UTC](https://discourse.pymc.io/t/how-to-debug-bad-initial-energy-nan/492/5 "2017-10-25T18:45:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![marcodena](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/marcodena/32/41_2.png) [@marcodena](https://discourse.pymc.io/u/marcodena)\
**Post date:** [October 25, 2017, 8:08pm UTC](https://discourse.pymc.io/t/how-to-debug-bad-initial-energy-nan/492/6 "2017-10-25T20:08:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![marcodena](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/marcodena/32/41_2.png) [@marcodena](https://discourse.pymc.io/u/marcodena)\
**Post date:** [October 25, 2017, 8:16pm UTC](https://discourse.pymc.io/t/how-to-debug-bad-initial-energy-nan/492/7 "2017-10-25T20:16:10Z")

</div>

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