What is the analytical expression of the epanechnikov kernel used in kdensity?
조회 수: 13 (최근 30일)
이전 댓글 표시
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
답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!