Skip to content

Commit 0400a7f

Browse files
authored
Minor code cleanups for statistics (GH-19873)
* Minor cleanups: Removed unused code. Move C import near its Python version. * Clean-up whitespace
1 parent 8aab843 commit 0400a7f

File tree

1 file changed

+7
-76
lines changed

1 file changed

+7
-76
lines changed

Lib/statistics.py

Lines changed: 7 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,13 @@ def _normal_dist_inv_cdf(p, mu, sigma):
894894
return mu + (x * sigma)
895895

896896

897+
# If available, use C implementation
898+
try:
899+
from _statistics import _normal_dist_inv_cdf
900+
except ImportError:
901+
pass
902+
903+
897904
class NormalDist:
898905
"Normal distribution of a random variable"
899906
# https://en.wikipedia.org/wiki/Normal_distribution
@@ -1111,79 +1118,3 @@ def __hash__(self):
11111118

11121119
def __repr__(self):
11131120
return f'{type(self).__name__}(mu={self._mu!r}, sigma={self._sigma!r})'
1114-
1115-
# If available, use C implementation
1116-
try:
1117-
from _statistics import _normal_dist_inv_cdf
1118-
except ImportError:
1119-
pass
1120-
1121-
1122-
if __name__ == '__main__':
1123-
1124-
# Show math operations computed analytically in comparsion
1125-
# to a monte carlo simulation of the same operations
1126-
1127-
from math import isclose
1128-
from operator import add, sub, mul, truediv
1129-
from itertools import repeat
1130-
import doctest
1131-
1132-
g1 = NormalDist(10, 20)
1133-
g2 = NormalDist(-5, 25)
1134-
1135-
# Test scaling by a constant
1136-
assert (g1 * 5 / 5).mean == g1.mean
1137-
assert (g1 * 5 / 5).stdev == g1.stdev
1138-
1139-
n = 100_000
1140-
G1 = g1.samples(n)
1141-
G2 = g2.samples(n)
1142-
1143-
for func in (add, sub):
1144-
print(f'\nTest {func.__name__} with another NormalDist:')
1145-
print(func(g1, g2))
1146-
print(NormalDist.from_samples(map(func, G1, G2)))
1147-
1148-
const = 11
1149-
for func in (add, sub, mul, truediv):
1150-
print(f'\nTest {func.__name__} with a constant:')
1151-
print(func(g1, const))
1152-
print(NormalDist.from_samples(map(func, G1, repeat(const))))
1153-
1154-
const = 19
1155-
for func in (add, sub, mul):
1156-
print(f'\nTest constant with {func.__name__}:')
1157-
print(func(const, g1))
1158-
print(NormalDist.from_samples(map(func, repeat(const), G1)))
1159-
1160-
def assert_close(G1, G2):
1161-
assert isclose(G1.mean, G1.mean, rel_tol=0.01), (G1, G2)
1162-
assert isclose(G1.stdev, G2.stdev, rel_tol=0.01), (G1, G2)
1163-
1164-
X = NormalDist(-105, 73)
1165-
Y = NormalDist(31, 47)
1166-
s = 32.75
1167-
n = 100_000
1168-
1169-
S = NormalDist.from_samples([x + s for x in X.samples(n)])
1170-
assert_close(X + s, S)
1171-
1172-
S = NormalDist.from_samples([x - s for x in X.samples(n)])
1173-
assert_close(X - s, S)
1174-
1175-
S = NormalDist.from_samples([x * s for x in X.samples(n)])
1176-
assert_close(X * s, S)
1177-
1178-
S = NormalDist.from_samples([x / s for x in X.samples(n)])
1179-
assert_close(X / s, S)
1180-
1181-
S = NormalDist.from_samples([x + y for x, y in zip(X.samples(n),
1182-
Y.samples(n))])
1183-
assert_close(X + Y, S)
1184-
1185-
S = NormalDist.from_samples([x - y for x, y in zip(X.samples(n),
1186-
Y.samples(n))])
1187-
assert_close(X - Y, S)
1188-
1189-
print(doctest.testmod())

0 commit comments

Comments
 (0)