kde

Kernel density estimate: a smooth estimate of the probability density of a sample.

Category: Data Import and Analysis > Descriptive Statistics

Syntax

[f, xi] = kde(x)

Estimates the density of the data x at a grid of 100 points.

Description

A kernel density estimate places a small smooth bump on every observation and adds them up, giving a continuous curve that integrates to 1. It is a smoother alternative to a histogram, with no dependence on bin edges. The bandwidth, which sets how wide each bump is, is chosen automatically from the data.

Plot f against xi to see the shape of the distribution, including multiple peaks.

Input arguments

x
Vector of observations.

Output arguments

f
Estimated density values.
xi
Points at which the density is evaluated, spanning the data with some margin.

Example

Density integrates to one

rng(1);
x = [randn(1,50), 4 + randn(1,50)];
[f, xi] = kde(x);
size(xi)
round(trapz(xi, f), 2)
Output
ans = 1×2

     1   100
ans = 1

See also

histcounts histogram mean