KDE — Kernel Density Estimation

Explore the shape of an observed distribution without selecting one parametric family. A Gaussian kernel is placed at every finite observation; their equally weighted mixture forms a smooth probability density and CDF.

Syntax

KDE(series)
KDE(series,bandwidth)

Bandwidth is a positive finite number in the same units as the input. It is not a percentage or a volatility. If omitted, the command uses h = 0.9 × min(sample SD, IQR/1.349) × n^(−1/5). When IQR is zero, it uses sample SD alone. The sample SD uses n−1; quartiles use linear interpolation at positions p*(n−1). Automatic bandwidth requires two or more nonconstant finite observations. Explicit bandwidth also supports one-point or constant samples.

Smaller bandwidth reveals local features; larger bandwidth smooths them. Automatic selection is a starting point, not proof that peaks are meaningful. Nonfinite observations are removed and counted. The input is copied and not modified.

Example

Changes=LOGDIFF(SERIES_SINCE(DEXJPUS,20200101))
D=KDE(Changes)
D

LOGDIFF returns fractional log changes. A bandwidth such as 0.002 therefore means 0.002 log-change units, approximately 0.2 percentage points.

D=KDE(Changes,0.002)
EXTRACT(D,"cdf:0")
EXTRACT(D,"pdf:0")
EXTRACT(D,"quantile:0.05")
EXTRACT(D,"samples:1000:42")

CDF at zero estimates the probability of a value at or below zero under the fitted marginal distribution. Density at zero is a density, not a point probability. Quantile probabilities must satisfy 0 < p < 1. Samples are a one-column matrix; seed 42 reproduces the draws for the same input and bandwidth.

Display and extracts

The default slideshow includes a histogram normalized to unit displayed bar area, the KDE density, a fitted Normal comparison, the KDE CDF, and a compact settings/diagnostics slide. The Normal comparison uses the sample mean and population/MLE standard deviation.

density/pdf and cdf return individual charts; summary/diagnostics returns settings; matrix/grid returns Value, Density and CDF columns. bandwidth, n, dropped and mean are numeric. stddev is the smoothed KDE standard deviation, sqrt(population variance + h²); sample_stddev is the original sample SD with n−1 denominator. median is the smoothed KDE median, not necessarily the sample median.

pdf:x, cdf:x and quantile:p evaluate the Gaussian mixture directly rather than interpolate the display grid. samples:n[:seed] samples an observed center uniformly and adds Gaussian noise with SD h. Count must be 1 through 100000; an optional seed is a signed 64-bit integer.

Interpretation and limitations

This first version supports Gaussian kernels and equal observation weights. Gaussian KDE extends over the whole real line, including below zero for positive observations. No boundary correction is applied. The display extends four bandwidths beyond the sample extrema; it does not truncate or renormalize numerical PDF/CDF calls.

All observations contribute equally regardless of date spacing. The estimate ignores serial dependence, seasonality and changing regimes. It is descriptive, not a conditional forecast or a risk-neutral pricing density. Sampling produces independent draws from the fitted mixture and does not preserve time-series autocorrelation. Chart resolution adapts to sample size; numerical methods still use every retained observation.

FIT_DIST compares parametric Normal, Exponential and Gamma candidates; its CURVE mode hides histogram bars. KDE provides a different, nonparametric view and may reveal multiple peaks that a single fitted family obscures.

Available extracts

Save the command result in a variable, then use EXTRACT(result, "key"). Availability can depend on the input form and fitted model.

In indexed patterns such as beta:n, replace n with the required index.