将pymc2代码移植到pymc3:自定义似然函数

Porting pymc2 code to pymc3: custom likelihood function

我正在尝试实施 Lee&Wagenmakers 书中的删失数据示例(第 5.5 章,第 70 页)。在 pymc2 中,我有以下模型:

nattempts = 950   
nfails = 949   
n = 50    # Number of questions
y = np.zeros(nattempts)
y[nattempts-1] = 1
z = 30
unobsmin = 15
unobsmax = 25
unobsrange = np.arange(unobsmin,unobsmax+1)

theta = pymc.Uniform("theta",lower = .25, upper = 1)

@pymc.observed
def Ylike(value=z, theta = theta, n=n, censorn=nfails, unobs=unobsrange):
    ylikeobs = pymc.binomial_like(x=value, n=n, p=theta)
    ylikeunobs = np.array([])
    for i in unobs:
        ylikeunobs = np.append(pymc.binomial_like(x=i, n=n, p=theta),ylikeunobs)
    return ylikeobs+sum(ylikeunobs)*censorn

testmodel = pymc.Model([theta,Ylike])
mcmc = pymc.MCMC(testmodel)
mcmc.sample(iter = 20000, burn = 50, thin = 2)

其中涉及装饰器 @pymc.observed
我想我需要使用 pm.DensityDist 来表达可能性,但是,我不知道该怎么做。

好的,我知道怎么做了:

with pm.Model():
    theta = pm.Uniform("theta",lower = .25, upper = 1)
    def logp(value,n,p):
        return pm.dist_math.bound(
        pm.dist_math.binomln(n, value) 
      + pm.dist_math.logpow(p, value) 
      + pm.dist_math.logpow(1 - p, n - value),
        0 <= value, value <= n,
        0 <= p, p <= 1)
    def Censorlike(value=z, n=n, censorn=nfails, unobs=unobsrange):
        ylikeobs = logp(value=value, n=n, p=theta)
        ylikeunobs = 0
        for i in unobs:
            ylikeunobs += logp(value=i, n=n, p=theta)
        return ylikeobs+ylikeunobs*censorn

    ylike = pm.DensityDist('ylike', Censorlike, observed={'value':z,'n':n,'censorn':nfails,'unobs':unobsrange})
    trace = pm.sample(3e3)