Skip to content

Commit

Permalink
Update statistics.py Faster
Browse files Browse the repository at this point in the history
Faster
  • Loading branch information
ThibaultDECO committed May 11, 2024
1 parent abead54 commit 7f11632
Showing 1 changed file with 4 additions and 3 deletions.
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

0 comments on commit 7f11632

Please sign in to comment.