Bad Initial Energy

Muddling my way through my first PyMC3 model and am running into SamplingError: Bad Initial Energy. It’s not the inf or nan variety, and I’m pretty sure my data don’t contain either as I clean it for infs and nans beforehand. What’s going on?

Here’s the model. Y is the observed data in a pd.Series, t is a predictor also in a series, and dn is an array of series.

with pm.Model() as mod:
    
    alpha_0 = pm.Uniform("alpha_0", lower=1e-02, upper=1e02)
    delta = pm.Uniform("delta", lower=0.2, upper=5) # CHANGE TO RECIPROCAL IF POSSIBLE
    
    theta = pm.Uniform("theta", lower=5, upper=500)
    beta = pm.Uniform("beta", lower=1/duration, upper=10)
    b = pm.Uniform("b", lower=0, upper=30)
    t0 = pm.Uniform("t0", lower=0, upper=1000)
    nba = pm.Uniform("nba", lower=1e-06, upper=1e06)
    
    dist = [(b ** k) * (pm.math.exp(-b)) / tt.gamma(k+1) for k in range(0, len(dn))]
    e_dn = pm.math.dot(dist, dn)
    
    alpha_f = alpha_0 * delta
    
    timesens = -(t.to_list()-t0/theta)
    
    alpha = pm.Deterministic("alpha", alpha_0 + 1/(1+ pm.math.exp(timesens)) * (alpha_f - alpha_0))
    
    Y_exp = pm.math.log(beta) - alpha * e_dn
    
    Y_obs = pm.NegativeBinomial("Y_obs", mu=Y_exp, alpha=nba, observed=Y)

The model initialises okay, but then when I run trace = pm.sample(10000, cores=1), I get:

---------------------------------------------------------------------------
SamplingError                             Traceback (most recent call last)
<ipython-input-7-b697c6645656> in <module>
      1 with mod:
----> 2     trace = pm.sample(10000, cores=1)

~\AppData\Local\Continuum\anaconda3\lib\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, **kwargs)
    455                 _log.info('Sequential sampling ({} chains in 1 job)'.format(chains))
    456                 _print_step_hierarchy(step)
--> 457                 trace = _sample_many(**sample_args)
    458 
    459         discard = tune if discard_tuned_samples else 0

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\sampling.py in _sample_many(draws, chain, chains, start, random_seed, step, **kwargs)
    501     for i in range(chains):
    502         trace = _sample(draws=draws, chain=chain + i, start=start[i],
--> 503                         step=step, random_seed=random_seed[i], **kwargs)
    504         if trace is None:
    505             if len(traces) == 0:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\sampling.py in _sample(chain, progressbar, random_seed, start, draws, step, trace, tune, model, **kwargs)
    542     try:
    543         strace = None
--> 544         for it, strace in enumerate(sampling):
    545             if it >= skip_first:
    546                 trace = MultiTrace([strace])

~\AppData\Local\Continuum\anaconda3\lib\site-packages\tqdm\std.py in __iter__(self)
   1127 
   1128         try:
-> 1129             for obj in iterable:
   1130                 yield obj
   1131                 # Update and possibly print the progressbar.

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\sampling.py in _iter_sample(draws, step, start, trace, chain, tune, model, random_seed)
    631                 step = stop_tuning(step)
    632             if step.generates_stats:
--> 633                 point, states = step.step(point)
    634                 if strace.supports_sampler_stats:
    635                     strace.record(point, states)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\step_methods\arraystep.py in step(self, point)
    245 
    246         if self.generates_stats:
--> 247             apoint, stats = self.astep(array)
    248             point = self._logp_dlogp_func.array_to_full_dict(apoint)
    249             return point, stats

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pymc3\step_methods\hmc\base_hmc.py in astep(self, q0)
    142             )
    143             self._warnings.append(warning)
--> 144             raise SamplingError("Bad initial energy")
    145 
    146         adapt_step = self.tune and self.adapt_step_size

SamplingError: Bad initial energy

Any ideas what’s happening?

Hi,
I’m pretty sure it comes from your priors: you probably need more informative ones. Uniform priors are pretty much always discourage – here are good prior recommendations.
Also, here is our FAQ for Bad Initial Energy error – you’ll probably find something useful here :wink:
Hope this helps :vulcan_salute:

Thanks! I was trying to find info on the error in the docs but couldn’t find it anywhere… but the error I’m getting specifically isn’t the nan or inf version though, so it seems like it’s a different version of the error?

I will try modifying the priors and see if that helps!

Hmm, I tried switching all the priors to more informative stuff (in most cases, Lognormal, mu=0, sigma=<between 1 and 5 depending on var>), but I’m still getting the exact same error :confused:

Okay, maybe I’m missing something, but - I reverted to a much simpler version of the model and I’m still getting the SamplingError: Bad initial energy problem. Simplified model is as follows, with dn now a single pd.Series predictor variable:

with pm.Model() as mod:
    alpha = pm.Lognormal("alpha", 0.0, 4.0)
    beta = pm.Lognormal("beta", 0.0, 1.0)
    nba = pm.Lognormal("nba", 0.0, 6.0)
    
    Y_exp = pm.math.log(beta) - alpha * dn
    
    Y_obs = pm.NegativeBinomial("Y_obs", mu=Y_exp, alpha=nba, observed=Y)

This seems like a pretty basic model at this point and I just can’t figure out why it’s still not working :frowning:

One thing I tried, which at least got me past the SamplingError but is apparently not recommended and gave weird results, was setting the step to be pm.Metropolis(). But it seems this is a bad solution…

Which version of PyMC3 are you on ?

PyMC3 v3.7, through Jupyter

EDIT: Updated (through Anaconda) to v3.8 - still encountering the same issue, even on the most basic model in my latest reply above…

I would recommend using 3.9.3 through pip or using the latest from master branch of github directly.

Having said that I think the problem here is that Y_exp can go negative. RV alpha is unbounded above and you are subtracting it in Y_exp. mu parameter of NegativeBinomial should be strictly positive.

3 Likes

Ah, that’s it! Redefining Y_exp in a way that ends up strictly nonnegative solved the problem. Thank you!

1 Like

Dear TYLim,

can you provide some details about how you solved your problem? How did you redefine T_exp?