Chain 1 failed due to Mass matrix contains zeros on the diagonal

I ran into below error

Traceback (most recent call last):
File “C:\Users\chigozie.boniface_in\anaconda3\envs\downgrade\lib\site-packages\pymc3\”, line 137, in run
File “C:\Users\chigozie.boniface_in\anaconda3\envs\downgrade\lib\site-packages\pymc3\”, line 191, in _start_loop
point, stats = self._compute_point()
File “C:\Users\chigozie.boniface_in\anaconda3\envs\downgrade\lib\site-packages\pymc3\”, line 216, in _compute_point
point, stats = self._step_method.step(self._point)
File “C:\Users\chigozie.boniface_in\anaconda3\envs\downgrade\lib\site-packages\pymc3\step_methods\”, line 276, in step
apoint, stats = self.astep(array)
File “C:\Users\chigozie.boniface_in\anaconda3\envs\downgrade\lib\site-packages\pymc3\step_methods\hmc\”, line 147, in astep
File “C:\Users\chigozie.boniface_in\anaconda3\envs\downgrade\lib\site-packages\pymc3\step_methods\hmc\”, line 272, in raise_ok
raise ValueError("\n".join(errmsg))
ValueError: Mass matrix contains zeros on the diagonal
The derivative of RV base_log__.ravel()[0] is zero.
The derivative of RV sat_paid_search_s_log__.ravel()[0] is zero.
The derivative of RV coef_paid_search_s_log__.ravel()[0] is zero.
The derivative of RV car_radio_s_logodds__.ravel()[0] is zero.
The derivative of RV sat_radio_s_log__.ravel()[0] is zero.
The derivative of RV coef_radio_s_log__.ravel()[0] is zero.
The derivative of RV car_tv_s_logodds__.ravel()[0] is zero.
The derivative of RV sat_tv_s_log__.ravel()[0] is zero.
The derivative of RV coef_tv_s_log__.ravel()[0] is zero.
while running

with pm.Model() as mmm:
channel_contributions = []

for channel in X.columns:
	coef = pm.Exponential(f'coef_{channel}', lam=0.0001) # regression coefficient
	sat = pm.Exponential(f'sat_{channel}', lam=1) # saturation strength
	car = pm.Beta(f'car_{channel}', alpha=2, beta=2) # carryover strength

	channel_data = X[channel].values
	channel_contribution = pm.Deterministic(f'contribution_{channel}',
											coef*saturate(carryover(channel_data, car), sat))

base = pm.Exponential('base', lam=0.0001) # baseline
noise = pm.Exponential('noise', lam=0.0001) # noise

sales = pm.Normal('revenue', mu=sum(channel_contributions) + base, sigma=noise, observed=y.values)
trace = pm.sample(return_inferencedata=True, tune=3000)

Kingly assist. I have gone through every suggestion regarding this error but couldn’t solve it.


How far into sampling did you get before this error occurred? If it occurred before the first sample, you might try:

trace = pm.sample(init='adapt_diag',

The jittering in default initialization (‘jitter+adapt_diag’) can throw errors when dealing with very small values (e.g., lam=0.0001).

1 Like

But I replaced init=‘adapt_diag’ with init=‘map’ and it work but cann’t really differentiate the two.

From the documentation:

map: Use the MAP as starting point. This is discouraged.