Statistics inv_cdf sync with corresponding random module normal distributions#95265
Merged
rhettinger merged 2 commits intopython:mainfrom Jul 26, 2022
Merged
Statistics inv_cdf sync with corresponding random module normal distributions#95265rhettinger merged 2 commits intopython:mainfrom
rhettinger merged 2 commits intopython:mainfrom
Conversation
jackoconnordev
added a commit
to jackoconnordev/RustPython
that referenced
this pull request
Jul 25, 2025
See python/cpython#95265. To quote: > Restores alignment with random.gauss(mu, sigma) and random.normalvariate(mu, sigma) both. of which are equivalent to sampling from NormalDist(mu, sigma).inv_cdf(random()). The two functions in the random module happy accept sigma=0 and give a well-defined result. > This also lets the function gently handle a sigma getting smaller, eventually becoming zero. As sigma decrease, NormalDist(mu, sigma).inv_cdf(p) forms a tighter and tighter internal around mu and becoming exactly mu in the limit. For example, NormalDist(100, 1E-300).inv_cdf(0.3) cleanly evaluates to 100.0but withsigma=1e-500`` the function previously would raised an unexpected error.
youknowone
pushed a commit
to RustPython/RustPython
that referenced
this pull request
Jul 25, 2025
* Copy CPython 3.13 statistics module into RustPython * Adjust CPython "magic constants" in KDE tests ## test_kde I'm not too sure why but this one takes a few seconds to run the second for loop which calculates the cumulative distribution and does a rough calculation of the area under the curve. ## test_kde_random I have a lower bound for RustPython to sort a random list of 1_000_000 numbers on my laptop of > 1 hour. By dropping n to 30_000 sort will not take an egregious amount of time to run. It is then necessary to lower the tolerance for the math.isclose check, or the computed values may **randomly** fail due to the higher variance caused by the smaller sample size. * Reintroduce expected failure in test_statistics.TestNormalDict.test_slots * Sync Rust `normal_dist_inv_cdf` with Python equivalent See python/cpython#95265. To quote: > Restores alignment with random.gauss(mu, sigma) and random.normalvariate(mu, sigma) both. of which are equivalent to sampling from NormalDist(mu, sigma).inv_cdf(random()). The two functions in the random module happy accept sigma=0 and give a well-defined result. > This also lets the function gently handle a sigma getting smaller, eventually becoming zero. As sigma decrease, NormalDist(mu, sigma).inv_cdf(p) forms a tighter and tighter internal around mu and becoming exactly mu in the limit. For example, NormalDist(100, 1E-300).inv_cdf(0.3) cleanly evaluates to 100.0but withsigma=1e-500`` the function previously would raised an unexpected error.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Remove the inconsistent restriction that inv_cdf() is undefined when sigma is zero.
Aligns the C
_normal_dist_inv_cdf()function with its pure python equivalent.Restores the invariant that
NormalDist(mu, sigma).inv_cdf(p)is equivalent to `NormalDist(0.0, 1.0)inv_cdf(p) * sigma + mi``. In general, operations on NormalDist should always match the rescaled and reentered results for the same operation on the standard normal distribution.Restores alignment with
random.gauss(mu, sigma)andrandom.normalvariate(mu, sigma)both. of which are equivalent to sampling fromNormalDist(mu, sigma).inv_cdf(random()). The two functions in the random module happy accept sigma=0 and give a well-defined result.This also lets the function gently handle a sigma getting smaller, eventually becoming zero. As sigma decrease,
NormalDist(mu, sigma).inv_cdf(p)forms a tighter and tighter internal around mu and becoming exactly mu in the limit. For example,NormalDist(100, 1E-300).inv_cdf(0.3)cleanly evaluates to 100.0but withsigma=1e-500`` the function previously would raised an unexpected error.The only opposing idea is that
inv_cdf()means to give the inverse ofcdf(), but the cdf isn't defined with sigma=0. This is fine though. all supported inputs tocdf()are still invertible. There is just a relaxation of a restriction beyond the supported range which makesinv_cdf()obey other invariants and handle edge cases cleanly.The edit also gives a very small performance improvement.