pymc3:NUTS采样,多层次模型,Wishart分布

时间:2017-01-28 12:21:07

标签: pymc3

我在pymc3的分层模型中对NUTS采样有问题。我在pymc3中做了一些简单的模型,没有任何问题,但是这个任务我无法移动。有人能帮帮我吗?

可以从这里获取数据:cars2004.csv。首先应该完成一些数据集工作:

import numpy as np
import pandas as pd
import pymc3 as pm
import theano.tensor as T

data = pd.read_csv("cars2004.csv", sep=",", index_col= 0)

interest = ["price.retail", "cons.city", "cons.highway", "engine.size", 
            "horsepower","weight", "wheel.base", "length", "width", "ncylinder"]

df1 = data[data.ncylinder != -1]
df1 = df1[df1.fhybrid != "Yes"]
df = df1[interest].copy()
n, p = len(df), 9
df["price.retail"] = df["price.retail"].apply(lambda x: x/1000)

python中的模型:

with pm.Model() as model:
    lambd = pm.Gamma("lambd", alpha=1, beta=.005)
    beta0 = pm.Normal("beta0", mu = 80, sd = 100)
    beta = pm.MvNormal("beta", mu = np.zeros(p), tau = lambd*np.eye(p), shape = p)

    mu = pm.MvNormal("mu", mu = np.array([10,10,3,200,1500,250,500,10,5]),
                     cov = np.diag([100,100,100,1000**2,1000**2,100**2,100**2,100**2,100]),
                     shape=p)

    sigmainvert = pm.WishartBartlett("sigmainvert", nu=p,S=.001*np.eye(p),
                                     testval=.001*np.eye(p))

    tau = pm.Gamma("tau", 1, .005)
    #sigma = pm.Uniform("sigma", .1, 100)

    X = pm.MvNormal("X", mu = mu, tau = sigmainvert, shape = (n,p),
                    observed = df[interest[1:]])

    Y = pm.Normal("Y", mu = beta0 + T.dot(X, beta), tau = tau,
                  observed = df["price.retail"])
    dev = pm.Deterministic("dev",-2*Y.logpt)

先前分布中的值是从赋值中获得的。 “X”有一些缺失数据,“sigmainvert”应该是Wishart(9,diag(0.001,...,0.001)。

ADVI用于NUTS步骤和起始值的应用(首先我在NaN elbo和NaN值方面存在一些问题,现在elbo = -inf)。取样:

with model:
    means, sds, elbo = pm.variational.advi(n=50000, learning_rate=0.1)
    step = pm.NUTS(scaling = model.dict_to_array(sds)**2, is_cov=True)
    trace = pm.sample(1000, step, start = means)  

plot1 = pm.traceplot(trace, varnames=["beta0","beta","tau","mu","dev"],alpha=1)  

跟踪的结果图:traceplot。如您所见,没有完成MCMC推断,跟踪作为起始值是常量。使用Metropolis步骤对相同模型进行采样可使“mu”轨迹保持不变,其他方式也可以。问题在哪里?

0 个答案:

没有答案