Hello, I am new to pymc3. I have model and am running into SamplingError: Bad initial energy.
I am wondering how to solve this.
Here is my code:
data = [0]

from theano.tensor import sqrt as Sqrt
from theano.tensor import log10 as log

with pm.Model() as model_g:
q = pm.Uniform(‘q’,lower=0.01, upper=0.9, transform=None)
rz=pm.Uniform(‘rz’,lower=401, upper=7, transform=None)
az = pm.Uniform(‘a/M’, lower=0, upper=0.8, transform=None)
fr= pm.Normal(‘M/M_s’, mu=5.4,sd=0.3)
μ = pm.Deterministic(‘μ’, Sqrt(0.25*(-441. + 4.774648292756861e7*((0.0006814774430966335*(1/rz)1.5)/fr + (0.0006814774430966335*az2*(1/rz)4.5)/fr - (0.0006814774430966335az)/(frrz3) - (0.5q(1/rz)1.5)/(2934.8fr - 1467.4frrz) - (1.qSqrt(1/rz)log(1. - 2./rz))/(2934.8fr - 1467.4frrz) + (2.q(1/rz)**1.5log(1. - 2./rz))/(2934.8fr - 1467.4frrz)))**2 + 100.(-17.3 + 3.e8*(-0.00010846050367445505*Sqrt(1/(fr2rz**3)) - (0.00010846050367445505az2*Sqrt(1/(fr2rz3)))/rz3 - (0.00016269075551168257az2*Sqrt(1/(fr2rz3)))/rz2 + (0.00032538151102336503azfr**1.5Sqrt(1/(fr2*rz3)))/(frrz)**1.5 + (0.07957747154594767frqSqrt(1/(fr2*rz3)))/(2934.8fr - 1467.4frrz) - (0.3183098861837907frqSqrt(1/(fr2*rz3))log(1. + 1/((1. - 2./rz)rz**2)))/(2934.8fr - 1467.4frrz) + (0.15915494309189535frqSqrt(1/(fr2*rz3))rzlog(1. + 1/((1. - 2./rz)rz**2)))/(2934.8fr - 1467.4frrz) - (0.3183098861837907frqSqrt(1/(fr**2rz3))log(1. - 2./rz))/(2934.8fr - 1467.4frrz) + (0.15915494309189535frq*Sqrt(1/(fr2rz**3))rzlog(1. - 2./rz))/(2934.8fr - 1467.4frrz) + 0.15915494309189535*((0.0006814774430966335*(1/rz)1.5)/fr + (0.0006814774430966335*az2*(1/rz)4.5)/fr - (0.0006814774430966335az)/(frrz3) - (0.5q(1/rz)1.5)/(2934.8fr - 1467.4frrz) - (1.qSqrt(1/rz)log(1. - 2./rz))/(2934.8fr - 1467.4frrz) + (2.q(1/rz)**1.5log(1. - 2./rz))/(2934.8fr - 1467.4frrz))))2 + 0.0625(-298. + 3.e8((0.012464278062135641azfrSqrt(1/rz)Sqrt((-1.(8804.400000000001fr - 1467.4fr*rz))/(fr3rz**4)))/(8804.400000000001fr - 1467.4frrz) + (0.024928556124271282azfr*(1/rz)1.5Sqrt((-1.(8804.400000000001fr - 1467.4fr*rz))/(fr3rz**4)))/(8804.400000000001fr - 1467.4frrz) + (12.193387752251892fr**2qSqrt((-1.(8804.400000000001fr - 1467.4frrz))/(fr**3rz4)))/((2934.8fr - 1467.4frrz)(8804.400000000001fr - 1467.4frrz)) - (3.048346938062973fr2qrzSqrt((-1.(8804.400000000001fr - 1467.4frrz))/(fr**3rz4)))/((2934.8fr - 1467.4frrz)(8804.400000000001fr - 1467.4frrz)) - 2.831374781276553e-6Sqrt((-8804.400000000001fr + 1467.4fr*rz)/(fr3rz**4)) + (9.145040814188922az2*fr2Sqrt((-8804.400000000001fr + 1467.4frrz)/(fr3*rz4)))/(-8804.400000000001fr + 1467.4frrz)**2 - (219.4809795405341az2*fr2Sqrt((-8804.400000000001fr + 1467.4frrz)/(fr3*rz4)))/(rz3*(-8804.400000000001fr + 1467.4frrz)**2) - (73.16032651351138az2fr**2Sqrt((-8804.400000000001fr + 1467.4frrz)/(fr**3rz4)))/(rz2*(-8804.400000000001fr + 1467.4frrz)**2) + (12.193387752251892az2*fr2Sqrt((-8804.400000000001fr + 1467.4frrz)/(fr3*rz4)))/(rz*(-8804.400000000001fr + 1467.4frrz)**2) - (73.16032651351138fr2qSqrt((-1.(8804.400000000001fr - 1467.4frrz))/(fr3rz4))*log(1. + 1/((1. - 2./rz)*rz2)))/((2934.8fr - 1467.4frrz)(8804.400000000001fr - 1467.4frrz)) + (48.77355100900757fr**2qrzSqrt((-1.(8804.400000000001fr - 1467.4frrz))/(fr3*rz4))log(1. + 1/((1. - 2./rz)rz**2)))/((2934.8fr - 1467.4frrz)(8804.400000000001fr - 1467.4frrz)) - (6.096693876125946fr2qrz2Sqrt((-1.(8804.400000000001fr - 1467.4frrz))/(fr**3rz4))*log(1. + 1/((1. - 2./rz)*rz2)))/((2934.8fr - 1467.4frrz)(8804.400000000001fr - 1467.4frrz)) - (73.16032651351138fr2qSqrt((-1.(8804.400000000001fr - 1467.4frrz))/(fr3rz**4))log(1. - 2./rz))/((2934.8fr - 1467.4frrz)(8804.400000000001fr - 1467.4frrz)) + (48.77355100900757fr2qrzSqrt((-1.(8804.400000000001fr - 1467.4fr*rz))/(fr3rz**4))log(1. - 2./rz))/((2934.8fr - 1467.4frrz)(8804.400000000001fr - 1467.4frrz)) - (6.096693876125946fr2qrz2Sqrt((-1.(8804.400000000001fr - 1467.4frrz))/(fr**3rz**4))log(1. - 2./rz))/((2934.8fr - 1467.4frrz)(8804.400000000001fr - 1467.4frrz)) + 0.15915494309189535*((0.0006814774430966335*(1/rz)1.5)/fr + (0.0006814774430966335*az2*(1/rz)4.5)/fr - (0.0006814774430966335az)/(frrz3) - (0.5q(1/rz)**1.5)/(2934.8fr - 1467.4frrz) - (1.qSqrt(1/rz)log(1. - 2./rz))/(2934.8fr - 1467.4frrz) + (2.q(1/rz)**1.5log(1. - 2./rz))/(2934.8fr - 1467.4fr*rz))))**2) )

``````y = pm.Normal('y', mu=μ, sd=1, observed=data)

trace= pm.sample(2000,target_accept=0.99,cores=1)
``````

## Here is the error:

SamplingError Traceback (most recent call last)
in
10 y = pm.Normal(‘y’, mu=μ, sd=1, observed=data)
11 # trace= pm.sample(20000,target_accept=0.99,chains=1,cores=1)
—> 12 trace= pm.sample(2000,target_accept=0.99,cores=1)

~\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)
487 _log.info(“Sequential sampling ({} chains in 1 job)”.format(chains))
488 _print_step_hierarchy(step)
→ 489 trace = _sample_many(**sample_args)
490

~\anaconda3\lib\site-packages\pymc3\sampling.py in _sample_many(draws, chain, chains, start, random_seed, step, **kwargs)
531 traces = []
532 for i in range(chains):
→ 533 trace = _sample(
534 draws=draws,
535 chain=chain + i,

~\anaconda3\lib\site-packages\pymc3\sampling.py in _sample(chain, progressbar, random_seed, start, draws, step, trace, tune, model, **kwargs)
603 try:
604 strace = None
→ 605 for it, (strace, diverging) in enumerate(sampling):
606 if it >= skip_first:
607 trace = MultiTrace([strace])

~\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.

~\anaconda3\lib\site-packages\pymc3\sampling.py in _iter_sample(draws, step, start, trace, chain, tune, model, random_seed)
698 step = stop_tuning(step)
699 if step.generates_stats:
→ 700 point, stats = step.step(point)
701 if strace.supports_sampler_stats:
702 strace.record(point, stats)

~\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

~\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