我正在 emcee 库中运行 emcee.EnsembleSampler 函数,为我正在做的一些研究制作参数的马尔可夫链。由于某些原因,run_mcmc 函数运行的某些时候,我在 emcee.EnsembleSampler.chain 对象中找到的链在本质上应该近似高斯时根本没有变化。
我已经在我的一些数据上运行了多次,它工作得非常好,但在某些情况下,它实际上只是链没有迭代,只有直线。
我如何使用司仪代码(大约 90% 的时间有效):
sampler1 = emcee.EnsembleSampler(n_walkers, n_dim, self.__ln_prob, args = (event1, parameters_to_fit, parallaxType1), threads = 1)
sampler1.run_mcmc(start1, n_steps)
samples1 = sampler1.chain[:, n_burn:, :].reshape((-1, n_dim), order = 'F')
__ln_prob 函数是这样写的(这里也详细介绍了其中使用的函数):
def __ln_prob(self, theta, event, parameters_to_fit, parallaxType):
""" combines likelihood and priors """
ln_prior_ = self.__ln_prior(theta, parameters_to_fit, parallaxType)
if not np.isfinite(ln_prior_):
return -np.inf, [-1, -1]
ln_like_ = self.__ln_like(theta, event, parameters_to_fit)
if np.isnan(ln_like_):
return -np.inf, [-1, -1]
if (parallaxType == 'positive') or (parallaxType == 'negative'):
sourceFlux, blendFlux = event.get_ref_fluxes()
if (sourceFlux[0] <= 0) or (blendFlux <= 0):
return -np.inf, [-1, -1]
sourceMag = MulensModel.Utils.get_mag_from_flux(sourceFlux[0])
blendMag = MulensModel.Utils.get_mag_from_flux(blendFlux)
return ln_prior_ + ln_like_, [sourceMag, blendMag]
else:
return ln_prior_ + ln_like_, [-1, -1]
先验功能:
def __ln_prior(self, theta, parameters_to_fit, parallaxType):
""" priors - we only reject obviously wrong models """
if theta[parameters_to_fit.index("t_E")] < 0.:
return -np.inf
elif (theta[parameters_to_fit.index("u_0")] < 0.) and (parallaxType == 'positive'):
return -np.inf
elif (theta[parameters_to_fit.index("u_0")] > 0.) and (parallaxType == 'negative'):
return -np.inf
return 0.0
我预计链值会振荡,但在某些情况下不会。我后来的代码没有抛出任何错误,所以似乎没有一个 -np.inf 值从 __ln_prior 或 __ln_prob 返回。如果返回 -np.inf 我认为代码应该中断?也许这就是为什么?
我认为代码对于理解这个问题不是很有必要,我只是希望这一定是一些我明显不理解的相对频繁出现的问题。
我尝试查看源代码,了解为什么 sample() 函数可能返回与其接收到的相同的参数集,但我似乎没有找到这一点。
这是找到源代码的地方: https ://github.com/dfm/emcee/blob/master/emcee/ensemble.py