0

我正在使用 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, x, y, yerr):

    lp = lnprior(theta)
    if not np.isfinite(lp):
        return -np.inf
    return lp + lnL_Poisson(theta, x, y, 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, initial_state, 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 然后除以这个值有关。但是,我不知道如何解决这个问题。有人有这方面的经验吗?

4

1 回答 1

1

检查x数组和y数组的dtype. 我遇到了同样的问题,然后发现我的x数组是float32,而y数组是float64. 改成后xfloat64问题解决。

于 2022-01-27T12:04:17.763 回答