Search code examples
matlabstatisticsestimation

Anyone can provide simple MATLAB routine of Kernel Density Estimation?


I am trying to learn the kernel density estimation from the basic. Anyone have the simple routine for 1d KDE would be great helpful. Thanks.


Solution

  • If you have the statistics toolbox in MATLAB, you can use the ksdensity to estimate pdf/cdf using kernel smoothing. Here's an example

    data=[randn(2000,1);4+randn(2000,1)];%# create a bimodal Gaussian distribution
    x=linspace(-4,8,1e4);%# need to evaluate density at these points
    
    pF=ksdensity(data,x,'function','pdf');%# evaluate the pdf of the data points
    

    If you plot it, it should look like this

    enter image description here

    You can also get the cumulative distribution or the inverse cumulative or change the kernel that is used. You can look up the list of options from the link provided. This should help you get started :)