我试图通过复制此处给出的混合高斯示例来模拟指数混合。代码如下。我知道这里的推理有一些时髦的方面,但我的问题更多是关于如何在这样的模型中调试计算。
这个想法是它是三个指数的混合,比例参数取自 Gamma 分配给scales. 但是,在ElemwiseCategoricalStep. 通过查看 ,您可以看到指数分量的观测值分配最初是不同的initial_assignments,并且您可以看到所有观测值都分配给所有交互上的第零分量,因为它set(tr['exp'].flatten())仅包含 0。
p我认为这是因为在表达式array([logp(v * self.sh) for v in self.values])中分配给的所有值ElemwiseCategoricalStep.astep都是负无穷大。我想知道为什么会这样以及如何纠正它,但更重要的是,我想知道可以使用哪些工具来调试这种事情。有什么办法让我逐步计算,logp(v * self.sh)看看结果是如何确定的?如果我尝试使用 pdb 来执行此操作,我想我会在outputs = self.fn()in受到阻碍theano.compile.function_module.Function.__call__,我想我无法进入,因为它是一个本机函数。
即使知道如何为给定的一组模型参数计算 pdf 也是一个有用的开始。
import numpy as np
import pymc as pm
from pymc import Model, Gamma, Normal, Dirichlet, Exponential
from pymc import Categorical
from pymc import sample, Metropolis, ElemwiseCategoricalStep
durations = np.concatenate(
[np.random.exponential(1/lam, 10)
for lam in [1e-3,7e-5,2e-6]])
initial_assignments = np.random.randint(0, 3, len(durations))
print 'initial_assignments', initial_assignments
with Model() as model:
scales = Gamma('hp', 1, 1, shape=3)
props = Dirichlet('props', a=np.array([1., 1., 1.]), shape=3)
category = Categorical('exp', p=props, shape=len(durations))
points = Exponential('obs', lam=scales[category], observed=durations)
step1 = pm.Metropolis(vars=[props,scales])
step2 = ElemwiseCategoricalStep(var=category, values=[0,1,2])
start = {'exp': initial_assignments,
'hp': np.ones(3),
'props': np.ones(3),}
tr = sample(3000, step=[step1, step2], start=start)
print set(tr['exp'].flatten())