有人可以指出要解释我所见内容的文档吗?
Jupyter笔记本中的粉红色东西让我觉得有些问题。
使用PyMC3(顺便说一句,这是一堂课的练习,我不知道自己在做什么)。
我插入了数字,最初在对角线上出现大约0的错误,将alpha_est
和rate_est
交换为1/alpha_est
和1/rate_est
(并停止获取错误),但我仍然得到粉红色的东西。
此代码随练习一起提供:
# An initial guess for the gamma distribution's alpha and beta
# parameters can be made as described here:
# https://wiki.analytica.com/index.php?title=Gamma_distribution
alpha_est = np.mean(no_insurance)**2 / np.var(no_insurance)
beta_est = np.var(no_insurance) / np.mean(no_insurance)
# PyMC3 Gamma seems to use rate = 1/beta
rate_est = 1/beta_est
# Initial parameter estimates we'll use below
alpha_est, rate_est
然后是我要添加的代码:
粉红色的东西会让我感到紧张还是我只是说“没有错误,继续前进”?
=======
“零问题”
---------------------------------------------------------------------------
RemoteTraceback Traceback (most recent call last)
RemoteTraceback:
"""
Traceback (most recent call last):
File "/Local/Users/vlb/anaconda3/lib/python3.7/site-packages/pymc3/parallel_sampling.py", line 110, in run
self._start_loop()
File "/Local/Users/vlb/anaconda3/lib/python3.7/site-packages/pymc3/parallel_sampling.py", line 160, in _start_loop
point, stats = self._compute_point()
File "/Local/Users/vlb/anaconda3/lib/python3.7/site-packages/pymc3/parallel_sampling.py", line 191, in _compute_point
point, stats = self._step_method.step(self._point)
File "/Local/Users/vlb/anaconda3/lib/python3.7/site-packages/pymc3/step_methods/arraystep.py", line 247, in step
apoint, stats = self.astep(array)
File "/Local/Users/vlb/anaconda3/lib/python3.7/site-packages/pymc3/step_methods/hmc/base_hmc.py", line 130, in astep
self.potential.raise_ok(self._logp_dlogp_func._ordering.vmap)
File "/Local/Users/vlb/anaconda3/lib/python3.7/site-packages/pymc3/step_methods/hmc/quadpotential.py", line 231, in raise_ok
raise ValueError('\n'.join(errmsg))
ValueError: Mass matrix contains zeros on the diagonal.
The derivative of RV `alpha__log__`.ravel()[0] is zero.
"""
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
ValueError: Mass matrix contains zeros on the diagonal.
The derivative of RV `alpha__log__`.ravel()[0] is zero.
The above exception was the direct cause of the following exception:
RuntimeError Traceback (most recent call last)
<ipython-input-14-36f8e5cebbe5> in <module>
13 g = pm.Gamma('g', alpha=alpha_, beta=rate_, observed=no_insurance)
14
---> 15 trace = pm.sample(10000)
/Local/Users/vlb/anaconda3/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, **kwargs)
435 _print_step_hierarchy(step)
436 try:
--> 437 trace = _mp_sample(**sample_args)
438 except pickle.PickleError:
439 _log.warning("Could not pickle model, sampling singlethreaded.")
/Local/Users/vlb/anaconda3/lib/python3.7/site-packages/pymc3/sampling.py in _mp_sample(draws, tune, step, chains, cores, chain, random_seed, start, progressbar, trace, model, **kwargs)
967 try:
968 with sampler:
--> 969 for draw in sampler:
970 trace = traces[draw.chain - chain]
971 if (trace.supports_sampler_stats
/Local/Users/vlb/anaconda3/lib/python3.7/site-packages/pymc3/parallel_sampling.py in __iter__(self)
391
392 while self._active:
--> 393 draw = ProcessAdapter.recv_draw(self._active)
394 proc, is_last, draw, tuning, stats, warns = draw
395 if self._progress is not None:
/Local/Users/vlb/anaconda3/lib/python3.7/site-packages/pymc3/parallel_sampling.py in recv_draw(processes, timeout)
295 else:
296 error = RuntimeError("Chain %s failed." % proc.chain)
--> 297 raise error from old_error
298 elif msg[0] == "writing_done":
299 proc._readable = True
RuntimeError: Chain 0 failed.
此处说明中的“提示”是告诉我应使用1/rate_est
吗?
您现在要创建自己的PyMC3模型!
对alpha使用指数优先。将此随机变量称为alpha_。
同样,在PyMC3的Gamma中,对rate(1/1)参数使用指数优先。
将此随机变量称为rate_(但它将作为pm.Gamma的beta参数提供)。提示:如果要建立prior的指数分布且先验估计to0的先验,请使用比例参数1 / a0。
使用您的alpha_和rate_随机变量以及观察到的数据创建Gamma分布。
进行10000次抽奖。
零问题可能是因为您正在从指数分布中采样零。
啊:
rate_est是0.00021265346963636103
rate_ci = np.percentile(trace ['rate_'],[2.5,97.5]) rate_ci = [0.00022031,0.00028109]
1 / rate_est是4702.486170152818
如果使用rate_est
,我可以相信我将对零采样。
答案 0 :(得分:1)
我对您的1 / alpha步骤感到怀疑。查看此讨论:https://discourse.pymc.io/t/help-with-fitting-gamma-distribution/2630
零问题可能是因为您正在从指数分布中采样零。
您可以在这里查看:https://docs.pymc.io/notebooks/PyMC3_tips_and_heuristic.html单元格[6]
我认为您对采样器的输出还可以。您可以使用traceplot
检查发行版本。