Jump to content

File:Kernel density estimation, comparison between rule of thumb and solve-the-equation bandwidth.png

Page contents not supported in other languages.
This is a file from the Wikimedia Commons
From Wikipedia, the free encyclopedia

Summary

Description
Date
Source
 
This diagram was created with MATLAB.
Author Kernel estimator
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.

% Data
randn('seed',1)                            % Used for reproducibility
data = [randn(100,1)-10; randn(100,1)+10]; % Two Normals mixed
% True
phi = @(x) exp(-.5*x.^2)/sqrt(2*pi);       % Normal Density
tpdf = @(x) phi(x+10)/2+phi(x-10)/2;       % True Density
% Kernel
h = std(data)*(4/3/numel(data))^(1/5);     % Bandwidth estimated by Silverman's Rule of Thumb
kernel = @(x) mean(phi((x-data)/h)/h);     % Kernel Density
kpdf = @(x) arrayfun(kernel,x);            % Elementwise application
% Plot
figure(2), clf, hold on
x = linspace(-25,+25,1000);                % Linear Space
plot(x,tpdf(x))                            % Plot True Density
plot(x,kpdf(x))                            % Plot Kernel Density with Silverman's Rule of Thumb
kde(data)                                  % Plot Kernel Density with Solve-the-Equation Bandwidth
#The same code with R language
#` Data
set.seed(1)                                                     #Used for reproducibility
data = c(rnorm(100,-10,1),rnorm(100,10,1))                      #Two Normals mixed
#` True
phi = function(x) exp(-.5*x^2)/sqrt(2*pi)                       #Normal Density
tpdf = function(x) phi(x+10)/2+phi(x-10)/2                      #True Density
#` Kernel
h = sd(data)*(4/3/length(data))^(1/5)                           #Bandwidth estimated by Silverman's Rule of Thumb
Kernel2 = function(x) mean(phi((x-data)/h)/h)                   #Kernel Density
kpdf = function(x) sapply(x,Kernel2)                            #Elementwise application
#` Plot
x=seq(-25,25,length=1000)                                       #Linear Space
plot(x,tpdf(x),type="l",ylim=c(0,0.23),col="red")               #Plot True Density
par(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:
Creative Commons CC-Zero This file is made available under the Creative Commons CC0 1.0 Universal Public Domain Dedication.
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.

Original upload log

Transferred from en.wikipedia to Commons using For the Common Good.

The original description page was here. All following user names refer to en.wikipedia.
Date/Time Dimensions User Comment
15:47, 19 April 2016 733 × 561 (28,809 bytes) w:en:Kernel estimator (talk | contribs) author: kernel estimator source: created by kernel estimator using the Matlab code on Wikipedia URL: N/A

Captions

Add a one-line explanation of what this file represents

Items portrayed in this file

depicts

19 April 2016

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current20:39, 26 October 2018Thumbnail for version as of 20:39, 26 October 2018733 × 561 (28 KB)JumpowTransferred from en.wikipedia: see original upload log above

The following page uses this file:

Global file usage

The following other wikis use this file:

Metadata