I was looking at the formula for the Kernel Density Estimator:

enter image description here

I have always considered the main purpose of the Kernel Density Estimator to provide an estimate of the density at some point "x" (e.g. f(x = 1.2) ). But are there any formulas which can be used to calculate the "expected value" of the density function estimated by the Kernel Density Estimator?

For example, if we had some random observations and the Kernel Density Estimator produced the following estimation of the density function :

enter image description here

Could the Kernel Density Estimator tell us something about the expected value of this function? If we were to take the arithmetic mean of this data, it would not be very "representative" of the data. In such examples, can estimators be modified to provide more "relevant" estimates of the mean?


  • $\begingroup$ Why don’t you think the arithmetic mean is representative of the data? $\endgroup$
    – Dave
    Nov 23 '21 at 2:54
  • $\begingroup$ This is straightforward. The expected value of a kernel centered at $x_i$ is $x_i$ (for any zero-mean symmetric kernel with finite variance). Hence the expectation of the KDE (which is a mixture density over such kernels) is exactly equal to the sample mean, irrespective of the choice of kernel. Not sure why you don't like it as an answer but it's exactly what you get with a KDE $\endgroup$
    – Glen_b
    Nov 23 '21 at 6:29
  • $\begingroup$ @ Gleb_b : thank you for your answer! I just wasn't sure if this was correct! $\endgroup$
    – stats555
    Nov 23 '21 at 6:31

I can propose something, the idea is to use an optimization method based on the KDE function. Here is a short example in R:

First I define a function to calculate a KDE (most of this code in taken from a personnal project on github (JeremyGelb/spNetwork)

quartic_kernel <- function(d, bw){
  u <- d/bw
  k <- (15/16)*(1-u**2)**2
  k <- k / bw
  k <- ifelse(abs(d)>bw,0,k)

kde <- function(samples, events, bw){
  ## calculating the kernel values with the bw
  kernel_values <- sapply(samples, function(s){
    dists <- abs(s-events)

Then I will create a new bimodal variable.

h1 <- rnorm(100, 10, 1)
h2 <- rnorm(50, 5, 1)
h<- c(h1,h2)

hist(h, breaks = 20)

I will use a simple approach in R to select the bandwidth and display the estimated densities.

d1 <- density(h, bw = "nrd0", adjust = 1,
        kernel = c("gaussian"))

hist(h, breaks = 20, freq = FALSE)
lines(d1, col = "blue")

bw <- d1$bw

enter image description here

And now I can use an optimization function to find the most likely value

optim(0, fn = kde, control = list(fnscale = -1), 
      events = h, bw = bw, method = "Brent",
      lower = -10, upper = 20

I get a value close to 10, which is indeed the value with the highest density in the chart.

To find the value that is associated with a specified density, one could adapt the function to be optimized but keep in mind that several values can have the same density.

kde2 <- function(sample, events, bw, val){
  dens <- kde(sample, events, bw)

optim(0, fn = kde2,
      events = df, bw = bw, method = "Brent",
      lower = -10, upper = 20,
      val = 0.10

This gives me something close to 8.

I hope it will help.

  • $\begingroup$ @ jeremy gelb: thank you for your answer! I will have to study it more closely and ask you questions about it! $\endgroup$
    – stats555
    Nov 23 '21 at 6:31
  • $\begingroup$ No problem, It is more a technical answer rather than a theoritical one $\endgroup$ Nov 23 '21 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.