# NUTS and zero derivative error

**URL:** https://discourse.pymc.io/t/nuts-and-zero-derivative-error/1731
**Category:** Questions
**Created:** [August 14, 2018, 6:51pm UTC](https://discourse.pymc.io/t/nuts-and-zero-derivative-error/1731 "2018-08-14T18:51:45Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![tbalo0](https://avatars.discourse-cdn.com/v4/letter/t/da6949/32.png) [@tbalo0](https://discourse.pymc.io/u/tbalo0)
#### Post date: [August 14, 2018, 6:51pm UTC](https://discourse.pymc.io/t/nuts-and-zero-derivative-error/1731/1 "2018-08-14T18:51:45Z")

</div>

When sampling using NUTS I keep getting this ValueError: Mass matrix contains zeros on the diagonal. Removing the jitter in the initialization does not eliminate this error. I tried what was recommended here: [Limit or prevent unrealistic output of neural network](https://discourse.pymc.io/t/limit-or-prevent-unrealistic-output-of-neural-network/871/3) but it didn’t work. What is the reason for this error and is there a way to fix it? Thank you so much!!

---

<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: [August 14, 2018, 6:55pm UTC](https://discourse.pymc.io/t/nuts-and-zero-derivative-error/1731/2 "2018-08-14T18:55:06Z")

</div>

One of the reason is some prior being too uninformative and the derivative is too small. What kind of model you are testing, and also what is the full error message?

---

<div class="post-metadata">

### Author: ![tbalo0](https://avatars.discourse-cdn.com/v4/letter/t/da6949/32.png) [@tbalo0](https://discourse.pymc.io/u/tbalo0)
#### Post date: [August 14, 2018, 10:24pm UTC](https://discourse.pymc.io/t/nuts-and-zero-derivative-error/1731/6 "2018-08-14T22:24:20Z")

</div>

Thanks so much for writing back! The full error message is:  
ValueError: Mass matrix contains zeros on the diagonal.

The derivative of RV `ind` .ravel()[0] is zero.  
The derivative of RV `flu` .ravel()[0] is zero.  
(on and on with all the indices where the derivative is zero).  
The model is a stochastic block model. NUTS takes four variables and two of them have these derivate equals zero errors.

Can the problem is ‘derivative being too small’ be solved by changing the numerical resolution of Python? The prior works on the subset of data, but when I increase the data used, it does not work? (The prior should not be very uninformative I thought from this)

---

<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: [August 15, 2018, 4:20am UTC](https://discourse.pymc.io/t/nuts-and-zero-derivative-error/1731/7 "2018-08-15T04:20:56Z")

</div>

It depends on your model, but in most cases this error is cause by your prior being too vague. You can try fixing the RV to a constant and narrow down the problem. Also you can search the discourse to have a look at related discussions.

---

<div class="post-metadata">

### Author: ![3arbie](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/3arbie/32/2145_2.png) [@3arbie](https://discourse.pymc.io/u/3arbie)
#### Post date: [August 18, 2019, 1:09am UTC](https://discourse.pymc.io/t/nuts-and-zero-derivative-error/1731/8 "2019-08-18T01:09:02Z")

</div>

```auto
with pm.Model() as GVmodel:
    mu = pm.Normal('mu', mu=0, sigma=.01)
    nu = pm.Normal('nu', (K/3 - 1)*dt, sigma= 0.1) # K:Kurtois of nu  
    gammadiff = pm.Gamma('gammadiff', alpha=dt/nu, beta=nu) 
    sigma = pm.HalfNormal('sigma', np.sqrt(V/dt)) # V: variance of the data
    theta = pm.Normal('theta', mu=S*sigma*np.sqrt(dt)/3*nu,sigma=1 )
    likelihood = pm.Normal('likelihood', mu = mu*dt + theta*gammadiff, sigma=(sigma**2)*gammadiff, observed= data.Return) #log_return data
    step = pm.HamiltonianMC()
    trace = pm.sample(step = step)

```

The data is from kaggle under maxi24/eurusd  
I have the same can someone help me please.

---

<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: [August 18, 2019, 7:25am UTC](https://discourse.pymc.io/t/nuts-and-zero-derivative-error/1731/9 "2019-08-18T07:25:20Z")

</div>

You shouldnt assign step method yourself unless you have special use case - try just `trace = pm.sample()`

---

<div class="post-metadata">

### Author: ![3arbie](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/3arbie/32/2145_2.png) [@3arbie](https://discourse.pymc.io/u/3arbie)
#### Post date: [August 18, 2019, 9:05am UTC](https://discourse.pymc.io/t/nuts-and-zero-derivative-error/1731/10 "2019-08-18T09:05:37Z")

</div>

I still have the same problem while using sample without step function.
