arm61 / uravu

A straightforward Bayesian data fitting library
https://uravu.readthedocs.io/
MIT License
25 stars 3 forks source link

Error thrown in markov chain monte carlo #25

Closed ff995 closed 3 years ago

ff995 commented 3 years ago

Hello again, I am getting an error message saying 'probablity function returned NaN' when running mcmc for a sample size of 135. Do you know what may cause this?

I also have some questions about how mcmc works. In your code, ln_probability is passed into EnsembleSampler in the mcmc function, is this replacing the log_prob function used in the emcee module? Also, ln_probability throws an error when I pass an array of values into it, including arrays of abscissa and ordinate values.

image image

Desktop (please complete the following information):

ff995 commented 3 years ago

Actually never mind, I have realised my mistake.

ff995 commented 3 years ago

My apologies, but I have encountered a similar error whilst using distributions. MCMC works well with a sample of 135 normal distributions, but throws the error shown above when the sample size is 150. I am sub-sampling a much larger array so the 2 samples have few elements in common.

arm61 commented 3 years ago

Hey @ff995 could you potential share more infomation about the problem? Or is it the code from the original issue?

ff995 commented 3 years ago

The function I am using now is very similar to the last but I have included a screenshot of this newer one as well as the errors and the code for the conversion of ordinate values to distributions.

image

image image image

arm61 commented 3 years ago

Sorry for the delay.

What are the contents of y in the lowest cell? Cause it still looks like a problem with the creation of the kernel density estimation object.

ff995 commented 3 years ago

y is a 1D array of experimental intensities. I am first removing all values where the intensity is zero from y and then I am creating a list of Distribution objects with values in y as the mean.

image

arm61 commented 3 years ago

Since you are working with normal distributions, can you try passing the y and ordinate_error=y*0.2 in to the Relationship class? This is to test if it is a problem in the creation of the Distribution-class object.

Sorry about the delay again (real life getting in the way of programming fun!)

ff995 commented 3 years ago

Thanks for responding. Running 'mini' with an ordinate error and no distribution still works with a sample size of 135 but throws the same error with a larger sample size.

arm61 commented 3 years ago

Is it possible that at some extrema of the parameters (guess1, etc.) there is a nonreal result to the calculate_Au3_secondary function?

ff995 commented 3 years ago

After some probing (and asking the creator of the function), I don't think this is the case. With non-physical values the function will still return real numbers. I have also run the function with more restricted bounds but the error still persists.

arm61 commented 3 years ago

Okay. I don't think I can debug this remotely. Would you be willing to send the relevant notebooks to me over email? (my email address should be on my profile)

ff995 commented 3 years ago

Hi Andrew, attached is the notebook I have been using.

UravuProject (1).zip

arm61 commented 3 years ago

Hey! There are a few functions normalise, convert_to_wavelength, etc that are called in this notebook but that aren't defined in the notebook, furthermore, the 'sec_trace.txt' is necessary to ensure everything works correctly. Would you possibly be able to send those over also? To accurately diagnose the problems, I need to be able to run everything 😄

arm61 commented 3 years ago

These functions might be in the MMM_core.ipynb notebook?

ff995 commented 3 years ago

Hi, I'm very sorry, here is a file containing the other notebooks. UravuProject (1).zip

arm61 commented 3 years ago

There is still no data file ('sec_trace.txt') that I can find...

ff995 commented 3 years ago

Very sorry (again), here is the document you're looking for. sec_trace (1).txt

arm61 commented 3 years ago

Hey! So the good news is I know what the problem is, the bad news is there is no nice fix yet. I am gonna work a fix over the next few weeks.

In the meantime if you select less than 100 points from the trace I have been able to get something that works. This is due to the numerical instability in the kde object that is used internally within uravu.

arm61 commented 3 years ago

Just to keep you up to date. So the problem was something deeper in the scipy library. However, I spent today working on a more robust solution and have made a pull request to the scipy package with my fix.

I am not sure how long this will take to make its way into the main scipy implementation. So I will put a workaround into uravu in the meantime and let you know when it is ready. Then once scipy have merged my change, I will silently update uravu.

arm61 commented 3 years ago

That is the newest version of uravu up on PyPI, so if you run pip install -U uravu this should install 1.2.0 which includes the workaround until scipy merge my change.

I will close this issue for now, but if my workaround doesn't solve things please reopen.