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

Skip to content

Commit 258f301

Browse files
rhettingermiss-islington
authored andcommitted
Update KDE recipe to match the standard use of the h parameter (gh-GH-113958)
(cherry picked from commit 2f126a7) Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
1 parent 5c23822 commit 258f301

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
@@ -1094,17 +1094,15 @@ from a fixed number of discrete samples.
10941094
The basic idea is to smooth the data using `a kernel function such as a
10951095
normal distribution, triangular distribution, or uniform distribution
10961096
<https://en.wikipedia.org/wiki/Kernel_(statistics)#Kernel_functions_in_common_use>`_.
1097-
The degree of smoothing is controlled by a single
1098-
parameter, ``h``, representing the variance of the kernel function.
1097+
The degree of smoothing is controlled by a scaling parameter, ``h``,
1098+
which is called the *bandwidth*.
10991099

11001100
.. testcode::
11011101

1102-
import math
1103-
11041102
def kde_normal(sample, h):
11051103
"Create a continuous probability density function from a sample."
1106-
# Smooth the sample with a normal distribution of variance h.
1107-
kernel_h = NormalDist(0.0, math.sqrt(h)).pdf
1104+
# Smooth the sample with a normal distribution kernel scaled by h.
1105+
kernel_h = NormalDist(0.0, h).pdf
11081106
n = len(sample)
11091107
def pdf(x):
11101108
return sum(kernel_h(x - x_i) for x_i in sample) / n
@@ -1118,7 +1116,7 @@ a probability density function estimated from a small sample:
11181116
.. doctest::
11191117

11201118
>>> sample = [-2.1, -1.3, -0.4, 1.9, 5.1, 6.2]
1121-
>>> f_hat = kde_normal(sample, h=2.25)
1119+
>>> f_hat = kde_normal(sample, h=1.5)
11221120
>>> xarr = [i/100 for i in range(-750, 1100)]
11231121
>>> yarr = [f_hat(x) for x in xarr]
11241122

0 commit comments

Comments
 (0)
0