Not sure if this is a bug.
However, I’ve been trying to sample a Negative Binomial model but constantly running into logp = -inf.
I have checked that there are no negative values anywhere, so not sure why this is happening.
Here is the code:
with pm.Model() as quantity_model_1:
alpha = pm.Uniform("alpha", lower=0, upper=10)
# Priors
sigma_a = pm.HalfCauchy('sigma_a', 1)
# Random intercepts
a = pm.HalfCauchy('a', beta=sigma_a, shape=no_stores)
b = pm.HalfCauchy('b', beta=1, shape=no_dow)
d = pm.Normal('d', mu=0.003, sd=0.001,testval = 0.05)
k = pm.Deterministic('k',var=b[dow]*pm.math.exp(d*temperature)) ##Note I specifically use exponential function to avoid any negative values.
y_hat = pm.Deterministic('y_hat',var=a[storeno]*k)
# Data likelihood
vol = pm.NegativeBinomial('vol', mu=y_hat, alpha=alpha, observed=total_quantity)
start = pm.find_MAP()
step = pm.NUTS()
# Inference
trace = pm.sample(3, start = start, step = step, cores =2, tune=0, chains=1) #I set it up this way for debugging purposes
Which results in the following:
logp = -inf, ||grad|| = 4,910.9: 100%|██████████| 3/3 [00:00<00:00, 386.14it/s]
Only 3 samples in chain.
Sequential sampling (1 chains in 1 job)
NUTS: [d, b, a, sigma_a, alpha]
0%| | 0/3 [00:00<?, ?it/s]
and subsequently, “ValueError: Bad initial energy: inf. The model might be misspecified.”
When I check the test_point, it is showing “vol” being the culprit for -inf
for RV in quantity_model_1.basic_RVs:
print(RV.name, RV.logp(quantity_model_1.test_point))
alpha_interval__ -1.3862943611198906
sigma_a_log__ -0.7698925914732455
a_log__ -1.539785182946491
b_log__ -5.389248140312718
d -1098.5111832542225
vol -inf
On further investigation:
y_hat.tag.test_value
array([1.83882346, 1.13897182, 1.60580621, ..., 1.39741757, 1.15156967,
1.45517467])
total_quantity
array([7., 2., 4., ..., 1., 1., 1.])
I have attached a pickle of the model. Use this to load it:
with open('quantity_model_fortest.pkl', 'rb') as buff:
data = pickle.load(buff)
quantity_model_1 = data['model']
Versions and main components
PyMC3 Version: 3.4.1
Theano Version: 1.0.2
Python Version: 3.6.5
Operating system: ubuntu
How did you install PyMC3: (conda/pip) pip
quantity_model_fortest.zip
. Just FYI, you can save your data (or a subset of it) as a txt/csv file, and then you can upload it to Gist to have everything in one place.
Now remove the nans in temperature and try again the model. FYI you can usually do 