Skip to content

Commit af0f038

Browse files
rhettingerSonicField
authored andcommitted
Minor edit: Simplify and tighten the distribution test (pythongh-118585)
Simplify and tighten the distribution test
1 parent 73cee61 commit af0f038

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

Lib/test/test_statistics.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2482,29 +2482,30 @@ def test_kde_random(self):
24822482
# Approximate distribution test: Compare a random sample to the expected distribution
24832483

24842484
data = [-2.1, -1.3, -0.4, 1.9, 5.1, 6.2, 7.8, 14.3, 15.1, 15.3, 15.8, 17.0]
2485+
xarr = [x / 10 for x in range(-100, 250)]
24852486
n = 1_000_000
24862487
h = 1.75
24872488
dx = 0.1
24882489

2489-
def p_expected(x):
2490-
return F_hat(x + dx) - F_hat(x - dx)
2491-
24922490
def p_observed(x):
2493-
# P(x-dx <= X < x+dx) / (2*dx)
2494-
i = bisect.bisect_left(big_sample, x - dx)
2495-
j = bisect.bisect_right(big_sample, x + dx)
2491+
# P(x <= X < x+dx)
2492+
i = bisect.bisect_left(big_sample, x)
2493+
j = bisect.bisect_left(big_sample, x + dx)
24962494
return (j - i) / len(big_sample)
24972495

2496+
def p_expected(x):
2497+
# P(x <= X < x+dx)
2498+
return F_hat(x + dx) - F_hat(x)
2499+
24982500
for kernel in kernels:
24992501
with self.subTest(kernel=kernel):
25002502

2501-
F_hat = statistics.kde(data, h, kernel, cumulative=True)
25022503
rand = kde_random(data, h, kernel, seed=8675309**2)
25032504
big_sample = sorted([rand() for i in range(n)])
2505+
F_hat = statistics.kde(data, h, kernel, cumulative=True)
25042506

2505-
for x in range(-40, 190):
2506-
x /= 10
2507-
self.assertTrue(math.isclose(p_observed(x), p_expected(x), abs_tol=0.001))
2507+
for x in xarr:
2508+
self.assertTrue(math.isclose(p_observed(x), p_expected(x), abs_tol=0.0005))
25082509

25092510

25102511
class TestQuantiles(unittest.TestCase):

0 commit comments

Comments
 (0)