Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stronger tests for the statistics kernel formulas #120506

Merged
merged 4 commits into from
Jun 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Lib/test/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2434,18 +2434,22 @@ 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():
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):
Expand Down
Loading