kernelDensity
Estimates the probability density of a set of observations with a kernel density estimate, evaluated on an evenly spaced grid. Each observation contributes one kernel scaled to the bandwidth, and the sum is normalized so the curve’s area is 1. Non-finite values are ignored; an empty input returns an empty array.
Bandwidth
Section titled “Bandwidth”A smaller bandwidth follows the data more closely and shows more bumps; a larger one smooths them away. It can also be a function of the values, such as a different rule of thumb.
import {kernelDensity} from "@d3plus/math";
const values = [1.2, 1.9, 2.1, 2.4, 2.6, 3, 3.1, 3.3, 4.8, 5.2, 5.5, 5.6, 6.1];
kernelDensity(values, {bandwidth: 0.3});
Basic example
Section titled “Basic example”Each observation (the dots) contributes a gaussian bump one bandwidth wide; their sum, normalized to an area of 1, is the density curve. The bandwidth defaults to Silverman’s rule of thumb, and the curve runs 3 bandwidths past the data, sampled at 100 evenly spaced [value, density] points. Violin and Ridgeline draw these curves.
import {kernelDensity} from "@d3plus/math";
const values = [1.2, 1.9, 2.1, 2.4, 2.6, 3, 3.1, 3.3, 4.8, 5.2, 5.5, 5.6, 6.1];
kernelDensity(values);
Epanechnikov
Section titled “Epanechnikov”kernel picks the bump’s shape: "gaussian" (default), "epanechnikov" (a parabola scaled to the gaussian’s spread, so the same bandwidth smooths about as much), or any function of the scaled distance. cut: 0 stops the curve at the smallest and largest value, and points sets how many values it is evaluated at; extent fixes the range outright.
import {kernelDensity} from "@d3plus/math";
const values = [1.2, 1.9, 2.1, 2.4, 2.6, 3, 3.1, 3.3, 4.8, 5.2, 5.5, 5.6, 6.1];
kernelDensity(values, {kernel: "epanechnikov", cut: 0, points: 60});
Parameters
import {kernelDensity} from "@d3plus/math";