Model with conditional parameters causing mass matrix to contain zeros on diagonal

It seems the disparity between my parameters B and H_{ult} was the second issue. For my synthetic data, I had B=1.127e-2 and H_{ult}=3.38e+5. If I change the units for H_{ult} to reduce the magnitude by three orders, I no longer get the “mass matrix contains zero on diagonal” error and the sampling process runs to the end. I think there is still room for improvement because the disparity is till quite high but at least it runs now. There is, however, divergences but i guess that should be a question for another thread. Here is the code that runs for anyone that is interested:

import numpy as npy
import pymc3 as pm
from theano import tensor as tt

npy.random.seed(12345)

rows = npy.array([2, 9, 37, 81])
tend = 21
dt = 0.25
tSeq = npy.arange(0, tend+dt, dt)
Nd = len(rows)

def logistic_(L, x0, t=tSeq, k=500):
    return L/(1+npy.exp(-k*(t-x0)))

""" Synthesise Data """
B_=1.172e-2; C_=1.6; D_=6.2; t2_=3.5; Hult_=3.38e2; err_sd=0.025
H_true = 0.5*Hult_*(1.-npy.exp(-B_*tSeq**C_))+logistic_(0.5, t2_)*(tSeq-t2_)/(tSeq-t2_+D_)
e = npy.random.randn(Nd)*err_sd
Hd = H_true[rows] * (npy.ones(Nd) + e)

def logistic(L, x0, t=tSeq, k=500):
    return L/(1+tt.exp(-k*(t-x0)))

""" Build model """
with pm.Model() as TempModel:
    B = pm.Normal('B', mu=B_, sd=B_, testval=B_)
    C = pm.Normal('C', mu=C_, sd=C_, testval=C_)
    D = pm.Normal('D', mu=D_, sd=D_, testval=D_)
    switch = pm.Beta('switch', alpha=1, beta=1, testval=0.1)
    t2 = switch*tend
    Hult = pm.Normal('Hult', mu=Hult_, sd=Hult_, testval=Hult_)

    factor = logistic(0.5, t2)
    Hn = 0.5*(Hult)*(1.-tt.exp(-(B)*tSeq**C)) + factor*(Hult)*(tSeq-t2)/(tSeq-t2+D)

    Hn_trim = Hn[rows]
    Hm = pm.Deterministic('Hm', Hn_trim)
    sigma = pm.HalfNormal('sigma', sd=1, shape=len(rows))
    H_obs = pm.Normal('H_obs', mu=Hm, sd=sigma, observed=Hd)

    trace = pm.sample(1000, tune=1000, cores=2, chains=2)
1 Like