<< cov Miscellaneous ksdensity_kernel >>

Stixbox >> Stixbox > Miscellaneous > ksdensity

ksdensity

Kernel smoothing density estimate

Calling Sequence

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)
f=ksdensity(...)
[f,xi]=ksdensity(...)
[f,xi,width]=ksdensity(...)

Parameters

x :

a n-by-1 matrix of doubles, the data

xi :

a npoints-by-1 matrix of doubles, the points where the density is estimated

width :

a 1-by-1 matrix of doubles, positive, the width of the kernel (default=Silverman's rule).

support :

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.

akernel :

a 1-by-1 matrix of strings, the type of kernel (default="normal"). Available values are "normal", "biweight", "triangle", "epanechnikov".

npoints :

a 1-by-1 matrix of doubles, positive, the number of points in the density estimate (default=100)

f :

a npoints-by-1 matrix of doubles, the density estimate

Description

Compute a kernel density estimate of the data.

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.

Examples

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.")

Authors


Report an issue
<< cov Miscellaneous ksdensity_kernel >>