From 41554ef0e0925695544d96a7bc49af1428d6bb6b Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 14 Jun 2024 10:21:35 -0500 Subject: [PATCH] Stronger tests for the statistics kernel formulas (gh-120506) --- Lib/test/test_statistics.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_statistics.py b/Lib/test/test_statistics.py index 0b28459f03d..c374c947e02 100644 --- a/Lib/test/test_statistics.py +++ b/Lib/test/test_statistics.py @@ -2434,18 +2434,22 @@ class TestKDE(unittest.TestCase): 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(): + 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: + self.assertAlmostEqual(cdf(invcdf(p)), p, places=11) @support.requires_resource('cpu') def test_kde_random(self):