2

I am using EMCEE. I write a part which problem is risen from there in the following. I ran into this error ValueError: lnprob returned NaN.

this error interrupts the calculation and I do not know how to overcome. So, I should do something to pass this error in order to continue the rest of computation.

The only things came to my mind is to add a line in lnprob function like:

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

but it is not correct

the code is:

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)

I appreciate your help.

Ma Y
  • 696
  • 8
  • 19
  • Is `lnlike` returning `+inf`? – Davis Herring Dec 28 '18 at 19:18
  • @DavisHerring No, it returns a number (e.g. 70, 35, 100 etc.) – Ma Y Dec 28 '18 at 19:21
  • Then how do you get NaN from the infinity produced by `log_prior`? – Davis Herring Dec 28 '18 at 20:23
  • it is logarithm, `-inf` is `zero` and `0` is `1`. I just want a way to pass the inputs from `log_prior` leading to `NaN` in `lnprob`. I just want to say python if the `lnprob` is NaN, never mind and continue with other values – Ma Y Dec 28 '18 at 21:59
  • @DavisHerring I got your point, `log_prior` will not be `inf`, it is just obtained a big number for some inputs. a big number like `2067579` that is ok. But I do not expect to see `NaN` error. Do you know a way to ignore this kind of error? or pass it? – Ma Y Dec 28 '18 at 23:05
  • I think there are some information missing in order to reproduce your error and then be able to solve it. For instance, the outputs of `log_prior` are hard coded and there are only two options, 0 or inf, and none of them are NaN. This means that the problem will most probably be with the likelihood or the chi2 functions, there may be a combination of input values which make chi2 return NaN. – OriolAbril Mar 06 '19 at 12:17

1 Answers1

0

I just want to say python if the lnprob is NaN, never mind and continue with other values

If this ^ is still the case for you, you should be able to do:

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

That code should replace the return statement in your def func(theta)

Ma Y
  • 696
  • 8
  • 19
fullStackChris
  • 1,300
  • 1
  • 12
  • 24