Kernel smoothing density estimate
ksdensity(x) ksdensity(x,xi) ksdensity(x,xi,"kernel",akernel) ksdensity(x,xi,"npoints",npoints) ksdensity(x,xi,"support",support) ksdensity(x,xi,"width",width) ksdensity(x,xi,"func",func) f=ksdensity(...) [f,xi]=ksdensity(...) [f,xi,width]=ksdensity(...)
a n-by-1 matrix of doubles, the data
a npoints-by-1 matrix of doubles, the points where the density is estimated
a 1-by-1 matrix of doubles, positive, the width of the kernel (default=Silverman's rule).
a 1-by-1 matrix of string, the support (default="unbounded"). If support is "unbounded", all real values are possible. If support is "positive", only positive values are possible.
a 1-by-1 matrix of strings, the type of kernel (default="normal"). Available values are "normal", "biweight", "triangle", "epanechnikov".
a 1-by-1 matrix of doubles, positive, the number of points in the density estimate (default=100)
a string, the function to estimate (default="pdf"). Available values are "pdf" and "cdf". If func=="pdf", estimates the probability density function. If func=="cdf", estimates the cumulative distribution function.
a npoints-by-1 matrix of doubles, the density estimate
Compute a kernel density estimate of the data. The density estimate f is evaluated at the points in xi.
For i=1,...,npoints, the estimate for the PDF is :
where K is a kernel for PDFs. These kernels are available in the ksdensity_kernelpdf function.
For i=1,...,npoints, the estimate for the CDF is :
where K is a kernel for PDFs. These kernels are available in the ksdensity_kernelcdf function.
The Silverman rule for the width u of the kernel is:
where is the empirical standard
deviation, and n is the size of the sample.
On output, xi
and f
contain
the kernel density estimate of the data.
For i=1,2,...,npoints, f(i) is the estimate
of the probability density at xi(i).
More details on the kernels available in this function is provided in the help of ksdensity_kernel.
X=distfun_normrnd(0,1,1000,1); [f,xi,width]=ksdensity(X); scf(); histo(X,[],%t); plot(xi,f,"r-"); xtitle("Kernel density estimate","X","Density"); legend(["Data","PDF estimate"]); // Set the kernel width X=distfun_normrnd(0,1,1000,1); [f,xi,width]=ksdensity(X,"width",0.5); scf(); histo(X,[],%t); plot(xi,f,"r-"); // Set the number of points X=distfun_normrnd(0,1,1000,1); [f,xi,width]=ksdensity(X,"npoints",500); scf(); histo(X,[],%t); plot(xi,f,"r-"); // Set the kernel scf(); X=distfun_normrnd(0,1,1000,1); // subplot(2,2,1); histo(X,[],%t); [f,xi,width]=ksdensity(X,"kernel","normal"); plot(xi,f,"r-"); xtitle("Gaussian Density Estimate","X","Density") legend(["Data","PDF estimate"]); // subplot(2,2,2); histo(X,[],%t); [f,xi,width]=ksdensity(X,"kernel","epanechnikov"); plot(xi,f,"r-"); xtitle("Epanechnikov Density Estimate","X","Density") legend(["Data","PDF estimate"]); // subplot(2,2,3); histo(X,[],%t); [f,xi,width]=ksdensity(X,"kernel","biweight"); plot(xi,f,"r-"); xtitle("Biweight Density Estimate","X","Density") legend(["Data","PDF estimate"]); // subplot(2,2,4); histo(X,[],%t); [f,xi,width]=ksdensity(X,"kernel","triangle"); plot(xi,f,"r-"); xtitle("Triangular Density Estimate","X","Density") legend(["Data","PDF estimate"]); // Set the kernel width X=distfun_normrnd(0,1,1000,1); scf(); // [f,xi,width]=ksdensity(X,"width",0.1); subplot(2,2,1) histo(X,[],%t); plot(xi,f,"r-"); xtitle("width=0.1") // [f,xi,width]=ksdensity(X,"width",0.25); subplot(2,2,2) histo(X,[],%t); plot(xi,f,"r-"); xtitle("width=0.25") // [f,xi,width]=ksdensity(X,"width",0.5); subplot(2,2,3) histo(X,[],%t); plot(xi,f,"r-"); xtitle("width=0.5") // [f,xi,width]=ksdensity(X,"width",1.); subplot(2,2,4) histo(X,[],%t); plot(xi,f,"r-"); xtitle("width=1.") // Estimate CDF X=distfun_normrnd(0,1,1000,1); [f,xi,width]=ksdensity(X,"func","cdf","kernel","epanechnikov"); scf(); xlabel("x"); ylabel("P(X<x)"); X=gsort(X,"g","i"); n=size(X,"*"); p=(1:n)/n; stairs(X,p); plot(xi,f,"r-"); xtitle("Kernel CDF estimate","X","Density"); legend(["Data","CDF estimate"],2); | ![]() | ![]() |