我想使用DensityDist
之前统一的自定义分布中的示例。精神上的东西:
import theano.tensor as T
from pymc3 import DensityDist, Uniform, Model
with Model() as model:
lim = 3
x0 = Uniform('x0', -lim, lim)
x1 = Uniform('x1', -lim, lim)
x = T.concatenate([x0,x1])
# Create custom densities
star = DensityDist('star', lambda x: star(x[:,0],x[:,1]))
其中star
是将2D笛卡尔点映射到非标准化对数似然函数的函数。这是我想要使用Metropolis-Hastings的样本。
我尝试了很多变化但没有效果。当前代码失败了:
ValueError: The index list is longer (size 2) than the number of dimensions of the tensor(namely 0). You are asking for a dimension of the tensor that does not exist! You might need to use dimshuffle to add extra dimension to your tensor.
任何帮助表示赞赏!
答案 0 :(得分:2)
x
的索引错误。它只是一维的,所以沿着两个维度的索引不能真正起作用。
import theano.tensor as tt
from pymc3 import DensityDist, Uniform, Model
def star(x):
return -0.5 * tt.exp(-tt.sum(x ** 2))
# or if you need the components individually
#return -0.5 * tt.exp(-x[0] ** 2 - x[1] ** 2)
with Model() as model:
lim = 3
x0 = Uniform('x0', -lim, lim)
x1 = Uniform('x1', -lim, lim)
x = T.stack([x0,x1])
# Create custom densities
star = DensityDist('star', star)