Inferensys

Glossary

Kolmogorov-Smirnov Test (KS Test)

A non-parametric statistical test that determines if two one-dimensional probability distributions differ, commonly used for univariate data drift detection in machine learning.
Data scientist building training data pipeline on laptop, data preprocessing visible, technical workspace.
DATA DRIFT DETECTION

What is the Kolmogorov-Smirnov Test (KS Test)?

A non-parametric statistical test used to detect univariate data drift by comparing two one-dimensional probability distributions.

The Kolmogorov-Smirnov (KS) test is a non-parametric statistical hypothesis test that quantifies the distance between the empirical distribution functions of two samples. It is a foundational method for univariate drift detection, comparing a reference dataset (e.g., training data) against a production dataset to determine if their underlying distributions differ significantly. The test's primary output is the KS statistic (D), which is the maximum vertical distance between the two cumulative distribution functions, and a p-value indicating the significance of this difference.

In machine learning operations, the KS test is applied per feature to monitor for covariate shift. It is sensitive to differences in both the shape and location of distributions but is limited to one-dimensional, continuous data. For multivariate drift, metrics like Jensen-Shannon Divergence are used. A calculated drift score exceeding a predefined drift threshold can trigger an automated retraining trigger or alert, forming a core component of data observability and quality posture.

STATISTICAL PROPERTIES

Key Characteristics of the KS Test

The Kolmogorov-Smirnov (KS) test is a non-parametric statistical test that quantifies the distance between two empirical cumulative distribution functions (ECDFs). Its core properties make it a foundational tool for univariate data drift detection.

01

Non-Parametric Nature

The KS test is a non-parametric test, meaning it makes no assumptions about the underlying distribution of the data (e.g., normality). It operates directly on the empirical cumulative distribution function (ECDF). This makes it robust and widely applicable for comparing any continuous or discrete one-dimensional data.

  • Key Advantage: No need to know or estimate distribution parameters.
  • Use Case: Ideal for monitoring production data where the true distribution is unknown or complex.
02

The KS Statistic (D)

The test's output is the KS statistic, denoted as D. It is the maximum vertical distance between the two empirical cumulative distribution functions being compared.

  • Calculation: (D = \sup_x | F_{reference}(x) - F_{production}(x) |), where (F) is the ECDF.
  • Interpretation: A D value of 0 indicates identical distributions. Larger D values (closer to 1) indicate greater divergence.
  • Example: A D statistic of 0.15 means the two ECDFs differ by at most 15 percentage points at their point of maximum separation.
03

Sensitivity to Location and Shape

The KS test is sensitive to differences in both the location (e.g., mean, median) and the shape (e.g., variance, skewness) of the two distributions. Because it measures the maximum distance across the entire range of the variable, it can detect:

  • Shifts in the median.
  • Changes in spread (variance).
  • Differences in skewness.

Limitation: It is generally less sensitive to differences in the tails of the distribution compared to dedicated tail-sensitivity tests.

04

Univariate and Two-Sample Focus

The standard KS test is designed for univariate, two-sample comparisons. This defines its primary role in data drift detection.

  • Univariate: It analyzes one feature (variable) at a time. To monitor a model with multiple features, a KS test must be run independently on each, potentially requiring multiplicity corrections.
  • Two-Sample: It requires two distinct datasets: a reference dataset (e.g., training data) and a target/production dataset.
  • Contrast: It is not inherently a multivariate test and cannot directly capture correlation shifts between features.
05

The p-value and Hypothesis Test

The KS test formalizes drift detection as a statistical hypothesis test.

  • Null Hypothesis (H0): The two samples are drawn from the same underlying distribution.
  • Alternative Hypothesis (H1): The two samples are drawn from different distributions.
  • p-value: The probability of observing a KS statistic (D) as extreme as the one calculated, assuming H0 is true. A small p-value (e.g., < 0.05) provides evidence to reject H0 and signal significant drift.

Critical Note: In large-scale production data, sample sizes are often enormous, leading to extremely small p-values for even minor, operationally insignificant shifts. Therefore, the D statistic is often a more practical drift metric than the p-value.

06

Comparison to PSI and Other Metrics

The KS test is one of several metrics for univariate drift. Key comparisons:

  • vs. Population Stability Index (PSI): PSI bins data and compares bin proportions. KS uses the full ECDF without binning, making it more sensitive to shifts within bins. PSI is more common in finance for monitoring score distributions.
  • vs. Wasserstein Distance: Wasserstein distance measures the "work" needed to transform one distribution into another. It can be more intuitive for continuous distributions and is often more sensitive to tail differences.
  • vs. Jensen-Shannon Divergence: JSD measures the similarity between distributions and is useful for multivariate drift when applied to lower-dimensional projections.

Best Practice: The KS test is a strong default for continuous features, but the choice of metric should align with the specific sensitivity required (e.g., tail events, central tendency).

UNIVARIATE DETECTION COMPARISON

KS Test vs. Other Drift Detection Methods

A feature comparison of the Kolmogorov-Smirnov test against other common statistical methods for detecting univariate data drift.

Feature / MetricKolmogorov-Smirnov (KS) TestPopulation Stability Index (PSI)Jensen-Shannon Divergence (JSD)Wasserstein Distance

Primary Use Case

Comparing two empirical cumulative distribution functions (ECDFs)

Comparing binned distributions, common in finance and credit risk

Measuring similarity between two probability distributions

Measuring distance between two continuous distributions

Data Type Compatibility

Continuous, ordinal

Categorical, binned continuous

Continuous, discrete (with smoothing)

Continuous

Output Range

0 to 1 (D-statistic)

0 to ∞ (typically < 0.25 is stable)

0 to 1 (symmetric, bounded)

0 to ∞ (depends on data scale)

Sensitive To

Differences in the shape/location of the entire distribution

Differences in bin probabilities, especially in tails

Overall distribution shape (smoothed KL Divergence)

Differences requiring mass to be 'moved'

Multivariate Extension

Common Drift Threshold

D-statistic > 0.1

PSI > 0.1

JSD > 0.1

Context-dependent; no universal threshold

Computational Complexity

O(n log n) for sorting

O(n + b) for b bins

O(n) with kernel density estimation

O(n^3) for exact, O(n log n) approximations

Handles Streaming/Online Data

DATA DRIFT DETECTION

Common Applications in Machine Learning

The Kolmogorov-Smirnov (KS) test is a foundational non-parametric statistical method for comparing one-dimensional probability distributions. Its primary application in machine learning is the detection of univariate data drift by quantifying the difference between a reference dataset (e.g., training data) and a production dataset.

01

Core Statistical Mechanism

The KS test quantifies the distance between two empirical cumulative distribution functions (ECDFs). For two samples, it calculates the maximum vertical distance (D-statistic) between their ECDFs. This D-statistic, along with the sample sizes, is used to compute a p-value to test the null hypothesis that the two samples are drawn from the same underlying distribution.

  • Non-parametric: Makes no assumptions about the underlying distribution shape (e.g., normal, uniform).
  • Sensitive to location, scale, and shape: Can detect differences in median, variance, and skewness.
  • One-dimensional: Operates on a single feature at a time, making it a univariate drift detection method.
02

Univariate Feature Drift Detection

This is the most common ML application. The KS test is applied independently to each feature (column) in a dataset to monitor for distributional shifts.

Process:

  1. Define a Reference Distribution: The ECDF is calculated for each feature using the training dataset or a trusted historical snapshot.
  2. Calculate a Production Distribution: The ECDF is calculated for the same feature using a recent batch of production inference data.
  3. Compute the KS Statistic: The maximum distance (D) between the two ECDFs is found.
  4. Alert on Drift: If the D-statistic exceeds a pre-defined drift threshold or the p-value falls below a significance level (e.g., 0.05), an alert is triggered for that feature.

This allows data scientists to pinpoint exactly which features have changed, aiding in root cause analysis.

03

Model Input (Covariate) Monitoring

The KS test is specifically suited for detecting covariate shift, where the distribution of input features P(X) changes between training and production, while the relationship P(Y|X) remains stable.

Example: A model trained to predict credit risk using applicant income data from 2020 may see its input distribution shift if applied to applicants in 2024 due to economic inflation. A KS test on the 'income' feature would detect this shift, signaling potential model decay even before performance metrics degrade.

Key Consideration: It cannot detect concept drift, where P(Y|X) changes, as it only analyzes the feature distributions X, not the relationship to the target Y.

04

Label Drift Analysis

The KS test can be applied to monitor the distribution of the target variable (label), especially in classification tasks with numeric scores or regression problems.

For Binary Classification: If a model outputs a prediction score (probability between 0 and 1), the KS test can compare the distribution of scores for the positive class between reference and production data. A significant drift may indicate a change in the underlying class prevalence or the model's calibration.

For Regression: The distribution of the actual target values (e.g., house prices, transaction amounts) can be monitored for drift. A shift suggests a change in the environment the model is predicting, which likely necessitates model retraining.

05

Model Output & Error Distribution Monitoring

Beyond raw features, the KS test can monitor the distributions of model predictions and prediction errors (residuals).

  • Prediction Drift: A shift in the distribution of model outputs may indicate covariate shift or early concept drift.
  • Residual Drift: In regression, the distribution of errors (actual - predicted) should ideally be consistent. A KS test detecting a change in the residual distribution is a strong signal of model performance degradation and potential concept drift, as the model's mistakes are becoming systematically different.
06

Strengths, Limitations & Practical Notes

Strengths:

  • Simple & Interpretable: The D-statistic is easy to understand (max difference between CDFs).
  • Distribution-free: Robust to non-normal data.
  • Sensitive to all distribution differences: Good for exploratory drift detection.

Limitations & Considerations:

  • Univariate Only: Cannot detect drift in correlations between features (multivariate drift). Must be combined with methods like Jensen-Shannon Divergence (JSD) or Wasserstein Distance for a full picture.
  • Sensitive to Sample Size: With very large datasets, even tiny, inconsequential distribution differences can yield statistically significant p-values. Drift thresholds should often be set based on the D-statistic's business impact, not just p < 0.05.
  • Discrete/Categorical Data: The standard KS test is for continuous data. For categorical features, use the Chi-Square test or a specialized adaptation.
  • Batch-Based: Typically used for offline drift detection on accumulated data batches. For online drift detection in streams, algorithms like ADWIN or Page-Hinkley are more suitable.
KOLMOGOROV-SMIRNOV TEST

Frequently Asked Questions

The Kolmogorov-Smirnov (KS) test is a cornerstone non-parametric statistical method for detecting univariate data drift. This FAQ addresses its core mechanics, applications in machine learning operations, and its relationship to other drift detection techniques.

The Kolmogorov-Smirnov (KS) test is a non-parametric statistical hypothesis test used to determine if two one-dimensional probability distributions differ, making it a fundamental tool for detecting univariate data drift. It works by comparing the empirical cumulative distribution functions (ECDFs) of two samples—typically a reference dataset (e.g., training data) and a production dataset. The test statistic, denoted D, is the maximum absolute vertical distance between the two ECDFs. A larger D indicates a greater divergence between the distributions. The p-value derived from D determines if the observed difference is statistically significant, allowing practitioners to reject the null hypothesis that the two samples are drawn from the same distribution.

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.