# Quantile of kernel density estimator

I consider the kernel density estimation, and I want to find the quantile the quantile at some level (0.95) of the KDE.

z<-rnorm(25)
v<-density(z)
plot(v$x,v$y,type="l")

qt<-quantile(z,0.997)
abline(v=qt,col="red")


plot(v$x,cumsum(v$y),type="l")
abline(h=0.997,col="blue",type="p")


The first plot is the plot of the KDE and the line of the quantile of the data. How to get the distribution of the KDE?

How to get the quantile with respect to the KDE? Is only for comparison.

• The answer to this question was given in answer to your previous question. Densities do not sum to 1, they integrate to 1
– Tim
Apr 17 '18 at 10:49
• Yes, but How I construct the distribution starting from the density? Apr 17 '18 at 10:54
• This is a different question. Why do you want to obtain the quantiles from KDE? Why not take the empirical quantiles? If you really want to take the extreme quantiles as on the example, then the KDE approximation of the distribution won't be accurate for estimating the tails of the distribution, so the whole procedure doesn't seem to be reasonable.
– Tim
Apr 17 '18 at 11:04
• Is only for comparison Apr 17 '18 at 11:14
• I agree with @Tim. Quantile estimation is in my view best approached directly. With kernel density estimation you can't escape the need for choice of kernel shape and width -- even if the choice is made by program defaults. Such variations are especially important near $P = 0$ or $P = 1$. The method of Harrell and Davis is especially worth mentioning (reference at jstor.org/stable/2335999). There are implementations in R, Stata and presumably all other good statistical software. If there isn't one in your software, it's not good.... Apr 17 '18 at 11:28

The cdf corresponding to the kernel density estimate $\hat f(x)$ is given by $$\hat F(x)=\int_{-\infty}^x \hat f(u)du = \frac1n\sum_{i=1}^n \phi(\frac{x-x_i}h)$$ where $h$ is the bandwidth and $\phi$ is the cdf of the kernel used (e.g. the cdf of the standard normal). So equating this to $\alpha$ and solving for $x$ (numerically using some root finding algorithm) gives you the corresponding estimate of the $\alpha$-quantile (red line below), slightly larger than the empirical quantile (blue line).

R implementation:

set.seed(1)
z<-rnorm(25)
v<-density(z)
plot(v,main="")

g <- function(x, z, bw, p) sum(pnorm(x-z, sd=bw))/length(z) - p
abline(v=uniroot(g,range(v$x)+c(-1,1),z=z,bw=v$bw,p=.95)\$root,col="red") # quantile based on kernel density estimate
abline(v=quantile(z,.95),col="blue") # empirical quantile
points(z,rep(0,25))


In my programs, I am using a split approach. Within the distribution range the emperical quantiles are accurate enough usually, so I am sorting the data and calculate the quantiles from this, best with linear interpolation. I think Excel or other programs are doing so too.
For extreme percentile e.g. beyond (n-1)/n and using KDE often only the last few kernels matter, so only these have pdf>0, and only these can make cdf<1. If only the last kernel matters I need only the CDF of that last kernel and the cdf (close to 1.0) will become 1-CDFkernel/n. And this is easy to invert for all practical kernels. And I do not need to deal with root finding techniques.