Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
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.
  • Loading branch information
jackoconnordev committed Jul 25, 2025
commit 3ef5264f614bbb64b59fdc52e8f765ad3c9267fe
2 changes: 1 addition & 1 deletion stdlib/src/statistics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod _statistics {
// See https://github.com/python/cpython/blob/6846d6712a0894f8e1a91716c11dd79f42864216/Modules/_statisticsmodule.c#L28-L120
#[allow(clippy::excessive_precision)]
fn normal_dist_inv_cdf(p: f64, mu: f64, sigma: f64) -> Option<f64> {
if p <= 0.0 || p >= 1.0 || sigma <= 0.0 {
if p <= 0.0 || p >= 1.0 {
return None;
}

Expand Down
Loading