Skip to content
Prev 351297 / 398506 Next

Getting a cdf equal to 1 from a variable kernel density estimation

On 5/27/2015 2:43 AM, Lionel Delmas wrote:
I ran your code.  Looking at the plot output, it is obvious that there 
is something wrong with you density estimates.  Since you are estimating 
density for standard normal random variates, the density for values near 
0 should be approximately 0.4, and for values in the tails the density 
should be "close" to 0.

You mention "variable kernel density estimation", but I don't see where 
you are varying your smoothing parameter, h, or any distance measure.  R 
provides a density function that could be used here, unless you are just 
wanting an excerise in how density estimation works (or this is a 
homework problem).

This is not my area of expertise, but the main problem(s) appears to be 
how gaussianKernel() and densityFunction() are written.  I think you 
want something like the following:

gaussianKernel <- function(u) exp(-u^2/2)/(2*pi)^.5

densityFunction <- function(x, df, ker, h){
     difference = t(t(df) - x)/h
     W = sum(apply(difference, 1, ker)) / (nrow(df)*h)
     }


If you are wanting to do density estimation for real world work, I would 
get help from someone in your local area.


Hope this is helpful,

Dan