pymc3.DensityDist 中的形状变量不能正常工作

The shape variable in pymc3.DensityDist does not work properly

我正在尝试通过 pymc3.DensityDist() 定义多变量自定义分布;但是,我不断收到以下尺寸不匹配的错误:

"LinAlgError: 0-dimensional array given. Array must be two-dimensional"

我已经看过 https://github.com/pymc-devs/pymc3/issues/535 但我找不到问题的答案。为清楚起见,这是我的简单示例

import numpy as np
import pymc3 as pm


def pdf(x):
    y = 0
    print(x)
    sigma = np.identity(2)
    isigma  = sigma
    mu = np.array([[1,2],[3,4]])
    for i in range(2):
        x0 = x- mu[i,:]
        xsinv = np.linalg.multi_dot([x0,isigma,x0])
        y = y + np.exp(-0.5*xsinv)
    return y


logp = lambda x: np.log(pdf(x))
with pm.Model() as model:
    pm.DensityDist('x',logp, shape=2)
    step = pm.Metropolis(tune=False, S=np.identity(2)) 
    trace = pm.sample(100000, step=step, chain=1, tune=0,progressbar=False)

result = trace['x']

在这个简单的代码中,我想定义一个非规范化的 pdf 函数,它是两个非规范化正态分布的总和,并通过 Metropolis 算法从这个 pdf 中提取样本。

谢谢,

尝试在以下行中将 numpy 替换为 theano:

xsinv = tt.dot(tt.dot(x0, isigma), x0)
y = y + tt.exp(-0.5 * xsinv)

作为旁注,尝试使用 NUTS 而不是 metropolis 并让 PyMC3 为您选择采样方法,只需这样做

trace = pm.sample(1000)

为了以后参考你也可以提问here