尝试按照 PyMC3 上的教程进行操作,结果是:“'Model' object has no attribute TransformedVar”

Trying to follow the tutorial on PyMC3, it comes to: "'Model' object has no attribute TransformedVar"

我一直在尝试按照 PYMC3 https://pymc-devs.github.io/pymc3/getting_started/ 上的教程进行操作,但是当我进入以下代码部分时,我 运行 出错了...

from pymc3 import Exponential, T, logtransform, exp, Deterministic
from pymc3.distributions.timeseries import GaussianRandomWalk


with Model() as sp500_model:

    nu = Exponential('nu', 1./10, testval=.1)

    sigma, log_sigma = sp500_model.TransformedVar('sigma', Exponential.dist(1./.02, testval=.1),
                                        logtransform)

    s = GaussianRandomWalk('s', sigma**-2, shape=n)

    volatility_process = Deterministic('volatility_process', exp(-2*s))

    r = T('r', nu, lam=volatility_process, observed=returns)

第一个错误是 "cannot import name logtransform"。 第二个错误(如果我不尝试加载 logtransform)是“'Model' 对象没有属性 TransformedVar”。

我运行在 Windows 7 上的 IPython Notebook 中使用它,我尝试卸载并重新安装 PyMC3 无济于事。

当前版本的 pymc3 与 the tutorial 不同步。

TransformedVar was removed 于 2015-06-03。

pymc3.logtransform was removed 于 2015-06-15。

新的实现方式no longer requires TransformedVar:

 sigma, log_sigma = model.TransformedVar(
     'sigma', Exponential.dist(1. / .02, testval=.1),
     logtransform)

取代
 sigma = Exponential('sigma', 1. / .02, testval=.1)

您的 pymc3 安装应包含 pymc3/examples/stochastic_volatility.py。 与网上教程不同,这段代码要和你的pymc3版本一致。

之所以可以这样简化代码,是因为ExponentialPositiveContinuous的子class,而这个classuses the logtransform by default .


作为记录,这里是 stochastic_volatility.py (as of 2015-06-04) 的当前版本:

from matplotlib.pylab import *
import numpy as np
from pymc3 import *
from pymc3.distributions.timeseries import *

from scipy.sparse import csc_matrix
from scipy import optimize

n = 400

returns = np.genfromtxt(get_data_file('pymc3.examples', "data/SP500.csv"))[-n:]
returns[:5]

model = Model()
with model:
    sigma= Exponential('sigma', 1. / .02, testval=.1)

    nu = Exponential('nu', 1. / 10)

    s = GaussianRandomWalk('s', sigma ** -2, shape=n)

    r = T('r', nu, lam=exp(-2 * s), observed=returns)


def run(n=2000):
    if n == "short":
        n = 50
    with model:
        start = find_MAP(vars=[s], fmin=optimize.fmin_l_bfgs_b)
        step = NUTS(model.vars, scaling=start, gamma=.25)
        trace = sample(5, step, start)

        # Start next run at the last sampled position.
        start2 = trace.point(-1)
        step2 = NUTS(model.vars, scaling=start2, gamma=.25)
        trace = sample(n, step2, trace=trace)

    # <codecell>

    # figsize(12,6)
    title(str(s))
    plot(trace[s][::10].T, 'b', alpha=.03)
    xlabel('time')
    ylabel('log volatility')

    # figsize(12,6)
    traceplot(trace, model.vars[:-1])

if __name__ == '__main__':
    run()

我通过从 github:

克隆 pymc3 找到了这个
git clone https://github.com/pymc-devs/pymc3

然后查看影响 transforms.py:

的提交
gitk pymc3/distributions/transforms.py 
gitk pymc3/distributions/continuous.py
gitk pymc3/examples/stochastic_volatility.py

一旦找到提交哈希(例如 c3120bce05bf8f1389272e1c38ddf83cb46c8d84), github 上的相应提交可以位于:

https://github.com/pymc-devs/pymc3/commit/c3120bce05bf8f1389272e1c38ddf83cb46c8d84

我无法在相关时间段 (2015-06-xx) 中找到 a github issue discussed/explained 此更改。