I have some troble understanding the Kernel density estimation.

If a consider the next example:

for(i in seq(min(z$x),max(z$x),length.out = 10000))

I expected getting that s is approximately equals to one since I am considering an approximation for the integral of the Kernel density function (that I suppose is also a density). However, I don't get the expected result. Why it doesn's sum one? The idea behind this is that I wanted to use the Kernel as the density of my data points. I was interpolating to get my explicit function and use with other x values. Or do you know another way to get a density function estimated by fitting?

  • $\begingroup$ If you approximate an integral as a sum of rectangular areas, the width of the rectangle matters, not just its height. $\endgroup$
    – Glen_b
    Apr 17 '18 at 11:05

The kernel density works fine

x <- rnorm(25)
z <- density(x)
f <- function(i) splinefun(z$x, z$y)(i)
integrate(f, min(z$x), max(z$x))
## 1.000833 with absolute error < 7.8e-05

The problem is that kernel density is a function of continuous variable, and sum is not the same as an integral. Integral can be approximated by a Riemann sum, e.g. using trapezoidal rule

$$ \int_{a}^{b} f(x)\, dx \approx \frac{1}{2} \sum_{k=1}^{N} \left( x_{k+1} - x_{k} \right) \Big( f(x_{k+1}) + f(x_{k}) \Big) $$

but it's far from taking raw sum of $f(x_k)$. You cannot expect the sum to be equal to one even from the bare fact that probability density can be greater then one and summing things that can be greater then one, obviously leads to the sum that can greater then one.


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.