Skip to content

Commit 63da5cc

Browse files
authored
gh-132893: More accurate CDF computation (gh-132895)
1 parent b1fc8b6 commit 63da5cc

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

Lib/statistics.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
from decimal import Decimal
139139
from itertools import count, groupby, repeat
140140
from bisect import bisect_left, bisect_right
141-
from math import hypot, sqrt, fabs, exp, erf, tau, log, fsum, sumprod
141+
from math import hypot, sqrt, fabs, exp, erfc, tau, log, fsum, sumprod
142142
from math import isfinite, isinf, pi, cos, sin, tan, cosh, asin, atan, acos
143143
from functools import reduce
144144
from operator import itemgetter
@@ -811,8 +811,9 @@ def deco(builder):
811811
def normal_kernel():
812812
sqrt2pi = sqrt(2 * pi)
813813
sqrt2 = sqrt(2)
814+
neg_sqrt2 = -sqrt2
814815
pdf = lambda t: exp(-1/2 * t * t) / sqrt2pi
815-
cdf = lambda t: 1/2 * (1.0 + erf(t / sqrt2))
816+
cdf = lambda t: 1/2 * erfc(t / neg_sqrt2)
816817
invcdf = lambda t: _normal_dist_inv_cdf(t, 0.0, 1.0)
817818
support = None
818819
return pdf, cdf, invcdf, support
@@ -1257,7 +1258,7 @@ def cdf(self, x):
12571258
"Cumulative distribution function. P(X <= x)"
12581259
if not self._sigma:
12591260
raise StatisticsError('cdf() not defined when sigma is zero')
1260-
return 0.5 * (1.0 + erf((x - self._mu) / (self._sigma * _SQRT2)))
1261+
return 0.5 * erfc((self._mu - x) / (self._sigma * _SQRT2))
12611262

12621263
def inv_cdf(self, p):
12631264
"""Inverse cumulative distribution function. x : P(X <= x) = p
@@ -1311,7 +1312,7 @@ def overlap(self, other):
13111312
dv = Y_var - X_var
13121313
dm = fabs(Y._mu - X._mu)
13131314
if not dv:
1314-
return 1.0 - erf(dm / (2.0 * X._sigma * _SQRT2))
1315+
return erfc(dm / (2.0 * X._sigma * _SQRT2))
13151316
a = X._mu * Y_var - Y._mu * X_var
13161317
b = X._sigma * Y._sigma * sqrt(dm * dm + dv * log(Y_var / X_var))
13171318
x1 = (a + b) / dv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improved the accuracy of ``statistics.NormalDist.cdf`` for negative inputs.

0 commit comments

Comments
 (0)