贝叶斯与PyMC3的相关性

时间:2016-10-16 21:32:52

标签: python correlation bayesian pymc pymc3

我试图将此example of Bayesian correlation for PyMC2转换为PyMC3,但结果却完全不同。最重要的是,多元正态分布的平均值很快变为零,而它应该在400左右(就像PyMC2一样)。因此,估计的相关性很快趋向于1,这也是错误的。

完整代码可在此notebook for PyMC2和此notebook for PyMC3中找到。

PyMC2的相关代码是

def analyze(data):
    # priors might be adapted here to be less flat
    mu = pymc.Normal('mu', 0, 0.000001, size=2)
    sigma = pymc.Uniform('sigma', 0, 1000, size=2)
    rho = pymc.Uniform('r', -1, 1)

    @pymc.deterministic
    def precision(sigma=sigma,rho=rho):
        ss1 = float(sigma[0] * sigma[0])
        ss2 = float(sigma[1] * sigma[1])
        rss = float(rho * sigma[0] * sigma[1])
        return np.linalg.inv(np.mat([[ss1, rss], [rss, ss2]]))

    mult_n = pymc.MvNormal('mult_n', mu=mu, tau=precision, value=data.T, observed=True)

    model = pymc.MCMC(locals()) 
    model.sample(50000,25000) 

上面代码到PyMC3的端口如下:

def precision(sigma, rho):
    C = T.alloc(rho, 2, 2)
    C = T.fill_diagonal(C, 1.)
    S = T.diag(sigma)
    return T.nlinalg.matrix_inverse(T.nlinalg.matrix_dot(S, C, S))


def analyze(data):
    with pm.Model() as model:
        # priors might be adapted here to be less flat
        mu = pm.Normal('mu', mu=0., sd=0.000001, shape=2, testval=np.mean(data, axis=1))
        sigma = pm.Uniform('sigma', lower=1e-6, upper=1000., shape=2, testval=np.std(data, axis=1))
        rho = pm.Uniform('r', lower=-1., upper=1., testval=0)

        prec = pm.Deterministic('prec', precision(sigma, rho))
        mult_n = pm.MvNormal('mult_n', mu=mu, tau=prec, observed=data.T)

    return model

model = analyze(data)
with model:
    trace = pm.sample(50000, tune=25000, step=pm.Metropolis())

PyMC3版本运行,但显然不会返回预期的结果。任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:4)

pymc.Normal的调用签名是

In [125]: pymc.Normal?
Init signature: pymc.Normal(self, *args, **kwds)
Docstring:
N = Normal(name, mu, tau, value=None, observed=False, size=1, trace=True, rseed=True, doc=None, verbose=-1, debug=False)

请注意pymc.Normal的第三个位置参数是tau,而不是标准差sd

因此,pymc代码使用

mu = Normal('mu', 0, 0.000001, size=2)

相应的pymc3代码应使用

mu = pm.Normal('mu', mu=0., tau=0.000001, shape=2, ...)

mu = pm.Normal('mu', mu=0., sd=math.sqrt(1/0.000001), shape=2, ...)

tau = 1/sigma**2

通过这一次更改,您的pymc3代码会产生(类似于)

enter image description here