Description
Description of your problem
I am trying to fit a Negative-Binomial regression model. However, I am running into SamplingError: Initial evaluation of model at starting point failed!
, even if I am trying to fit samples from the model prior.
I found out that the test_value of my observation value (which should be positive because of the Negative-Binomial distribution) are sometimes negative, which leads to the error. I then checked model.check_test_point()
and get:
alpha_log__ -1.14
mu -1.61
obs -inf
Name: Log-probability of test_point, dtype: float64
This happens because some of the test values of the observed variable are negative:
model_instance.obs.tag.test_value.min()
If I understand correctly, the test values of the observed variable are basically the same as the values I am trying to fit:
>>> model_instance.obs.tag.test_value[0][:10]
array([136519719, 139778075, 162329337, 147874471, 176346820, 131886145,
131664941, 145409569, 141731980, 152439463])
>>> prior_trace['obs'][0][:10]
array([136519719, 139778075, 162329337, 147874471, 176346820, 131886145,
131664941, 145409569, 141731980, 152439463])
However, for very large sampled values, the test value is negative:
>>> model_instance.obs.tag.test_value.min()
-2146971425
>>> prior_trace['obs'].reshape(-1)[model_instance.obs.tag.test_value.reshape(-1).argmin()]
2147995871
>>> model_instance.obs.tag.test_value.reshape(-1)[np.where(prior_trace['obs'].reshape(-1) != model_instance.obs.tag.test_value.reshape(-1))]
array([-2041161858, -1615277538, -2008654162, -1830866193, -2066486365,
-1939932683, -1927434696, -1920384235, -1997835169, -1856732049,
-1849204327, -1857766493, -2045704721, -1855359298, -2123503404,
-1829876267, -2071833989, -1976140492, -2110619502, -1816045663, ...
Thus, it seems that the test value is, for some reason, overflowing and turning negative for very large values.
Below is a minimal reproducer:
Please provide a minimal, self-contained, and reproducible example.
def model_factory(y):
with pm.Model() as model:
alpha = pm.HalfCauchy("alpha", 100)
mu = pm.Normal('mu', 17., 2.)
lam = pm.Deterministic('lambda', tt.exp(mu))
pm.NegativeBinomial("obs", mu=lam, alpha=alpha, observed=y)
return model
# Just some dummy data to let us sample from the prior
y = np.random.binomial(n=10, p=0.5, size=100)
# Sample from the model's prior
with model_factory(y):
prior_trace = pm.sample_prior_predictive(1000)
# Fit the model to the prior samples
model_instance = model_factory(prior_trace['obs'])
with model_instance:
trace = pm.sample()
Please provide the full traceback.
---------------------------------------------------------------------------
SamplingError Traceback (most recent call last)
<ipython-input-21-1bfc50620c9f> in <module>
1 model_instance = model_factory(prior_trace['obs'])
2 with model_instance:
----> 3 trace = pm.sample()
~/path/to/lib/python3.7/site-packages/pymc3/sampling.py in sample(draws, step, init, n_init, start, trace, chain_idx, chains, cores, tune, progressbar, model, random_seed, discard_tuned_samples, compute_convergence_checks, callback, return_inferencedata, idata_kwargs, mp_ctx, pickle_backend, **kwargs)
423 update_start_vals(chain_start_vals, model.test_point, model)
424
--> 425 check_start_vals(start, model)
426 if cores is None:
427 cores = min(4, _cpu_count())
~/path/to/lib/python3.7/site-packages/pymc3/util.py in check_start_vals(start, model)
228 "Initial evaluation of model at starting point failed!\n"
229 "Starting values:\n{}\n\n"
--> 230 "Initial evaluation results:\n{}".format(elem, str(initial_eval))
231 )
232
SamplingError: Initial evaluation of model at starting point failed!
Starting values:
{'alpha_log__': array(4.60517019), 'mu': array(17.)}
Initial evaluation results:
alpha_log__ -1.14
mu -1.61
obs -inf
Name: Log-probability of test_point, dtype: float64
Please provide any additional information below.
Versions and main components
- PyMC3 Version: current master (9deb5ee) (Note that this also happens with the current release)
- Theano Version: 1.0.11
- Python Version: 3.7.7
- Operating system: Mac OS X
- How did you install PyMC3: (conda/pip)