What is the analytical expression of the epanechnikov kernel used in kdensity?
    9 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
    Samuel M
 am 18 Nov. 2024
  
    
    
    
    
    Kommentiert: Samuel M
 am 29 Nov. 2024
            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
0 Kommentare
Akzeptierte Antwort
  Aastha
 am 29 Nov. 2024
        
      Bearbeitet: Aastha
 am 29 Nov. 2024
  
      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 Kommentare
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





