SiLab-Bonn / pylandau

Simple Landau distribution definitions to be used in Python.
GNU Lesser General Public License v2.1
20 stars 5 forks source link

Very large mpv in pylandau.landau results in RunTimeError #6

Closed stevemolloy closed 2 years ago

stevemolloy commented 7 years ago
>>> landau(np.array([1.0]), mpv=1e10, eta=20, A=10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pyLandau\cpp\pylandau.pyx", line 84, in pylandau.landau (pyLandau/cpp/pylandau.cpp:3904)
  File "pyLandau\cpp\pylandau.pyx", line 129, in pylandau._scale_to_mpv (pyLandau/cpp/pylandau.cpp:5457)
RuntimeError: Cannot calculate MPV, check function parameters and file bug report!

Note the very large value of mpv.

No such error is reported for mpv=1e9. The threshold for causing this error is somewhere between 8.5899345e9 and 8.5899346e9.

I'm using OS X 10.11.6

Python 3.6.0 (default, Dec 24 2016, 00:01:50) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
stevemolloy commented 7 years ago

It seems that this exception is thrown from pylandau.pyx, and is a result of scipy.optimize.fmin not converging when called in the function _scale_to_mpv.

DavidLP commented 7 years ago

Thank you for your bug report. You are right, the issue is not in pylandau itself but a scipy module it uses. May I ask why it makes sense to support a MPV that is 4 orders larger than the eta? As a workaround you can right now just shift the distribution by 10^5. The shifting could also be implemented as a solution to this bug to be still able to use scipy.optimize.fmin on extreme parameter values.

stevemolloy commented 7 years ago

No problem. You're right to note that it is probably a little nonsensical to use such extreme values. I discovered this when using Python Hypothesis to test my code.

https://github.com/stevemolloy/BLMs/tree/master

This testing framework works by sending a statistically large number of wildly different inputs to confirm that the code responds as expected -- an extremely robust way to test. I discovered this issue with PyLandau when Hypothesis used these very extreme values.

I agree that it is not a critical bug in your project. Very much an edge case :) Thanks for responding.

DavidLP commented 7 years ago

Ok :-). I will put this on may to do list for the next release. To test the functionality of pyLandau I might also consider hypothesis. Did not know about it. THX.

DavidLP commented 6 years ago

A parameter difference over 9 orders of magnitude reaches the numerical precision of all scipy minimizers that I tested. I added unit tests with hypothesis and determined parameter ranges where sane results with no crashes can be expected. These are listed here. All fitting and unit test functions should obey to these limits. When the code with limits is released this issue will be closed.

stevemolloy commented 6 years ago

Sounds good. Thanks for keeping me up to date.

cbespin commented 2 years ago

This should be fixed with the current release that includes the fixes from @DavidLP. Closing the issue now, feel free to reopen/open another one if the problem persists.