@@ -1104,17 +1104,15 @@ from a fixed number of discrete samples.
1104
1104
The basic idea is to smooth the data using `a kernel function such as a
1105
1105
normal distribution, triangular distribution, or uniform distribution
1106
1106
<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 * .
1109
1109
1110
1110
.. testcode ::
1111
1111
1112
- import math
1113
-
1114
1112
def kde_normal(sample, h):
1115
1113
"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
1118
1116
n = len(sample)
1119
1117
def pdf(x):
1120
1118
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:
1128
1126
.. doctest ::
1129
1127
1130
1128
>>> 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 )
1132
1130
>>> xarr = [i/ 100 for i in range (- 750 , 1100 )]
1133
1131
>>> yarr = [f_hat(x) for x in xarr]
1134
1132
0 commit comments