JohannesBuchner / UltraNest

Fit and compare complex models reliably and rapidly. Advanced nested sampling.
https://johannesbuchner.github.io/UltraNest/
Other
142 stars 30 forks source link

Assertion error , nan value in log likelihood #55

Closed shan1525 closed 2 years ago

shan1525 commented 2 years ago

Description

Dear @JohannesBuchner ,

I hope you are healthy and doing good. I am using your amazing program to marginalize my model parameters through the chi^2 of observed and theory parameters.

What I Did

firstly, I defined one of the chi^2 as

def chiH(Om0, w, wa, CC, h, rd):
        class= CLPCDM(Om0, w, wa, CC, h, rd)
        H_th = clas2.HUBz(0.)
        chiH1 = np.sum((74.0-H_th)**2./1.432**2)
        return chiH1 

and some other chi2 . Then I calculated the total chi^2. after that I defined the priors as this,

def my_prior_transform(cube):
    params = cube.copy()
 #### from 0.2 to 0.5
    params[0] = cube[0] * 0.3+0.2
###-3 to 3 
    params[1] = cube[1] * 6 - 3
 #### -3 to 3 
    params[2] = cube[2] * 6 - 3
 ##### this goes from -6 to 0
    params[3] = cube[3] * 5.999-0.6
##### 0.6 to 0.8
    params[4] = cube[4] * 0.2 + 0.6
######120 to 160
    params[5] = cube[5] * 40 + 120
#####-19.4 to -19
    params[6] = cube[6] * 0.4- 19.4
    return params   

and then I defined the loglike like this

def my_likelihood(params):
    Om0, w, wa, CC, h, rd, M = params
    return lnlike(Om0, w, wa, CC, h, rd, M)

where lnlike = -0.5*chi2(Om0, w, wa, CC, h, rd, M) Then I run it with the command

sampler = ultranest.ReactiveNestedSampler(param_names, my_likelihood, my_prior_transform,log_dir='shan/')

result = sampler.run()
sampler.print_results()

But I get the assertion error, where the log likelihood is coming to be nan for some combinations of the parameter space. The error is like this,

error in log likelihood function: returned non-finite numbers for input u=%s p=%s" % (logl, u, p)) assertionerror: error in loglikelihood function: returned non-finite number: [ nan -121726.57582046] for input u=[[0.78765003 0.00515522 0.81853438 0.81965105 0.5427253 0.18686831 0.66807462] [0.09369217 0.65672809 0.29540557 0.59462096 0.39587013 0.76211824 0.91300439]] p=[[ 7.08885029e-01 -2.96906866e+00 1.91120629e+00 -5.05059022e+00 7.08545060e-01 1.27474732e+02 -1.92672298e+01] [ 8.43229506e-02 9.40368563e-01 -1.22756659e+00 -1.13512078e+01 6.79174026e-01 1.50484730e+02 -1.93652018e+01]]

I think this may be the problem where I give the priors in my_prior_transform. I may have wrongly given the priors for (-6,0). Please, guide me through this. That will be very helpful to me. Thanks in advance. Please, don't mind if the question is too trivial. I am still learning.

With the best regards, shan.

JohannesBuchner commented 2 years ago

try:

u = np.random.uniform(size=len(param_names))
params = my_prior_transform(u)
print("params", params) # look whether the parameters are reasonable
l = my_likelihood(params)
print("likelihood:", l)

do this for several times until you hit the nan. Then figure out why this happens.

shan1525 commented 2 years ago

Thank you so much @JohannesBuchner for your prompt reply. I will try your suggestion and see what happens. I will update about it. I truly appreciate your help.

shan1525 commented 2 years ago

Dear @JohannesBuchner , Thanks for the earlier suggestion. It helped me to check the parameter values where the likelihood is giving nan. So , I realised that one of the parameters which I set to be negative is going positive, and for the positive value of that parameter , the likelihood is coming nan. To be more clear to you I set priors for params[3] to be [-6,0] , which I did like this params[3] = cube[3] 5.999-6 ###is it right to write like this? But it is coming positive in some cases see here params [ 0.46020096 2.83666804 1.89530621 1.5965133 0.74369123 148.30793771 -18.61855001].(the bold one is positive) likelihood: nan Have I wrongly given the priors ? I can't comprehend why is this going positive, where I have set this to negative. Please, help me through this. I really appreciate your help. With the best regards, shan. Update: When I set the priors to be like this : params[3] = cube[3] 6-6 I get a different error where the likelihood is not nan but this kind of error pops up, [ultranest] Explored until L=-7e+02
[ultranest] Likelihood function evaluations: 400 Traceback (most recent call last): File "ultra_nest.py", line 169, in result = sampler.run() File "/home/shanno/.local/lib/python3.6/site-packages/ultranest/integrator.py", line 2184, in run region_class=region_class, File "/home/shanno/.local/lib/python3.6/site-packages/ultranest/integrator.py", line 2536, in run_iter self._update_results(main_iterator, saved_logl, saved_nodeids) File "/home/shanno/.local/lib/python3.6/site-packages/ultranest/integrator.py", line 2634, in _update_results main_iterator, mpi_comm=self.comm if self.use_mpi else None) File "/home/shanno/.local/lib/python3.6/site-packages/ultranest/netiter.py", line 910, in combine_results assert np.isclose(w.sum() - 1, 0), w.sum() AssertionError: nan

Please help.

JohannesBuchner commented 2 years ago

Have I wrongly given the priors ?

Yes

I can't comprehend why is this going positive, where I have set this to negative.

cube contains values between 0 and 1. Just mentally place these values in, then you will see what values params can yield.

I get a different error where the likelihood is not nan but this kind of error pops up,

Using the code I posted above, have a look at several likelihood values of random points. Perhaps they are all equal?

JohannesBuchner commented 2 years ago

Here are instructions on how to format code on github to be easily readable: https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax

shan1525 commented 2 years ago

Here are instructions on how to format code on github to be easily readable.

I am really sorry. I didn't write in this manner and made my query difficult to read. I will keep this in mind while raising an issue. And thanks for helping me with your suggestions.

regarding the assertion error.

I will check manually about the likelihoods. Thanks for your help.

with the best regards, shan.

shan1525 commented 2 years ago

Dear @JohannesBuchner ,

I played around with the params. And checked manually with your code . The likelihood is not coming nan. But I am getting new kind of assertion error like this

[ultranest] Explored until L=-3e+03
[ultranest] Likelihood function evaluations: 400 Traceback (most recent call last): File "ultra_nest.py", line 169, in result = sampler.run() File "/home/shanno/.local/lib/python3.6/site-packages/ultranest/integrator.py", line 2184, in run region_class=region_class, File "/home/shanno/.local/lib/python3.6/site-packages/ultranest/integrator.py", line 2536, in run_iter self._update_results(main_iterator, saved_logl, saved_nodeids) File "/home/shanno/.local/lib/python3.6/site-packages/ultranest/integrator.py", line 2634, in _update_results main_iterator, mpi_comm=self.comm if self.use_mpi else None) File "/home/shanno/.local/lib/python3.6/site-packages/ultranest/netiter.py", line 910, in combine_results assert np.isclose(w.sum() - 1, 0), w.sum() AssertionError: nan

If you don't mind. Can you please , tell me what could be the problem? I am sorry for bothering you with these naive questions.

regard, shan.

JohannesBuchner commented 2 years ago

Huh, I have not seen this before. Not sure how w can end up having NaNs here https://github.com/JohannesBuchner/UltraNest/blob/master/ultranest/netiter.py#L910

shan1525 commented 2 years ago

Dear @JohannesBuchner , I found out the solution to my problem. As it was mentioned in #5 , I averted the nan errors by defining my log_likelihood as :

def log_likelihood(params):
    Om0, w, wa, CC, h, rd, M = params
    ll = lnlike(Om0, w, wa, CC, h, rd, M)
    if np.isnan(ll):
                return -1e300
    else:
                return ll

I think that numpy.sqrt() was creating the assertion error trouble , because I was getting the following errors before the assertion error :

[ultranest] Sampling 400 live points from prior ... /home/shanno/Desktop/New_work/cosmo.py:405: RuntimeWarning: invalid value encountered in sqrt return np.sqrt(a+b*(1.+z)*3.+c(1.+z)*4) /home/shanno/Desktop/New_work/cosmo.py:101: IntegrationWarning: The occurrence of roundoff error is detected, which prevents the requested tolerance from being achieved. The error may be underestimated. return self.D_H()quad(self.invhub, 0, z1)[0] /home/shanno/.local/lib/python3.6/site-packages/numpy/lib/function_base.py:2192: RuntimeWarning: invalid value encountered in ? (vectorized) outputs = ufunc(*inputs) .

I am very grateful of you for your invaluable suggestions regarding the queries. Thank you so much for your fast and prompt replies. It helped me a lot to understand the problem. I am closing the issue now as it has been resolved.

With the best regards, shan.