0
$\begingroup$

I have some troble understanding the Kernel density estimation.

If a consider the next example:

s<-0
z<-density(rnorm(25))
f<-function(i)approx(z$x,z$y,xout=i)$y
for(i in seq(min(z$x),max(z$x),length.out = 10000))
s<-s+f(i)
s

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?

$\endgroup$
  • $\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 -Reinstate Monica Apr 17 '18 at 11:05
1
$\begingroup$

The kernel density works fine

set.seed(123)
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.

$\endgroup$

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.