Skip to content

Commit 55a09ed

Browse files
authored
Micro-optimizations for the statistics module (#152618)
1 parent 089e6f6 commit 55a09ed

1 file changed

Lines changed: 4 additions & 7 deletions

File tree

Lib/statistics.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ def _quartic_invcdf_estimate(p):
894894

895895
@register('quartic', 'biweight')
896896
def quartic_kernel():
897-
pdf = lambda t: 15/16 * (1.0 - t * t) ** 2
897+
pdf = lambda t: 15/16 * (u := 1.0 - t * t) * u
898898
cdf = lambda t: sumprod((3/16, -5/8, 15/16, 1/2),
899899
(t**5, t**3, t, 1.0))
900900
invcdf = _newton_raphson(_quartic_invcdf_estimate, f=cdf, f_prime=pdf)
@@ -1486,15 +1486,13 @@ def _sum(data):
14861486
"""
14871487
count = 0
14881488
types = set()
1489-
types_add = types.add
14901489
partials = {}
1491-
partials_get = partials.get
14921490

14931491
for typ, values in groupby(data, type):
1494-
types_add(typ)
1492+
types.add(typ)
14951493
for n, d in map(_exact_ratio, values):
14961494
count += 1
1497-
partials[d] = partials_get(d, 0) + n
1495+
partials[d] = partials.get(d, 0) + n
14981496

14991497
if None in partials:
15001498
# The sum will be a NAN or INF. We can ignore all the finite
@@ -1524,12 +1522,11 @@ def _ss(data, c=None):
15241522

15251523
count = 0
15261524
types = set()
1527-
types_add = types.add
15281525
sx_partials = defaultdict(int)
15291526
sxx_partials = defaultdict(int)
15301527

15311528
for typ, values in groupby(data, type):
1532-
types_add(typ)
1529+
types.add(typ)
15331530
for n, d in map(_exact_ratio, values):
15341531
count += 1
15351532
sx_partials[d] += n

0 commit comments

Comments
 (0)