【问题标题】:PyMC3, NUTS sampler, what's happening here?PyMC3,NUTS 采样器,这里发生了什么?
【发布时间】:2020-01-29 07:11:32
【问题描述】:

有人可以指出我所看到的文档吗?

Jupyter 笔记本中的粉红色内容让我觉得有些不对劲。

使用 PyMC3(顺便说一句,这是一个课堂练习,我不知道自己在做什么)。

我输入了数字,最初在对角线上出现大约 0 的错误,将 alpha_estrate_est 交换为 1/alpha_est1/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 中的速率 ( 1/???? ) 参数使用指数先验。
将此随机变量称为 rate_(但它将作为 pm.Gamma 的 beta 参数提供)。提示:为 ???? 建立一个具有指数分布的先验你在哪里有一个初步的估计???? ????0 ,使用 1/????0 的比例参数。
使用您的 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,我可以相信我正在采样零。

【问题讨论】:

  • 我读过discourse.pymc.io/t/…——不是我理解的语言。 :-(
  • 我发现不理会 alpha_est 而只执行 1/rate_est 也可以正常运行。

标签: python bayesian pymc3


【解决方案1】:

我对您的 1/alpha 步骤有疑问。请参阅此讨论:https://discourse.pymc.io/t/help-with-fitting-gamma-distribution/2630

零问题可能是因为您从指数分布中采样零。

你可以看这里:https://docs.pymc.io/notebooks/PyMC3_tips_and_heuristic.htmlcell[6]

我认为您对采样器输出没问题。您可以使用traceplot 检查您的发行版。

【讨论】:

  • 谢谢。我希望我能理解你在说什么,但我认为我的意思是粉红色的东西还可以。
  • @VickiB 不要接受或支持对您没有满意帮助的答案。这样,您可能会吸引愿意按照您的条件进行更多解释的人。
  • @VickiB 随时不接受答案。我会努力为你写一个更好的答案。而且你不应该感到愚蠢:-)
  • > 零问题可能是因为您从指数分布中采样零。 - - 啊啊。我想是的,是的。我不需要反转 alpha(尽管我没有;没有伤害任何东西)。我确实需要反转汇率。显然,rae(未倒置)偶尔会抛出 0。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-04
  • 1970-01-01
  • 2013-11-03
  • 1970-01-01
  • 2010-12-14
  • 2017-11-14
相关资源
最近更新 更多