2

我正在使用EMCEE。我在下面写了从那里出现问题的部分。我遇到了这个错误 ValueError: lnprob returned NaN.

这个错误会中断计算,我不知道如何克服。所以,我应该做一些事情来传递这个错误,以便继续剩下的计算。

我想到的唯一事情是在lnprob函数中添加一行,例如:

 if not np.isfinite(lp):
        return -np.inf
 if  np.isnan(lp):
        return -np.inf

但这是不正确的

代码是:

def log_prior(H0, od0, c, b, Orc, M):
    if  0.4 < od0 < 0.9 and  50 < H0 < 90  and  0 < c < 3 and  0 < b < 1  and  -0.3 < M < 0.2 and  0 < Orc < 0.1:
        return 0.0
    return -np.inf

def lnlike(H0, od0, c, b, Orc, M):
    lg = -chi2(H0, od0, c, b, Orc, M)/2.
    return lg

def lnprob(H0, od0, c, b, Orc, M):
    lp = log_prior(H0, od0, c, b, Orc, M)
    if not np.isfinite(lp):
        return -np.inf
    return lp + lnlike(H0, od0, c, b, Orc, M)

def func(theta):
    H0, od0, c, b, Orc, M = theta
    return -2. * lnprob(H0, od0, c, b, Orc, M)

我感谢您的帮助。

4

1 回答 1

0

如果 lnprob 是 NaN,我只想说 python,没关系,继续使用其他值

如果这 ^ 仍然适合您,您应该能够:

def func(theta):
    H0, od0, c, b, Orc, M = theta
    try:
       lnprobval = -2. * lnprob(H0, od0, c, b, Orc, M)
    except ValueError: # NaN value case
       lnprobval = -np.inf # just set to negative infinity 
    return lnprobval

该代码应替换return您的语句def func(theta)

于 2018-12-28T23:08:14.913 回答