# What differs between mean and kernel density estimation to determine the densest point of a dataset?

I'm trying to calculate the densest cluster of values along an x axis. I recently discovered Kernel Density Estimation, before hand I was just simply averaging all the values along the x axis to try and determine the densest point. Considering mean finds the central tendency which is also influenced by the densest cluster of values, how would kernel density estimation and mean differ in finding the densest point?

When I looked "kernel density estimation" and "mean" up as keywords, I couldn't find any results discussing both topics. So I take it this is a very uneducated question. For that I apologise in advance. From what I understand, kde's primary focus is on probability while of course mean is just to determine general central tendencies. But it definitely intrigues me that they output similar estimations of what appear to be the densest point of a dataset. Perhaps outliers play a role here?

Thanks.

• Your "kernel density estimation" is close to the concept of the mode, which can be a different measure of location when compared to the mean. The R package modeest lists several different approaches for finding the mode or densest part of a sample Jun 1, 2020 at 14:19
• For a unimodally distributed population (rather than a sample), the difference between the mode and the mean can be up to $\sqrt{3}\approx 1.732$ standard deviations Jun 1, 2020 at 14:29
• Mode just determined the most common occurrence of a single value in a dataset right? The values in my dataset will always be different. That's why the kernel density estimation interested me because it seemed almost like a trimmed mean or something. Jun 1, 2020 at 14:34
• The point is that for a continuous random variable the sample values will almost always be distinct, but in a large sample they are likely to be more densely clustered near the highest point of the population density, i.e. at the mode, at least if the distribution is unimodal Jun 1, 2020 at 15:20

It seems you are asking how to find the 'mode' of a sample, based on a KDE of the sample. That is an interesting question. Fortunately, there is an easy answer.

As an example, consider the gamma distribution $$\mathsf{Gamma}(\mathrm{shape} = \alpha = 3, \mathrm{rate} = \lambda = 0.1),$$ which has mean $$\mu = \alpha/\lambda = 30$$ and mode $$(\alpha - 1)/\lambda = 20.$$ So this is a distribution in which the mean, median, and mode are all different.

Let's look at a histogram of $$n = 1000$$ random observations from this distribution. The tick marks at the horizontal axis show values of individual observations. The superimposed red curve is the default kernel density estimator in R.

set.seed(602)
x = rgamma(1000, 3, 0.1)
summary(x)
Min.  1st Qu.   Median     Mean  3rd Qu.     Max.
0.8087  17.3924  26.8166  30.4317  40.3331 121.5121

hist(x, prob=T, col="skyblue2",
main="Sample from GAMMA(3, .1)")
rug(x)
lines(density(x), type="l", lwd=2, col="red")


The procedure density returns a list of 512 (x,y)-pairs. We can retrieve the value on the horizontal axis where the vertical height of the KDE is highest. And we can view this as the 'sample mode' as determined by the KDE---here about 20, as anticipated.

h = density(x)$$y v = density(x)$$x
v[h==max(h)]
[1] 20.17976

• This is great, yeah i'd say the mode is a concrete observation. I came from an R background, but now moved to javascript for development. Formatting a kde in javascript is quite the task haha. Thanks for this Jun 2, 2020 at 21:11
• If you're going to write your own KDE, maybe look at some of Bernard Silverman's magnificently readable papers on density estimation. Notice that R allows changing bandwidth and also choice of shapes of ingredients merged to make a KDE. In my experience, adjusting bandwidth by halving or doubling has been good enough. // Sometimes, KDEs don't work so well near boundaries of support for the population distribution. Jun 2, 2020 at 21:31