8000 gh-115532: Add kernel density estimation to the statistics module by rhettinger · Pull Request #115863 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-115532: Add kernel density estimation to the statistics module #115863

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

Merged
merged 19 commits into from
Feb 25, 2024
Merged
Changes from 1 commit
Commits
File filter 8000

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
Prev Previous commit
Next Next commit
Add PDF area test
  • Loading branch information
rhettinger committed Feb 23, 2024
commit e9386edcf8d7631428a55327671cb045dc4a5386
24 changes: 24 additions & 0 deletions Lib/test/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2353,6 +2353,30 @@ def test_mixed_int_and_float(self):
self.assertAlmostEqual(actual_mean, expected_mean, places=5)


class TestKDE(unittest.TestCase):

def test_kde_area(self):
# The approximate integral of a PDF should be close to 1.0

sample = [-2.1, -1.3, -0.4, 1.9, 5.1, 6.2]

def integrate(func, low, high, steps=10_000):
"Numeric approximation of a definite function integral."
width = (high - low) / steps
xarr = (low + width/2 + width * i for i in range(steps))
return sum(map(func, xarr)) * width

kernels = ['normal', 'gauss', 'logistic', 'sigmoid', 'rectangular',
'uniform', 'triangular', 'parabolic', 'epanechnikov',
'quartic', 'biweight', 'triweight', 'cosine']

for kernel in kernels:
with self.subTest(kernel=kernel):
f_hat = statistics.kde(sample, h=1.5, kernel=kernel)
area = integrate(f_hat, -20, 20)
self.assertAlmostEqual(area, 1.0, places=4)


class TestQuantiles(unittest.TestCase):

def test_specific_cases(self):
3A25 Expand Down
0