Inferensys

Glossary

Kolmogorov-Smirnov Test (KS Test)

A nonparametric statistical test used to determine if two samples are drawn from the same underlying distribution, frequently applied to detect feature drift.
Data engineer managing feature store on laptop, feature definitions visible, casual data engineering session.
STATISTICAL DRIFT DETECTION

What is Kolmogorov-Smirnov Test (KS Test)?

The Kolmogorov-Smirnov test is a nonparametric statistical method used to quantify the divergence between two probability distributions, commonly applied in MLOps to detect feature drift in production machine learning models.

The Kolmogorov-Smirnov (KS) test is a nonparametric goodness-of-fit test that determines whether two independent samples are drawn from the same continuous distribution. It calculates the KS statistic, defined as the maximum absolute difference between the empirical cumulative distribution functions (ECDFs) of the two samples. In model monitoring, it compares the distribution of a feature in the training data against its distribution in live inference data, providing a scalar metric to flag data drift.

The test outputs a p-value indicating the statistical significance of the observed divergence; a low p-value (typically < 0.05) rejects the null hypothesis that the distributions are identical. Unlike the Population Stability Index (PSI), the KS test does not require binning continuous variables, making it sensitive to shifts in the tails of a distribution. It is frequently paired with Kullback-Leibler Divergence and Wasserstein Distance as part of a comprehensive continuous evaluation framework to trigger triggered retraining pipelines.

DISTRIBUTIONAL COMPARISON

Key Properties of the KS Test

The Kolmogorov-Smirnov test is a nonparametric method for quantifying the distance between two empirical distributions. It is a foundational tool in MLOps for detecting feature drift and covariate shift.

01

Nonparametric Nature

The KS test makes no assumptions about the underlying data distribution. It does not require the data to follow a Gaussian curve, making it robust for financial transaction features that often exhibit heavy-tailed or multimodal distributions.

  • Distribution-Free: Works directly on the empirical cumulative distribution function (ECDF).
  • Versatile: Applicable to continuous variables like transaction amounts or risk scores.
  • Limitation: Sensitivity is highest near the center of the distribution and lower at the tails.
02

The D-Statistic

The test statistic, D, is the maximum absolute vertical distance between the two empirical cumulative distribution functions (ECDFs).

  • Calculation: D = max|F1(x) - F2(x)|.
  • Interpretation: A larger D indicates a greater divergence between the reference and production data.
  • Threshold: If D exceeds a critical value (derived from the sample size and significance level α), the null hypothesis that the samples are drawn from the same distribution is rejected.
03

Drift Detection Workflow

In a continuous evaluation pipeline, the KS test is applied as a univariate drift detector on individual features.

  • Reference Window: A baseline sample from the training data or a stable production period.
  • Analysis Window: The most recent batch of production traffic.
  • Alerting: A trigger is fired if the p-value falls below a strict threshold (e.g., p < 0.001), indicating a statistically significant shift that may degrade model performance.
04

Sensitivity to Sample Size

The KS test is highly sensitive to large sample sizes. In big data streaming contexts, even trivially small distributional shifts can produce a statistically significant p-value.

  • Practical Drift: A statistically significant result does not always imply practical model degradation.
  • Mitigation: Combine the KS statistic with a business impact metric like the Population Stability Index (PSI) to filter out noisy alerts that have no material effect on fraud detection accuracy.
05

Comparison with KL Divergence

Unlike Kullback-Leibler Divergence, the KS test is symmetric and does not require binning continuous data.

  • Symmetry: KS(P, Q) = KS(Q, P), unlike KL divergence.
  • No Binning: Operates directly on raw values, avoiding information loss from discretization.
  • Trade-off: KL divergence can be more sensitive to differences in the tails of a distribution, whereas the KS test focuses on the median body of the data.
06

Two-Sample vs. One-Sample

The KS test has two primary variants for drift detection:

  • Two-Sample KS Test: Compares two empirical distributions (e.g., training data vs. production data). This is the standard for data drift monitoring.
  • One-Sample KS Test: Compares a sample against a specific theoretical distribution (e.g., testing if errors are normally distributed). This is less common in raw feature monitoring but useful for residual analysis.
STATISTICAL DRIFT DETECTION COMPARISON

KS Test vs. Other Drift Detection Methods

A comparison of the Kolmogorov-Smirnov test against other statistical methods used to detect distributional shifts in production fraud detection models.

FeatureKS TestPopulation Stability IndexWasserstein DistanceMaximum Mean Discrepancy

Test Type

Nonparametric

Information-theoretic

Metric-based

Kernel-based

Measures

Maximum vertical distance between CDFs

Divergence across binned distributions

Earth mover's cost between distributions

Distance between mean embeddings in RKHS

Sensitive to location shifts

Sensitive to scale shifts

Sensitive to shape changes

Requires binning

Handles multivariate data natively

Computational complexity

O(n log n)

O(n)

O(n³) exact; O(n²) approx

O(n²)

KS TEST EXPLAINED

Frequently Asked Questions

Clear, technically precise answers to the most common questions about the Kolmogorov-Smirnov test and its role in detecting feature drift in production fraud models.

The Kolmogorov-Smirnov test (KS test) is a nonparametric statistical test that determines whether two samples are drawn from the same underlying continuous distribution. It works by calculating the maximum vertical distance (the supremum) between two empirical cumulative distribution functions (ECDFs). This distance, known as the KS statistic (D), is then compared against a critical value derived from the Kolmogorov distribution. If the calculated D exceeds the critical value at a chosen significance level (typically α = 0.05), the null hypothesis—that both samples originate from the same distribution—is rejected. Unlike parametric tests such as the t-test, the KS test makes no assumptions about the underlying data distribution, making it highly versatile for monitoring arbitrary feature distributions in production machine learning pipelines. In the context of model drift monitoring, the two samples are typically a reference window (e.g., training data) and a current production window, and the test is applied independently to each input feature.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.