diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2021-11-09 16:30:06 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-09 16:30:06 (GMT) |
commit | c3bc0fe5a6e4ee50bd186eebb638e881b1c4bf54 (patch) | |
tree | 99ca57f65b1bfbfd2efbac9c1341353e3cb25021 /Lib/statistics.py | |
parent | 5b7c7cb104163a178e9d70cb3c80cbfa6af8fbfc (diff) | |
download | cpython-c3bc0fe5a6e4ee50bd186eebb638e881b1c4bf54.zip cpython-c3bc0fe5a6e4ee50bd186eebb638e881b1c4bf54.tar.gz cpython-c3bc0fe5a6e4ee50bd186eebb638e881b1c4bf54.tar.bz2 |
Factor-out constant calculation. (GH-29491)
Diffstat (limited to 'Lib/statistics.py')
-rw-r--r-- | Lib/statistics.py | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/Lib/statistics.py b/Lib/statistics.py index 13e5fe7..e67c517 100644 --- a/Lib/statistics.py +++ b/Lib/statistics.py @@ -139,6 +139,8 @@ from math import hypot, sqrt, fabs, exp, erf, tau, log, fsum from operator import itemgetter, mul from collections import Counter, namedtuple +_SQRT2 = sqrt(2.0) + # === Exceptions === class StatisticsError(ValueError): @@ -1102,7 +1104,7 @@ class NormalDist: "Cumulative distribution function. P(X <= x)" if not self._sigma: raise StatisticsError('cdf() not defined when sigma is zero') - return 0.5 * (1.0 + erf((x - self._mu) / (self._sigma * sqrt(2.0)))) + return 0.5 * (1.0 + erf((x - self._mu) / (self._sigma * _SQRT2))) def inv_cdf(self, p): """Inverse cumulative distribution function. x : P(X <= x) = p @@ -1158,7 +1160,7 @@ class NormalDist: dv = Y_var - X_var dm = fabs(Y._mu - X._mu) if not dv: - return 1.0 - erf(dm / (2.0 * X._sigma * sqrt(2.0))) + return 1.0 - erf(dm / (2.0 * X._sigma * _SQRT2)) a = X._mu * Y_var - Y._mu * X_var b = X._sigma * Y._sigma * sqrt(dm * dm + dv * log(Y_var / X_var)) x1 = (a + b) / dv |