【问题标题】:PyMC, deterministic nodes in loopsPyMC,循环中的确定性节点
【发布时间】:2014-07-20 15:22:28
【问题描述】:

我对 Python 和 PyMC 有点陌生,并且进步很快。但我只是对使用设置二维矩阵的确定性值感到困惑。我在下面有一个模型,我无法正确解析。问题与在模型中设置值 theta 有关。

import numpy as np
import pymc

定义已知变量

N = 2
T = 10
tau = 1

定义模型...我无法正确解析。这是我遇到问题的theta 的分配。目的是获取 D 和 x 的样本。 Theta 只是一个中间变量,但我需要保留它,因为它用于模型的更复杂的变体。

def NAFCgenerator():

    D = np.empty(T, dtype=object)
    theta = np.empty([N,T], dtype=object)
    x = np.empty([N,T], dtype=object)

    # true location of signal
    for t in range(T):
        D[t] = pymc.DiscreteUniform('D_%i' % t, lower=0, upper=N-1)

    for t in range(T):
        for n in range(N):
            @pymc.deterministic(plot=False)
            def temp_theta(dt=D[t], n=n):
                return dt==n
            theta[n,t] = temp_theta

            x[n,t] = pymc.Normal('x_%i,%i' % (n,t),
                               mu=theta[n,t], tau=tau)

    return locals()

** 编辑 **

显式索引对我很有用,因为我正在学习 PyMC 和 Python。但似乎提取 MCMC 样本有点笨拙,例如

D0values = pymc_generator.trace('D_0')[:]

但我可能遗漏了一些东西。但是我是否设法让矢量化版本正常工作

# Approach 1b - actually quite promising
def NAFCgenerator():

# NOTE TO SELF. It's important to declare these as objects
D = np.empty(T, dtype=object)
theta = np.empty([N,T], dtype=object)
x = np.empty([N,T], dtype=object)

# true location of signal
D = pymc.Categorical('D', spatial_prior, size=T)

# displayed stimuli
@pymc.deterministic(plot=False)
def theta(D=D):
    theta = np.zeros([N,T])
    theta[0,D==0]=1
    theta[1,D==1]=1
    return theta

#for n in range(N):    
x = pymc.Normal('x', mu=theta, tau=tau)

return locals()

例如,使用此示例似乎更容易获得 MCMC 示例

Dvalues = pymc_generator.trace('D')[:]

【问题讨论】:

  • 当我执行您的代码(使用m = pymc.MCMC(NAFCgenerator()))时,我收到此错误:ValueError: A tallyable PyMC object called temp_theta already exists. This will cause problems for some database backends. 这是您看到的问题吗?您应该将其添加到您的问题中,以便人们更容易地提供帮助。
  • 是的。抱歉,以后会更明确。

标签: python bayesian pymc


【解决方案1】:

在 PyMC2 中,当使用装饰器创建确定性节点时,默认是从函数名中获取节点名。解决方法很简单:指定节点名作为装饰器的参数。

        @pymc.deterministic(name='temp_theta_%d_%d'%(t,n), plot=False)
        def temp_theta(dt=D[t], n=n):
            return dt==n
        theta[n,t] = temp_theta

这里是a notebook that puts this in context

【讨论】:

  • 谢谢,能够使用显式索引对我很有用,来自 JAGS。似乎提取 MCMC 样本比较笨拙,需要像 pymc_generator.trace('D_0')[:] 这样的行,我将添加一个我设法开始工作的矢量化版本。
猜你喜欢
  • 2017-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-10
  • 2014-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多