【问题标题】:sampling multivariate uniform in PyMC3在 PyMC3 中采样多元均匀
【发布时间】:2017-04-30 22:43:09
【问题描述】:

我想使用 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 进行采样的函数。

我尝试了多种变体,但都没有奏效。当前代码失败:

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.

任何帮助表示赞赏!

【问题讨论】:

    标签: pymc pymc3


    【解决方案1】:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-10
      • 2012-09-18
      • 1970-01-01
      • 2018-05-08
      • 1970-01-01
      • 2017-07-12
      • 2014-06-06
      • 1970-01-01
      相关资源
      最近更新 更多