# Which is the formula from Silverman to calculate the bandwidth in a kernel density estimation?

I want to calculate a better bandwidh for my kernel density estimator, which is an Epanechnikov. I use Silverman's formula which involves the standard deviation of the sample, the sample size and a constant, but I'm getting a very smooth curve in most cases and I would prefer if it were more balanced. Thank you for any help you can give me.

• What is Silverman's formula, exactly?
– whuber
Jan 28 '11 at 19:15

To shamelessly quote the Stata manual entry for kdensity:

The optimal width is the width that would minimize the mean integrated squared error if the data were Gaussian and a Gaussian kernel were used, so it is not optimal in any global sense. In fact, for multimodal and highly skewed densities, this width is usually too wide and oversmooths the density (Silverman 1992).

Silverman, B. W. 1992. Density Estimation for Statistics and Data Analysis. London: Chapman & Hall. ISBN 9780412246203

The formula Stata give for the optimal bandwidth $h$ is:
$$h = \frac{0.9m}{n^{1/5}} \quad \mbox{with } m = \min\left(\sqrt{\operatorname{Var}(X)},\frac{\operatorname{IQR}(X)}{1.349}\right),$$ where $n$ is the number of observations on $X$, $\operatorname{Var}(X)$ is its variance and $\operatorname{IQR}(X)$ its interquartile range.

• For anyone else wondering, $\frac{\operatorname{IQR}(X)}{1.349}$ is the F-pseudosigma. That $m$ value is less susceptible to outliers than always using the variance. Jun 13 '18 at 1:17

I asked a similar question a few months ago. Rob Hyndman provided an excellent answer that recommends the Sheather-Jones method.

One addition point. In R, for the density function, you set the bandwidth explicitly via the bw argument. However, I often find that the adjust argument is more helpful. The adjust argument scales the value of the bandwidth. So adjust=2 means double the bandwidth.

I second @onestop, but quote Wilcox, 'Introduction to Robust Estimation and Hypothesis Testing', 2nd edition, page 50: $$h = 1.06\frac{A}{n^{1/5}}, \qquad A = \min{\left(s,\frac{IQR(x)}{1.34}\right)},$$ where $s$ is the sample standard deviation.

• The two formulas are essentially the same: 1.34 is 1.349 rounded down and 0.9 has been increased to 1.06. But that change makes little difference in the appearance of smoothness, so we might just as well recognize what's going on and provide a simpler formula: divide the smaller of two estimates of the standard deviation by the fifth root of the number of observations. The two estimates are the sample standard deviation, s, and a rescaled IQR (which agrees with s asymptotically for normal distributions).
– whuber
Jan 28 '11 at 19:14
• @whuber: yes indeed, the two answers are the same up to a factor of about 15% for any set of data. I am not sure the constant factor in front is irrelevant, however! Jan 28 '11 at 19:49
• Given that these formulas are approximate and have optimal properties only (a) asymptotically and (b) for Normally distributed data, a 15% difference is immaterial. Remember, too, that they are "optimal" only in the sense of reducing mean squared error, which tends to impose a lot of smoothness. They are not necessarily optimal for other purposes. That's why the OP should feel free to depart from such recommendations: they should be considered initial values only.
– whuber
Jan 28 '11 at 21:36

What I usually do is to calculate the plugin bandwidth using Silverman's formula (h_p) and then crossvalidate in the range of [h_p/5, 5h_p] to find the optimal bandwidth. This crossvalidation can be done either by using leave-one-out least squares crossvalidation or by leave-one-out-likelihood crossvalidation.