What is the analytical expression of the epanechnikov kernel used in kdensity?

I am using the kdensity function to obtain the density function of some data that is bounded at the top and bottom. As kernel I am using an epanechnikov kernel.
[pdf_kdensity,~,bwpdf] = ksdensity(data,x_values,'Support',[-0.1,(max(data)+0.1)],'BoundaryCorrection','reflection','Bandwidth','plug-in','Kernel','epanechnikov','Function','pdf')
Once kdensity adjusts the data and the optimal bandwidth value is obtained, I would like to build the function I have adjusted, for that I have built the following function:
function [f_epa]= epanechnikov_Kernel(data, h, L, U)
% Parameters:
% - data
% - h: bandwidth
% - L: lower bound
% - U: uper bound
%
% Kernel Epanechnikov
K_epa = @(u) (abs(u) <= 1) .* (3/4) .* (1 - u.^2);
f_epa = @(x) arrayfun(@(xi) (sum(K_epa((xi - data) / h)) + ...
sum(K_epa((xi - (2 * L - data)) / h)) + ...
sum(K_epa((xi - (2 * U - data)) / h))) / (length(data) * h), x);
end
When I use this function, my results are not the same as what I get with kdensity.
f_epa= epanechnikov_Kernel(data, h, L, U)
mypdf=f_epa(data,bwpdf,-0.1,(max(data)+0.1))
figure()
hold on
plot(x_values,mypdf,"Color",'blue',LineWidth=1.5)
plot(x_values, pdf_kdensity,"Color",'red',LineWidth=1);
legend('kdensity epanechnikov','myfunction epanechnikov')
hold off
However, if I adjust the data with kdensity using a normal kernel and try to replicate those results with a function programmed by me, the results do match.
[pdf_kdensity_gauss,~,bwpdf_gauss] = ksdensity(data,x_values,'Support',[-0.1,(max(data)+0.1)],'BoundaryCorrection','reflection','Bandwidth','plug-in','Kernel','normal','Function','pdf')
function [f_gauss]= gauss_Kernel(data, h, L, U)
% Parameters:
% - data
% - h: bandwidth
% - L: lower bound
% - U: uper bound
% Kernel Gaussiano
K_gauss = @(u) (1/sqrt(2*pi)) .* exp(-0.5 * u.^2);
f_gauss = @(x) arrayfun(@(xi) (sum(K_gauss((xi - data) / h)) + ...
sum(K_gauss((xi - (2 * L - data)) / h)) + ...
sum(K_gauss((xi - (2 * U - data)) / h))) / (length(data) * h), x);
end
f_gauss= gauss_Kernel(data, h, L, U)
mypdf_gauss=f_epa(data,bwpdf_gauss,-0.1,(max(data)+0.1))
figure()
hold on
plot(x_values,mypdf_gauss,"Color",'blue',LineWidth=1.5)
plot(x_values, pdf_kdensity_gauss,"Color",'red',LineWidth=1);
legend('kdensity epanechnikov','myfunction epanechnikov')
hold off
Therefore, I think that the differences between kdensity and my function when using the epanechnikov kernel are due to the fact that they use different expressions to fit the data.
In short, which expression does kdenstiy use for the epanechnikov kernel? I am using
Thank you very much

 Accepted Answer

As I understand, you want to compare your implementation of "kdensity" with MATLAB's implementation using the Epanechnikov kernel. The function "kdensity" provides an option to use a custom kernel to perform the kernel smoothing function estimate for the input data.
You can generate some dummy data as the input data for your kernel density estimation. You may refer to MATLAB code below to do so:
data = randn(100, 1); % Generate 100 samples from a standard normal distribution
xPoints = linspace(min(data) - 1, max(data) + 1, 100); % Generate points where the density will be evaluated
You can then provide a custom kernel as input to the “kdensity” function as illustrated in the MATLAB code below:
% Define a custom kernel function
customKernel = @(u) (3/4) * (1 - u.^2) .* (abs(u) <= 1); % Epanechnikov kernel
% Define a function handle for kdensity that uses the custom kernel
customKernelDensity = @(x) ksdensity(data, x, ...
'Function', 'pdf', ...
'Bandwidth', 0.5, ... % Set the bandwidth
'Kernel', customKernel);
% Evaluate the density at the specified points
densityValues = arrayfun(customKernelDensity, xPoints);
You can use your implementation of the "kdensity" function and the Epanechnikov kernel used in your implementation as the input to the custom kernel argument in the "kdensity" function. This will enable you to compare your implementation with MATLAB’s results.
For more information on “kdensity” function, kindly refer to MathWorks documentation whose link is mentioned below:
The reference section of the "kdensity" function mentions some resources that you may refer to get more information about expression of kdensity using Epanechnikov kernel.
Hope this is helpful!

3 Comments

Hi @Aastha ,thank you for your response.
I see that the way you propose me to compare the Epanechnikov kernel implemented in Matlab, with the Kernel that I have implemented is much simpler than the one I have used, so thank you very much for that.
However, my question was not oriented in that sense. I would like to know which is the Epanechnikov Kernel that Matlab uses. With your implementation, and my implementation I have already checked that they are not the same.
data = randn(100, 1); % Generate 100 samples from a standard normal distribution
xPoints = linspace(min(data) - 1, max(data) + 1, 100);
% custom epanechnikov kernel function
customKernel = @(u) (3/4) * (1 - u.^2) .* (abs(u) <= 1); % Epanechnikov kernel
% function handle for kdensity that uses the custom kernel
customKernelDensity = @(x) ksdensity(data, x, 'Function', 'pdf', 'Bandwidth', 0.5,'Kernel', customKernel);
densityValues = arrayfun(customKernelDensity, xPoints);
[pdf_kdensity]=ksdensity(data,xPoints,'Function', 'pdf', 'Bandwidth', 0.5,'Kernel', 'epanechnikov');
figure()
hold on
plot(xPoints,densityValues)
plot(xPoints,pdf_kdensity)
legend('My epanechnikov kernel','Matlabs epanechnikov kernel')
hold off
% custom gauss kernel function
customKernelGauss = @(u) (1/sqrt(2*pi)) .* exp(-0.5 * u.^2); % Gauss kernel
% function handle for kdensity that uses the custom kernel
customKernelDensityGauss = @(x) ksdensity(data, x, 'Function', 'pdf', 'Bandwidth', 0.5,'Kernel', customKernelGauss);
densityValuesGauss = arrayfun(customKernelDensityGauss, xPoints);
[pdf_kdensity_gauss]=ksdensity(data,xPoints,'Function', 'pdf', 'Bandwidth', 0.5,'Kernel', 'normal');
figure()
hold on
plot(xPoints,densityValuesGauss)
plot(xPoints,pdf_kdensity_gauss)
legend('My gauss kernel','Matlabs gauss kernel')
hold off
On the other hand, if I use your implementation to compare my gaussian kernel with the matlab one, I get the same results. It is therefore evident that the epenickov kernel I am using is not the same as the one used by Matlab.
So what I would like to know is the epanechnikov kernel that Matlab is using. I have not found anything in the documentation
The reference section of the "kdensity" function contains some references that you check which may be useful to you. The references mentioned in the "kdensity" function documentation contain the theory of the kernals.
Thanks,
Aastha
I finally found the solution.
The Epanechnikov Kernel distribution is defined as
If c= sqrt(5) is chosen, the kernel has certain optimal properties as indicated in reference [2] of https://www.mathworks.com/help/stats/ksdensity.html
Thank you for your time
data = randn(100, 1); % Generate 100 samples from a standard normal distribution
xPoints = linspace(min(data) - 1, max(data) + 1, 100);
% custom epanechnikov kernel function
c=sqrt(5);
customKernel = @(u) (3/(4*c)) * (1 - (u/c).^2) .* (abs(u) <= c); % Epanechnikov kernel
% function handle for kdensity that uses the custom kernel
customKernelDensity = @(x) ksdensity(data, x, 'Function', 'pdf', 'Bandwidth', 0.5,'Kernel', customKernel);
densityValues = arrayfun(customKernelDensity, xPoints);
[pdf_kdensity]=ksdensity(data,xPoints,'Function', 'pdf', 'Bandwidth', 0.5,'Kernel', 'epanechnikov');
figure()
hold on
plot(xPoints,densityValues)
plot(xPoints,pdf_kdensity)
legend('My epanechnikov kernel','Matlabs epanechnikov kernel')
hold off

Sign in to comment.

More Answers (0)

Asked:

on 18 Nov 2024

Commented:

on 29 Nov 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!