1

I have written the following code for MCMC using EMCEE Python package

In the log_prior function I defined the range of parameters to EMCEE moves between them not outside of them. But the problem is, in the results I see that for instance od0 has the value like 0.64 which is not in 0.68 < od0 < 0.70 I have this problem even for b.

I wonder how to force the EMCEE to be exactly between the ranges I defined. The model is very sensitive to values and I just want to explore between 0.68 < od0 < 0.70 not bigger or smaller than this.

def log_prior(H0, od0, c, b, M): 
    if  not 0.68 < od0 < 0.70 and  60 < H0 < 80  and   -20 < M < -18.5 and 0.045 < b < 0.065 :
        #return 0.0
        return -np.inf
    mu = 0.878
    sigma = 0.0004
    return np.log(1.0/(np.sqrt(2*np.pi)*sigma))-0.5*(c-mu)**2/sigma**2

without Gaussian prior the code is:

 def log_prior(H0, od0, c, b, M): 
        if  0.68 < od0 < 0.70 and  60 < H0 < 80  and   -20 < M < -18.5 and 0.045 < b < 0.065 :
            return 0.0
        return -np.inf
Ma Y
  • 696
  • 8
  • 19

2 Answers2

0

Use parentheses in your logical statement. For example, for od0 = 0.64, H0 = 50, where the prior should be -inf

not 0.68 < od0 < 0.70 and  60 < H0 < 80  
>> False

so the prior gives a negative log probability, but if you add parentheses:

not (0.68 < od0 < 0.70 and  60 < H0 < 80) 
>> True

which drives the prior to the correct value of -inf

0

To get best-fit values of each parameter within their prior range, just put prior range in bracks () i.e.

def log_prior(H0, od0, c, b, M): 
    if  (0.68 < od0 < 0.70 and  60 < H0 < 80  and   -20 < M < -18.5 and 0.045 < b < 0.065) :
        return 0.0
    return -np.inf

Hope this will work for you. Please let me know further.