@@ -1094,17 +1094,15 @@ from a fixed number of discrete samples.
1094
1094
The basic idea is to smooth the data using `a kernel function such as a
1095
1095
normal distribution, triangular distribution, or uniform distribution
1096
1096
<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 * .
1099
1099
1100
1100
.. testcode ::
1101
1101
1102
- import math
1103
-
1104
1102
def kde_normal(sample, h):
1105
1103
"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
1108
1106
n = len(sample)
1109
1107
def pdf(x):
1110
1108
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:
1118
1116
.. doctest ::
1119
1117
1120
1118
>>> 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 )
1122
1120
>>> xarr = [i/ 100 for i in range (- 750 , 1100 )]
1123
1121
>>> yarr = [f_hat(x) for x in xarr]
1124
1122
0 commit comments