试图按照PyMC3上的教程,它来到:“'模型'对象没有属性TransformedVar”

时间:2015-08-29 19:15:38

标签: python jupyter-notebook pymc3

我一直在尝试按照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)

第一个错误是“无法导入名称logtransform”。 第二个错误(如果我不尝试加载logtransform)是“'模型'对象没有属性TransformedVar”。

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

1 个答案:

答案 0 :(得分:5)

当前版本的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的子类,而这个类uses 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,其中讨论/解释了这一变化。