Skip to content
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

Update statistics.py Faster implementation for normal distribution #118958

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
7 changes: 4 additions & 3 deletions Lib/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
from collections import Counter, namedtuple, defaultdict

_SQRT2 = sqrt(2.0)
_SQRT1_2 = sqrt(0.5)
_random = random

# === Exceptions ===
Expand Down Expand Up @@ -919,9 +920,9 @@ def kde(data, h, kernel='normal', *, cumulative=False):

case 'normal' | 'gauss':
sqrt2pi = sqrt(2 * pi)
sqrt2 = sqrt(2)
sqrt1_2 = sqrt(0.5)
K = lambda t: exp(-1/2 * t * t) / sqrt2pi
W = lambda t: 1/2 * (1.0 + erf(t / sqrt2))
W = lambda t: 0.5 * (1.0 + erf(t * sqrt1_2))
support = None

case 'logistic':
Expand Down Expand Up @@ -1533,7 +1534,7 @@ def cdf(self, x):
"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 * _SQRT2)))
return 0.5 * (1.0 + erf(((x - self._mu) * _SQRT1_2) / self._sigma))

def inv_cdf(self, p):
"""Inverse cumulative distribution function. x : P(X <= x) = p
Expand Down
Loading