This graph image could be re-created using vector graphics as an SVG file. This has several advantages; see Commons:Media for cleanup for more information. If an SVG form of this image is available, please upload it and afterwards replace this template with {{vector version available|new image name}}.
It is recommended to name the SVG file “Kernel density estimation, comparison between rule of thumb and solve-the-equation bandwidth.svg”—then the template Vector version available (or Vva) does not need the new image name parameter.
The Matlab script for this example uses
kde.m and is given below.
% Datarandn('seed',1)% Used for reproducibilitydata=[randn(100,1)-10;randn(100,1)+10];% Two Normals mixed% Truephi=@(x)exp(-.5*x.^2)/sqrt(2*pi);% Normal Densitytpdf=@(x)phi(x+10)/2+phi(x-10)/2;% True Density% Kernelh=std(data)*(4/3/numel(data))^(1/5);% Bandwidth estimated by Silverman's Rule of Thumbkernel=@(x)mean(phi((x-data)/h)/h);% Kernel Densitykpdf=@(x)arrayfun(kernel,x);% Elementwise application% Plotfigure(2),clf,holdonx=linspace(-25,+25,1000);% Linear Spaceplot(x,tpdf(x))% Plot True Densityplot(x,kpdf(x))% Plot Kernel Density with Silverman's Rule of Thumbkde(data)% Plot Kernel Density with Solve-the-Equation Bandwidth
#The same code with R language#` Dataset.seed(1)#Used for reproducibilitydata=c(rnorm(100,-10,1),rnorm(100,10,1))#Two Normals mixed#` Truephi=function(x)exp(-.5*x^2)/sqrt(2*pi)#Normal Densitytpdf=function(x)phi(x+10)/2+phi(x-10)/2#True Density#` Kernelh=sd(data)*(4/3/length(data))^(1/5)#Bandwidth estimated by Silverman's Rule of ThumbKernel2=function(x)mean(phi((x-data)/h)/h)#Kernel Densitykpdf=function(x)sapply(x,Kernel2)#Elementwise application#` Plotx=seq(-25,25,length=1000)#Linear Spaceplot(x,tpdf(x),type="l",ylim=c(0,0.23),col="red")#Plot True Densitypar(new=T)plot(x,kpdf(x),type="l",ylim=c(0,0.23),xlab="",ylab="",axes=F)#Plot Kernel Density with Silverman's Rule of Thumb
Licensing
User:Kernel estimator, the copyright holder of this work, hereby publishes it under the following license:
The person who associated a work with this deed has dedicated the work to the public domain by waiving all of their rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law. You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.
http://creativecommons.org/publicdomain/zero/1.0/deed.enCC0Creative Commons Zero, Public Domain Dedicationfalsefalse