Implementation of Landau distribution into scipy

Hi, I am new here, though have been a SciPy user for several years. I am working on solid state radiation detectors and often need to use the [Landau distribution](https://es.wikipedia.org/wiki/Distribuci%C3%B3n_de_Landau) to model my data. Since I could not find it in Python I implemented it myself (see here https://github.com/SengerM/landaupy). It is based on the Root implementation, still it is a "pure Python" version only using scipy and numpy. Now I would like to add it into SciPy so it is there for other people as well. I would like to do this myself mainly for two reasons, one is to give back a grain of salt to SciPy and the other is to actually contribute to a large open source project, which I've never done before but always wanted to. I am following the steps listed in the [contributor quickstart guide](https://scipy.github.io/devdocs/dev/dev_quickstart.html). I run into [an issue](https://github.com/scipy/scipy/issues/18390) which will hopefully be solved but while asking for help about that I was suggested to propose this feature here before sitting to work on it. Please let me know your thoughts. Kind regards, Matias.

On 4/30/23, Matias Senger <m.senger@hotmail.com> wrote:
Hi,
I am new here, though have been a SciPy user for several years. I am working on solid state radiation detectors and often need to use the [Landau distribution](https://es.wikipedia.org/wiki/Distribuci%C3%B3n_de_Landau) to model my data. Since I could not find it in Python I implemented it myself (see here https://github.com/SengerM/landaupy). It is based on the Root implementation, still it is a "pure Python" version only using scipy and numpy.
Now I would like to add it into SciPy so it is there for other people as well. I would like to do this myself mainly for two reasons, one is to give back a grain of salt to SciPy and the other is to actually contribute to a large open source project, which I've never done before but always wanted to. I am following the steps listed in the [contributor quickstart guide](https://scipy.github.io/devdocs/dev/dev_quickstart.html). I run into [an issue](https://github.com/scipy/scipy/issues/18390) which will hopefully be solved but while asking for help about that I was suggested to propose this feature here before sitting to work on it.
Please let me know your thoughts.
Thanks for your interest in contributing to SciPy, Matias. The ROOT library uses the LGPL license, and if your code is derived from the ROOT code, it must also use the LGPL license. The LGPL license is not compatible with SciPy's license, so we would not be able to include your version in SciPy. (Disclaimer: I am not a lawyer; this is just my understanding of the situation.) The Landau distribution is a special case of Levy's stable distribution, which is available in SciPy as `scipy.stats.levy_stable`. The Landau distribution is obtained from `levy_stable` by setting both shape parameters alpha and beta to 1. That leaves the location and scale parameters still available to adjust the location and scale of the Landau distribution. It might be worthwhile to have a specialized implementation specifically for the Landau distribution. The specialized code could probably be made much faster and/or more accurate than the implementation of `levy_stable`. It would be good to get more feedback from users (probably mostly physicists) on whether there is demand for it. But if we do, the implementation cannot be derived from the ROOT implementation. Warren
Kind regards, Matias. _______________________________________________ SciPy-Dev mailing list -- scipy-dev@python.org To unsubscribe send an email to scipy-dev-leave@python.org https://mail.python.org/mailman3/lists/scipy-dev.python.org/ Member address: warren.weckesser@gmail.com

I submitted a pull request with an implementation of the Landau distribution, see https://github.com/scipy/scipy/pull/19145. It is not based on the ROOT implementation but on a paper, and the code if you look at it is actually very different, so I hope there are no legal problems. I have been comparing it against the Lévy stable with alpha=beta=1 and found that the Landau implementation is approximately 1500 times faster when calculating the PDF, which actually makes it suitable to fit histograms, and both PDF and CDF seem to be more accurate than the Lévy stable alternative for large values of x, as there are no "discrete jumps" in this region. However, I am not sure about how to test the accuracy in an absolute way, I am just comparing it against the Lévy stable implementation, as you can see in the unittests in my pull request. As a physicist working with radiation detectors, I am in favor of adding it to SciPy. There are mainly two reasons I see: 1) Many colleagues don't seem to know that the Landau is a special case of the Lévy stable distribution (I was one of them for a large fraction of my PhD). If you search for things like "landau distribution python" on the web, you find either implementations that are not in SciPy, or are suggested to use the Moyal distribution, which is not the same. But the Lévy stable distribution appears nowhere in the first page of the search results. As a consequence, many colleagues decide not to use Python simply because they cannot easily find the distribution. This could, in principle, be solved simply by adding some comment to the documentation of the Lévy stable mentioning that the Landau is just a special case of it. 2) After finding out that the Landau is just the Lévy stable with alpha=beta=1, you discover that the calculation of the PDF is so slow that actually it is useless for many applications commonly found in the lab. On top of this, the current implementation of Lévy stable when alpha=beta=1 has a bug in the scale parameter, see https://github.com/scipy/scipy/issues/19140, but let's hope it is solve soon so it should no be a reason in favor of an implementation of the Landau distribution. Please let me know your thoughts on this. Matias.
participants (2)
-
Matias Senger
-
Warren Weckesser