# Can you take the Expected Value of a Kernel Density Estimates?

I was looking at the formula for the Kernel Density Estimator: 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 : 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?

Thanks

• Why don’t you think the arithmetic mean is representative of the data?
– Dave
Nov 23 at 2:54
• 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 Nov 23 at 6:29
• @ Gleb_b : thank you for your answer! I just wasn't sure if this was correct! Nov 23 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)
return(k)
}

kde <- function(samples, events, bw){

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

return(1/(bw*length(events))*kernel_values)
}


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 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)
return((dens-val)**2)
}

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.

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