From 7f11632556b7a0a6248b08f471c06de3eb22452d Mon Sep 17 00:00:00 2001 From: ThibaultDECO <80053070+ThibaultDECO@users.noreply.github.com> Date: Sun, 12 May 2024 00:47:01 +0200 Subject: [PATCH] Update statistics.py Faster Faster --- Lib/statistics.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/statistics.py b/Lib/statistics.py index c2f4fe8e054d3d..bcf4bd506ef40a 100644 --- a/Lib/statistics.py +++ b/Lib/statistics.py @@ -145,6 +145,7 @@ from collections import Counter, namedtuple, defaultdict _SQRT2 = sqrt(2.0) +_SQRT1_2 = sqrt(0.5) _random = random # === Exceptions === @@ -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': @@ -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