在 PyMC3 中采样多变量制服
sampling multivariate uniform in PyMC3
我想在使用 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
是将二维笛卡尔点映射到非标准化对数似然函数的函数。这是我想使用 Metropolis-Hastings 采样的函数。
我尝试了多种变体,但 none 奏效了。当前代码失败:
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.
感谢任何帮助!
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)
我想在使用 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
是将二维笛卡尔点映射到非标准化对数似然函数的函数。这是我想使用 Metropolis-Hastings 采样的函数。
我尝试了多种变体,但 none 奏效了。当前代码失败:
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.
感谢任何帮助!
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)