1

我正在尝试实施 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,但是,我不知道该怎么做。

4

1 回答 1

0

好的,我发现了如何做到这一点:

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)
于 2016-03-15T01:31:56.213 回答