Python PyMC,循环中的确定性节点
我对Python和PyMC有点陌生,而且进展很快。但我只是对设置2D矩阵的确定值的用法感到困惑。我下面有一个模型,无法正确解析。问题与在模型中设置值Python PyMC,循环中的确定性节点,python,bayesian,pymc,Python,Bayesian,Pymc,我对Python和PyMC有点陌生,而且进展很快。但我只是对设置2D矩阵的确定值的用法感到困惑。我下面有一个模型,无法正确解析。问题与在模型中设置值theta有关 import numpy as np import pymc def NAFCgenerator(): D = np.empty(T, dtype=object) theta = np.empty([N,T], dtype=object) x = np.empty([N,T], dtype=object)
theta
有关
import numpy as np
import pymc
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()
定义已知变量
N = 2
T = 10
tau = 1
定义模型。。。我无法正确解析它。我遇到的问题是θ的分配。目的是获得D和x的样品。θ只是一个中间变量,但我需要保留它,因为它用于更复杂的模型变化
import numpy as np
import pymc
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')[:]
在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
下面是。当我执行代码时(使用
m=pymc.MCMC(NAFCgenerator())
),我得到了这个错误:ValueError:一个名为temp_theta的可安装pymc对象已经存在。这将导致某些数据库后端出现问题。
这就是您看到的问题吗?你应该把它添加到你的问题中,让人们更容易帮助你。很抱歉,将来会更显式。谢谢,能够使用显式索引对我很有用,来自JAGS。不过,提取MCMC样本似乎更为笨拙,需要像pymc_generator.trace('D_0')[:]
等这样的行。我将添加一个矢量化的版本,使其能够正常工作。