# Model specification problem?

**URL:** <https://discourse.pymc.io/t/model-specification-problem/804>\
**Category:** Questions\
**Created:** [January 31, 2018, 3:58pm UTC](https://discourse.pymc.io/t/model-specification-problem/804 "2018-01-31T15:58:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![drbenvincent](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/drbenvincent/32/4313_2.png) [@drbenvincent](https://discourse.pymc.io/u/drbenvincent)\
**Post date:** [January 31, 2018, 3:58pm UTC](https://discourse.pymc.io/t/model-specification-problem/804/1 "2018-01-31T15:58:06Z")

</div>

So I’ve had some good luck implementing a bunch of models. But there’s one that I’m having trouble getting to work (ie sample). It dies before sampling with a very long bit cryptic error message. One salient point is: `ValueError: Bad initial energy: inf. The model might be misspecified.` I’m fairly sure it’s just something basic wrong with the way how I’ve written the model.

```python
with pm.Model() as model:
    # Priors
    β = pm.Normal('beta', mu=0, sd=10, shape=4)
    α = pm.Exponential('alpha', lam=1)
    ϵ = 0.01
            
    D = data['B']-data['A']
    R = (data['B']-data['A'])/data['A']
    T = data['DB']-data['DA']
    I = ((data['B']-data['A'])**(1./(data['DB']-data['DA'])))-1.
    diff = β[0] + β[0]*D + β[1]*R + β[2]*I + β[3]*T
    
    # Choice function: psychometric
    P_chooseB = pm.Deterministic('P_chooseB', choice_func_psychometric2(α, ϵ, diff))
    # Likelihood of observations
    r_likelihood = pm.Bernoulli('r_likelihood', p=P_chooseB, observed=data['R'])

```

I have a very similar one (below) working fine, so I’m being a bit blind to what it might be.

```python
    with pm.Model() as model:
        # Priors
        β = pm.Normal('beta', mu=0, sd=1, shape=5)
        α = pm.Exponential('alpha', lam=1)
        ϵ = 0.01
                    
        A = data['B']-data['A']
        B = (data['B']-data['A'])/((data['B']+data['A'])/2)
        C = data['DB']-data['DA']
        D = (data['DB']-data['DA'])/((data['DB']+data['DA'])/2) 
        diff = β[0] + β[1]*A + β[2]*B + β[3]*C + β[4]*D
                    
        # Choice function: psychometric
        P_chooseB = pm.Deterministic('P_chooseB', choice_func_psychometric2(α, ϵ, diff))
        # Likelihood of observations
        r_likelihood = pm.Bernoulli('r_likelihood', p=P_chooseB, observed=data['R'])

```

Any thoughts?

---

<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, 2018, 4:46pm UTC](https://discourse.pymc.io/t/model-specification-problem/804/2 "2018-01-31T16:46:52Z")

</div>

Have you check out the diagnostic mentioned here:

> [@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.)

Most likely I would say is the Deterministic node `P_chooseB` being outside of [0, 1] (the support of `p` in Bernoulli)

---

<div class="post-metadata">

**Author:** ![drbenvincent](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/drbenvincent/32/4313_2.png) [@drbenvincent](https://discourse.pymc.io/u/drbenvincent)\
**Post date:** [February 2, 2018, 9:35am UTC](https://discourse.pymc.io/t/model-specification-problem/804/3 "2018-02-02T09:35:49Z")

</div>

Ah! The problem is that `data['B']-data['A']` is sometimes negative, so the power in

```
I = ((data['B']-data['A'])**(1./(data['DB']-data['DA'])))-1.

```

causes `I` to be a complex number when `data['B']-data['A']<0`
