Search code examples
rderivative

Derivative of Kernel Density


I am using density {stats} to construct a kernel "gaussian' density of a vector of variables. If I use the following example dataset:

    x <- rlogis(1475, location=0, scale=1)  # x is a vector of values - taken from a rlogis just for the purpose of explanation
    d<- density(x=x, kernel="gaussian")

Is there some way to get the first derivative of this density d at each of the n=1475 points


Solution

  • The curve of a density estimator is just the sum of all the kernels, in your case a gaussian (divided by the number of points). The derivative of a sum is the sum of the derivatives and the derivative of a constant times a function is that constant times the derivative. So the derivative of the density estimate at a given point will just be the average of the slopes for the 1475 different gaussian curves at that given point. Each gaussian curve will have a mean corresponding to each of the data points and a standard deviation based on the bandwidth. So if you can calculate the slope for a gaussian, then finding the slope for the density estimate is just a mean of the 1475 slopes.