Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Kernel Density Estimation, Autoencoders, and Anomaly Detection

1. Introduction to Unsupervised Learning

In unsupervised learning, the dataset consists of unlabeled examples:

D={xi}i=1N\mathcal{D} = \{x_i\}_{i=1}^N

The goal is to discover hidden structure or patterns in the data, such as clusters, low-dimensional representations, or density estimates.

AspectSupervised LearningUnsupervised Learning
Data{(xi,yi)}\{(x_i, y_i)\}{xi}\{x_i\}
GoalLearn mapping x→yx \to yDiscover hidden structure
EvaluationLabel-based metrics (accuracy, etc.)Task-dependent metrics

Main Approaches

ApproachGoal
ClusteringPartition data into groups where samples within a cluster are more similar to each other
Dimensionality ReductionFind a low-dimensional representation preserving important structure
Density EstimationEstimate the probability density function of the unknown data distribution
AutoencodersLearn compact representations by reconstructing input from a lower-dimensional code
Anomaly DetectionIdentify rare or unusual samples that differ from the majority

2. Kernel Density Estimation

Model

Given samples {xi}i=1N\{x_i\}_{i=1}^N drawn from an unknown pdf f(x)f(x), the kernel density estimator is:

f^b(x)=1Nb∑i=1Nk(x−xib)\hat{f}_b(x) = \frac{1}{Nb} \sum_{i=1}^N k\left(\frac{x - x_i}{b}\right)

where b>0b > 0 is the bandwidth and k(⋅)k(\cdot) is a kernel function.

A common choice is the Gaussian kernel:

k(z)=12πexp⁡(−z22)k(z) = \frac{1}{\sqrt{2\pi}} \exp\left(-\frac{z^2}{2}\right)

Role of the Bandwidth

The bandwidth is the most important hyperparameter:

KDE is a non-parametric method — it does not assume a specific parametric form for the density.


3. Autoencoders

Formulation

An autoencoder is a neural network that learns to reconstruct its input. It consists of:

The model is trained to minimize the reconstruction error:

min⁡θ,ϕ∑i=1N∥xi−x^i∥2\min_{\theta, \phi} \sum_{i=1}^N \|x_i - \hat{x}_i\|^2

If the latent dimension m<dm < d (input dimension), the bottleneck forces the network to learn a compact representation. Autoencoders can be viewed as a nonlinear approach to dimensionality reduction.


4. Anomaly Detection

Anomaly detection identifies samples that differ significantly from the majority. It is often treated as unsupervised because anomaly labels are scarce.

Common assumptions:

Main Approaches

ApproachIdea
Distance-basedA sample is anomalous if far from most other samples
Density-basedA sample is anomalous if it lies in a low-density region (p(x)p(x) small)
Clustering-basedSamples not belonging to any major cluster are anomalies
Reconstruction-basedIf an autoencoder reconstructs a sample poorly, it may be anomalous

For reconstruction-based methods, the anomaly score is:

∥x−x^∥2\|x - \hat{x}\|^2

A large reconstruction error suggests the sample may be abnormal.