Skip to content

Clustering, density estimation, and anomaly detection · Mixture and density models · UCL046 · Model

Kernel density estimation (KDE)

Estimate a smooth density without specifying a finite parametric population family. KDE supports visualization, likelihood-based scoring, mode exploration, and anomaly scores based on low reference density.

Runs in CodeSplash Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page.

Do it in CodeSplash Run it on your own data, in Python or R.

Availability
Runs in CodeSplash
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through discover.kde. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Estimate a smooth density without specifying a finite parametric population family. KDE supports visualization, likelihood-based scoring, mode exploration, and anomaly scores based on low reference density.

Question / hypotheses

Which probability model adequately explains the reference distribution and its latent heterogeneity? This entry defines an estimation or optimization objective, not an intrinsic null/alternative hypothesis test.

Architecture / computational structure

One kernel centered on each training observation, combined into a normalized density.

Assumptions

Continuous density is coordinate- and unit-dependent. Density estimation becomes statistically difficult with many dimensions, and naive kernels leak probability beyond bounded support. Low density can identify a rare legitimate subgroup rather than an error.

Formula / computational definition

f^h(x)=(nhd)−1∑iK((x−xi)/h)\hat f_h(x)=(nh^d)^{-1}\sum_iK((x-x_i)/h) for dimension dd, bandwidth h>0h>0, and normalized kernel KK. Bandwidth controls the bias–variance tradeoff; kernel choice is usually less influential. Adaptive bandwidth and nearest-neighbor density estimates are extensions, not separately counted here.

Parameters and notation

Learned or fitted quantities: The retained sample and any fitted bandwidth or transformation parameters. User-set hyperparameters/design choices: Choose metric, bandwidth, kernel, boundary treatment, and preprocessing. Tune these against the stated objective and stability/validation criteria; do not select settings on the final evaluation set.

Fitting, fine-tuning, and adaptation

Refit likelihood or posterior parameters on the new reference population; neural fine-tuning does not apply to this classical model. Warm starts can reduce computation but should be compared with fresh initialization. Recheck predictive fit and regularization after a domain shift.

How to use and calibrate

Use held-out log density or suitable bandwidth criteria, visual predictive checks, and sensitivity analysis. Never choose bandwidth using evaluation anomalies and then report that same set as untouched validation.

Example application

Visualize and score a continuous reference population without fixing component count. Define the reference population, measurement units, and success criterion before fitting; investigate whether discovered groups or alerts reflect the intended phenomenon.

Interpretation / pitfalls

Watch out

The reported result is conditional on the representation and objective. A stable partition can reflect nuisance structure such as batch, collection site, or missingness. Confirm the interpretation in original variables and an independent sample.

Python and R code examples

Python has sklearn.neighbors.KernelDensity; R stats::density handles univariate KDE. Multivariate R examples require custom kernels or a separately verified package. Retain normalization constants when comparing likelihoods across bandwidths.

Component: Normalized Gaussian KDE at query points. Python: executed successfully; R: not executed (Rscript unavailable). These small fixtures establish the arithmetic operation, not full-method accuracy or reproduction of paper results.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — Normalized Gaussian KDE at query points. · needs numpy

import numpy as np
train=np.array([-1.,0.,1.]); query=np.array([0.,2.]); h=.5
result=np.exp(-.5*((query[:,None]-train)/h)**2).mean(1)/(np.sqrt(2*np.pi)*h)
print(result)
R

Component demo, not a trained model — Normalized Gaussian KDE at query points.

train=c(-1,0,1); query=c(0,2); h=.5
result=sapply(query,function(q) mean(dnorm((q-train)/h))/h)
print(result)

Sources

From the CodeSplash reference, entry UCL046.