问题描述
我正在尝试生成我的模型的轨迹图,但它显示 module 'pymc3' has no attribute 'traceplot'
错误。我的代码是:
with pm.Model() as our_first_model:
# a priori
theta = pm.Beta('theta',alpha=1,beta=1)
# likelihood
y = pm.Bernoulli('y',p=theta,observed=data)
#y = pm.Binomial('theta',n=n_experimentos,observed=sum(datos))
start = pm.find_MAP()
step = pm.Metropolis()
trace = pm.sample(1000,step=step,start=start)
burnin = 0 # no burnin
chain = trace[burnin:]
pm.traceplot(chain,lines={'theta':theta_real});
然后给出以下错误:
AttributeError Traceback (most recent call last)
<ipython-input-8-40f97a342e0f> in <module>
1 burnin = 0 # no burnin
2 chain = trace[burnin:]
----> 3 pm.traceplot(chain,lines={'theta':theta_real});
AttributeError: module 'pymc3' has no attribute 'traceplot'
我使用的是 windows10 并且我已经用 pip 下载了 pymc3,因为它没有包含在我下载的 anaconda 中。
解决方法
自 several versions ago 起,PyMC3 将绘图和统计委托给 ArviZ,为了方便和易于转换,原始绘图命令被保留为 ArviZ 方法的别名。
最新的 PyMC3 版本 (3.11.0) 是第一个不包含 pm.traceplot
等别名的版本。您必须使用适用于 PyMC3 对象的 arviz.plot_trace
。
与问题本身无关的额外注释:
- 您正在使用
pm.find_MAP
初始化链并且您手动将采样器设置为pm.Metropolis
而不是允许pm.sample
选择其自己的默认值。这样做是有理由的,这并不是本质上的错误,但不鼓励这样做,请参阅PyMC3 FAQs。 - PyMC3 正在过渡到使用
InferenceData
作为pm.sample
的默认输出。我建议在return_inferencedata=True
中设置pm.sample
,原因如下:1) ArviZ 函数在幕后转换为这种格式,您将避免这种小开销,2) InferenceData 比 MultiTrace 具有更多功能,3) PyMC3 正在过渡到 InferenceData 作为pm.sample
的默认输出,那么为什么不开始呢? - 您有一个
# no burn-in
注释,但是,pm.sample
返回的跟踪已经执行了传递给它的tune
参数长度的老化。tune
的默认值为 1000。要实际获取所有样本并查看 MCMC 如何缓慢收敛到典型集,您需要使用discard_tuned_samples=False
。
一些 InferenceData 资源:
- 推理数据概述:https://arviz-devs.github.io/arviz/getting_started/XarrayforArviZ.html
- 使用 InferenceData 示例(展示如何执行老化等操作):https://arviz-devs.github.io/arviz/getting_started/WorkingWithInferenceData.html