Previous Up Next

7.4.22  Kernel density estimation : kernel_density, kde

kernel_density (alias : kde) accepts a list of samples L=[X1,X2,…,Xn] and optionally a sequence of options. It performs kernel density estimation1 (KDE), optionally restricted to an interval [a,b], to obtain an estimate f of the (unknown) probability density function f from which the samples are drawn, defined by :

f(x)=
1
n h
 
n
i=1
K


xXi
h



,     (1)

where K is the Gaussian kernel K(u)=1/√2 π exp(−1/2 u2) and h is the positive real parameter called the bandwidth.

The supported options are listed below.

Examples.

Input :

kernel_density([1,2,3,2],bandwidth=1/4,exact)

Output :

0.4*(exp(-8*(x-3)^2)+2*exp(-8*(x-2)^2)+exp(-8*(x-1)^2))

Input :

f:=unapply(normald(4,1,x)/2+normald(7,1/2,x)/2,x); plot(f(x),x=0..10)

Output :

Input :

X:=randvar(f,range=0..10,1000):; S:=sample(X,1000):; F:=kernel_density(S,piecewise):; plot([F,f(x)],x=0..10, display=[line_width_2+blue,line_width_1+black])

Output :

Input :

kernel_density(S,bins=50,spline=3,eval=4.75)

Output :

0.14655478136

Input :

time(kernel_density(sample(X,1e5),piecewise))

Output :

"Done",[0.17,0.1653323]

Input :

S:=sample(X,5000):; sqrt(int((f(x)-kde(S,piecewise))^,x=0..10))

Output :

0.0269841239243

Input :

S:=sample(X,25000):; sqrt(int((f(x)-kde(S,bins=150,piecewise))^2,x=0..10))

Output :

0.0144212781377

Previous Up Next