-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
maths/gaussian.py is wrong #2212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@QuantumNovice Could we create a doctest with one of these to prove this claim: |
Defining it as the OP did, gives (in Python 3.8.2 shell) exactly the same results as in the docstring of maths/gaussian.py:
None of the search results linked by @cclauss calculates the same function. I was able to find an implementation from the authors of the famous Algorithms textbooks (Sedgewick et al): This also gives exactly the same results as the docstring of maths/gaussian.py (with the exception of supporting NumPy arrays of course, and the Overflow error):
So... maths/gaussian.py seems correct, but the OP might be right about the performance (unnecessary squaring and square-rooting). |
Thanks for the explanation. Should we close? @stkain, your sense? |
Hello folks, @spamegg1 's calls gave the same results because sigma was never specified (defaulting to 1.0) Try something like:
in ipython or jupyter Bye, |
Correction. The prefixes for numpy and pyplot have to be explicitly specified.
|
Hmm OK. In that case the docstring provides insufficient testing. It should specify some different sigma values. The only case with a specified sigma value returns 0. Let me compare maths/gaussian.py against Sedgewick's implementation again:
They are indeed different! Adding parentheses around 2 * sigma ** 2 fixes it:
Strangely, for larger values they are the same (I guess the difference is further down the decimals):
OK, so I propose that the implementation in maths/gaussian.py should just add parentheses around the 2 * sigma ** 2 (then it gives the correct answer), and add some docstring cases with small sigma values that are different than 1. If the OP would like to make a PR that is fine. @cclauss You can close it once someone makes a PR. |
#2249 was merged in, should this be closed now? |
Hi, thx. Yes, the message can be closed. Bye, |
Hi,
the formula is wrong.
Instead, for example:
avoids squaring sigma and taking the sqrt again in the factor before the exponential
and first calculating (x-mu) / sigma and then squaring saves you squaring numerator and denominator separately
in the exponential.
Bye,
Stefan
The text was updated successfully, but these errors were encountered: