Hello,
I wanted to implement this small GLM sub-module into code so that I could better understand how it works.
with pm.Model() as model_glmsubmodule:
pm.GLM.from_formula(‘alquiler ~ normalize_dia + log_cantidad’, data,
family=pm.glm.families.Binomial())trace_glmsubmodule = pm.sample(draws=500, chains=2, tune=500, cores=1, target_accept=0.9)
this would be the code similar to the one above:
with pm.Model() as model_fullsyntax:
# define priors, use the default intercept = pm.Flat('intercept') b1 = pm.Normal('normalize_dia', mu=0, tau=1.0E-6) b2 = pm.Normal('log_cantidad', mu=0, tau=1.0E-6) # define linear model yest = ( intercept + b1 * mx_ex1['normalize_dia'] + b2 * mx_ex1['log_cantidad']) ## Define Binomial likelihood likelihood = pm.Binomial('likelihood', n=1, p=yest, observed=mx_en1['alquiler']) trace_fullsyntax = pm.sample(500, chains=2, tune=500, init='adapt_diag', cores=1)
but I get the error in the second one:
SamplingError Traceback (most recent call last)
in
1 with model_fullsyntax:
----> 2 trace_fullsyntax = pm.sample(500, chains=2, tune=500, init=‘adapt_diag’, 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
491 discard = tune if discard_tuned_samples else 0~\anaconda3\lib\site-packages\pymc3\sampling.py in _sample_many(draws, chain, chains, start, random_seed, step, **kwargs)
537 step=step,
538 random_seed=random_seed[i],
–> 539 **kwargs
540 )
541 if trace is None:~\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)
1105 fp_write=getattr(self.fp, ‘write’, sys.stderr.write))
1106
-> 1107 for obj in iterable:
1108 yield obj
1109 # 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
146 adapt_step = self.tune and self.adapt_step_sizeSamplingError: Bad initial energy
Anyone can help me to understand that error? Thankyou so much!
EDIT: I reviewed it and the likelihood logp of the pm.Binomial is -inf, that’s the problem, but I can’t find a way to solve it.
EDIT2 : The mx_en1 and mx_ex1 I get from patsy function:
(mx_en1, mx_ex1) = pt.dmatrices(‘alquiler ~ normalize_dia + log_cantidad’, data, return_type=‘dataframe’, NA_action=‘raise’)