From 9ece723d5fd326359ac6729cbc008e7a3fdbd5b5 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sun, 2 Jun 2024 14:10:31 -0500 Subject: [PATCH 1/3] Stronger cdf/invcdf roundtrip test --- Lib/test/test_statistics.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/test/test_statistics.py b/Lib/test/test_statistics.py index 0b28459f03d86a..3cc735b4e22f8e 100644 --- a/Lib/test/test_statistics.py +++ b/Lib/test/test_statistics.py @@ -2440,12 +2440,15 @@ def test_kde_kernel_invcdfs(self): # Verify that cdf / invcdf will round trip xarr = [i/100 for i in range(-100, 101)] + parr = [i/1000 + 5/10000 for i in range(1000)] for kernel, spec in kernel_specs.items(): invcdf = spec['invcdf'] with self.subTest(kernel=kernel): cdf = kde([0.0], h=1.0, kernel=kernel, cumulative=True) for x in xarr: self.assertAlmostEqual(invcdf(cdf(x)), x, places=6) + for p in parr: + self.assertAlmostEqual(cdf(invcdf(p)), p, places=11) @support.requires_resource('cpu') def test_kde_random(self): From 12e5dd8e024c97233050743015be0702b174013b Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sun, 2 Jun 2024 14:21:27 -0500 Subject: [PATCH 2/3] Remove kde() dependency to isolate the kernel tests --- Lib/test/test_statistics.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_statistics.py b/Lib/test/test_statistics.py index 3cc735b4e22f8e..60bcc926399fce 100644 --- a/Lib/test/test_statistics.py +++ b/Lib/test/test_statistics.py @@ -2434,17 +2434,18 @@ def integrate(func, low, high, steps=10_000): data.append(100) self.assertGreater(f_hat(100), 0.0) - def test_kde_kernel_invcdfs(self): + def test_kde_kernel_specs(self): + # White-box test for the kernel formulas in isolation from + # their downstream use in kde() and kde_random() kernel_specs = statistics._kernel_specs - kde = statistics.kde # Verify that cdf / invcdf will round trip xarr = [i/100 for i in range(-100, 101)] parr = [i/1000 + 5/10000 for i in range(1000)] - for kernel, spec in kernel_specs.items(): + for kernel, spec kernel_specs.items(): + cdf = spec['cdf'] invcdf = spec['invcdf'] with self.subTest(kernel=kernel): - cdf = kde([0.0], h=1.0, kernel=kernel, cumulative=True) for x in xarr: self.assertAlmostEqual(invcdf(cdf(x)), x, places=6) for p in parr: From bf8197caa45d7bface2d32f67246ac0373341aa3 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 14 Jun 2024 09:56:01 -0500 Subject: [PATCH 3/3] Restore "in". --- Lib/test/test_statistics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_statistics.py b/Lib/test/test_statistics.py index 60bcc926399fce..c374c947e02a6b 100644 --- a/Lib/test/test_statistics.py +++ b/Lib/test/test_statistics.py @@ -2442,7 +2442,7 @@ def test_kde_kernel_specs(self): # Verify that cdf / invcdf will round trip xarr = [i/100 for i in range(-100, 101)] parr = [i/1000 + 5/10000 for i in range(1000)] - for kernel, spec kernel_specs.items(): + for kernel, spec in kernel_specs.items(): cdf = spec['cdf'] invcdf = spec['invcdf'] with self.subTest(kernel=kernel):