ValueError:使用 emcee 包时概率函数返回 NaN

问题描述

我正在使用 emcee 包来确定应遵循泊松分布的测量数据集的最佳参数。我使用的代码

def lnL_Poisson(theta,x,y,yerr):

    logA,beta = theta
    A = 10**logA
    model = the Poisson likelihood
    return np.sum(model)

def lnprior(theta):

    logA,beta = theta
    if -5 < logA < 0 and -2 < beta < 4:
        return 0.0
    return -np.inf


def lnprob_Poisson(theta,yerr):

    lp = lnprior(theta)
    if not np.isfinite(lp):
        return -np.inf
    return lp + lnL_Poisson(theta,yerr)

但是,当运行此代码时,它返回

ValueError                                Traceback (most recent call last)
<ipython-input-81-460e20ecdf72> in <module>
      1 sampler_2 = emcee.EnsembleSampler(nwalkers,ndim,lnprob_Poisson,args=(x,y_obs,dy))
----> 2 tmp = sampler_2.run_mcmc(pos,500) #Run the sampler 500 times
      3 samples_2 = sampler_2.chain[:,50:,:].reshape((-1,2))
      4 fig = corner.corner(samples_2,labels=[r"$\log(A)$",r"$\beta$"],quantiles=[0.16,0.5,0.84],show_titles=True,label_kwargs=dict(fontsize=15))

~\Anaconda3\lib\site-packages\emcee\ensemble.py in run_mcmc(self,initial_state,nsteps,**kwargs)
    382 
    383         results = None
--> 384         for results in self.sample(initial_state,iterations=nsteps,**kwargs):
    385             pass
    386 

~\Anaconda3\lib\site-packages\emcee\ensemble.py in sample(self,log_prob0,rstate0,blobs0,iterations,tune,skip_initial_state_check,thin_by,thin,store,progress)
    283             state.blobs = blobs0
    284         if state.log_prob is None:
--> 285             state.log_prob,state.blobs = self.compute_log_prob(state.coords)
    286         if np.shape(state.log_prob) != (self.nwalkers,):
    287             raise ValueError("incompatible input dimensions")

~\Anaconda3\lib\site-packages\emcee\ensemble.py in compute_log_prob(self,coords)
    454         # Check for log_prob returning NaN.
    455         if np.any(np.isnan(log_prob)):
--> 456             raise ValueError("Probability function returned NaN")
    457 
    458         return log_prob,blob

ValueError: Probability function returned NaN

代码在使用高斯对数似然法时确实有效。我猜这与某个地方的概率为 0 然后除以这个值有关。但是,我不知道如何解决这个问题。有人有这方面的经验吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)