2017-04-30 31 views
2

DensityDistを使用する前に均一なカスタム分布のサンプルを使用したいと思います。精神に何か:starは非正規化対数尤度関数の2Dデカルトポイントをマッピングする関数であるPyMC3で多変量のサンプルをサンプリングする

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])) 

。 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. 

答えて

2

xへのインデックスが間違っています。これは1次元だけなので、2次元のインデックス作成は実際には機能しません。

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) 
関連する問題