4.61538

4.6 | 13 ratings Rate this file 226 downloads (last 30 days) File Size: 2.89 KB File ID: #14034

Kernel Density Estimator

by Zdravko Botev

 

21 Feb 2007 (Updated 28 Jun 2009)

Code covered by the BSD License  

Reliable and extremely fast kernel density estimator for one-dimensional data

Download Now | Watch this File

File Information
Description

% Reliable and extremely fast kernel density estimator for one-dimensional data;
% Gaussian kernel is assumed and the bandwidth is chosen automatically;
% Unlike many other implementations, this one is immune to problems
% caused by multimodal densities with widely separated modes (see example). The
% estimation does not deteriorate for multimodal densities, because we never assume
% a parametric model for the data.
% INPUTS:
% data - a vector of data from which the density estimate is constructed;
% n - the number of mesh points used in the uniform discretization of the
% interval [MIN, MAX]; n has to be a power of two; if n is not a power of two, then
% n is rounded up to the next power of two, i.e., n is set to n=2^ceil(log2(n));
% the default value of n is n=2^12;
% MIN, MAX - defines the interval [MIN,MAX] on which the density estimate is constructed;
% the default values of MIN and MAX are:
% MIN=min(data)-Range/10 and MAX=max(data)+Range/10, where Range=max(data)-min(data);
% OUTPUTS:
% bandwidth - the optimal bandwidth (Gaussian kernel assumed);
% density - column vector of length 'n' with the values of the density
% estimate at the grid points;
% xmesh - the grid over which the density estimate is computed;
% Reference: Z. I. Botev, J. F. Grotowski and D. P. Kroese
% "KERNEL DENSITY ESTIMATION VIA DIFFUSION" ,Submitted to the Annals of Statistics, 2009
%
%
% Example:
% data=[randn(100,1);randn(100,1)*2+35 ;randn(100,1)+55];
% [bandwidth,density,xmesh]=kde(data,2^14,min(data)-5,max(data)+5);
% plot(xmesh,density)

% Notes: If you have a more reliable and accurate one-dimensional kernel density
% estimation software, please email me at botev@maths.uq.edu.au

MATLAB release MATLAB 7.1.0 (R14SP3)
Zip File Content  
Other Files kde.m,
license.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (16)
15 Mar 2007 Dirk Kroese

Check this out. Much better than the currently available density estimation procedures!

21 Mar 2008 Tingju Zhu

Highly recommend this! Very fast and robust.

04 Apr 2008 Adel Daas  
23 May 2008 rao chen

thanks a lot! it's good.

29 May 2008 Andreas H.  
07 Oct 2008 DI YANG

Not Bad, But this program is only available for 1d data. But it is still useful some questions . In any way Thanks for sharing.

10 Oct 2008 Alban Notin

Thanks for sharing this code. However using Matlab 6.5R13 I had to debug it: inputs arguments I,a2,N not specified for function fixed_point (for example).

16 Dec 2008 Nate Yoder

Great code but I believe line 83 should be:
density=idct1d(a_t)/N;
instead of:
density=idct1d(a_t)/R;
in order to get an accurately scaled density function.

16 Dec 2008 Nate Yoder

I was incorrect but there does seem to be a scale factor on the density functions

24 Jan 2009 red

Yes, the method seems to scale the function so that it becomes a pdf. But my data do not represent a pdf. How can I modify the method so that it works for general (nondensity) estimation?

25 Jan 2009 Michael Jordan  
06 Feb 2009 Karin Petrini  
01 Jun 2009 Dazhi Jiang

I think the new version just missed the heading line. Please check it. But still good job. Thank.

07 Jan 2010 David

i am using your above code and my data is plotting density values well over 1 (i.e. >500). I looked at your example

% Example:
% data=[randn(100,1);randn(100,1)*2+35 ;randn(100,1)+55];
% [bandwidth,density,xmesh]=kde(data,2^14,min(data)-5,max(data)+5);
% plot(xmesh,density)

but even then, sum(density) = 235.6368, which obviously is greater than 1. It should be 1 if it's a pdf right?

So, does your code generate a pdf? Or is scaled in some other way? If it is not a pdf, do you know how to convert it to a pdf? (do you just normalize by sum(density) ?)

08 Jan 2010 oluwole ogunkelu

can someone provide me with hierarchical token bucket(HTB) algorithm used to optimize bandwidth? kinda stucked

12 Jan 2010 Ange

Fantastic script - fast and easy to use!

Please login to add a comment or rating.
Updates
17 Oct 2007

Using higher order asymptotic approximations to achieve superior estimation accuracy for problems with few data points.

26 May 2009

updated the reference - now a journal paper submitted to the Annals of Statistics

28 Jun 2009

As pointed out by Dazhi Jiang in the comments section, the healine
"function [bandwidth,density,xmesh]=kde(data,n,MIN,MAX)"
is missing. This version corrects this editing mistake.

Tag Activity for this File
Tag Applied By Date/Time
statistics Zdravko Botev 22 Oct 2008 09:01:24
probability Zdravko Botev 22 Oct 2008 09:01:24
kernel smoothing Zdravko Botev 22 Oct 2008 09:01:24
automatic hitech bandwidth selection Zdravko Botev 22 Oct 2008 09:01:24
automatic hitech bandwidth selection Tseviet Tchen 05 Jan 2009 23:52:52
automatic hitech bandwidth selection Jean-Baptiste Sicot 20 Jan 2009 14:35:17
kernel smoothing Daniel Dolan 06 Feb 2009 18:56:31
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com