8000 Update KDE recipe to match the standard use of the h parameter (gh-#1… · python/cpython@2f126a7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2f126a7

Browse files
authored
Update KDE recipe to match the standard use of the h parameter (gh-#113958)
1 parent 2e7577b commit 2f126a7

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

Doc/library/statistics.rst

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,17 +1104,15 @@ from a fixed number of discrete samples.
11041104
The basic idea is to smooth the data using `a kernel function such as a
11051105
normal distribution, triangular distribution, or uniform distribution
11061106
<https://en.wikipedia.org/wiki/Kernel_(statistics)#Kernel_functions_in_common_use>`_.
1107-
The degree of smoothing is controlled by a single
1108-
parameter, ``h``, representing the variance of the kernel function.
1107+
The degree of smoothing is controlled by a scaling parameter, ``h``,
1108+
which is called the *bandwidth*.
11091109

11101110
.. testcode::
11111111

1112-
import math
1113-
11141112
def kde_normal(sample, h):
11151113
"Create a continuous probability density function from a sample."
1116-
# Smooth the sample with a normal distribution of variance h.
1117-
kernel_h = NormalDist(0.0, math.sqrt(h)).pdf
1114+
# Smooth the sample with a normal distribution kernel scaled by h.
1115+
kernel_h = NormalDist(0.0, h).pdf
11181116
n = len(sample)
11191117
def pdf(x):
11201118
return sum(kernel_h(x - x_i) for x_i in sample) / n
@@ -1128,7 +1126,7 @@ a probability density function estimated from a small sample:
11281126
.. doctest::
11291127

11301128
>>> sample = [-2.1, -1.3, -0.4, 1.9, 5.1, 6.2]
1131-
>>> f_hat = kde_normal(sample, h=2.25)
1129+
>>> f_hat = kde_normal(sample, h=1.5)
11321130
>>> xarr = [i/100 for i in range(-750, 1100)]
11331131
>>> yarr = [f_hat(x) for x in xarr]
11341132

0 commit comments

Comments
 (0)
0