The Kolmogorov-Smirnov (KS) test is a non-parametric, two-sample hypothesis test that quantifies the distance between the empirical cumulative distribution functions (ECDFs) of two datasets to determine if they are drawn from the same underlying distribution. It is a cornerstone of batch drift detection, used to compare a recent batch of production data against a stable reference dataset, such as the training set. The test's primary output is the KS statistic (D), which is the maximum vertical distance between the two ECDFs, and a p-value indicating the significance of the observed difference.
Glossary
Kolmogorov-Smirnov Test

What is the Kolmogorov-Smirnov Test?
A non-parametric statistical test for comparing probability distributions.
In machine learning monitoring, the KS test is particularly effective for detecting univariate data drift in continuous or ordinal features. Its non-parametric nature makes no assumptions about the shape of the distributions, providing robustness. However, it is less sensitive to differences in the tails of distributions compared to metrics like the Wasserstein distance and does not directly handle multivariate drift without feature-wise application. For drift localization, data scientists often compute the KS statistic for each feature to identify which specific variables have shifted.
Key Characteristics of the KS Test
The Kolmogorov-Smirnov (KS) test is a foundational non-parametric two-sample hypothesis test. It quantifies distributional drift by comparing the empirical cumulative distribution functions (ECDFs) of two datasets.
Non-Parametric Foundation
The KS test is a non-parametric test, meaning it makes no assumptions about the underlying distribution (e.g., normal, Poisson) of the data. This makes it robust and widely applicable for real-world data where distributional forms are unknown or complex.
- Key Advantage: It works on any continuous or discrete univariate distribution.
- Comparison: Unlike parametric tests (e.g., t-test), it tests for any difference in the shape, spread, or location of the entire distribution, not just a specific parameter like the mean.
The KS Statistic (D)
The core of the test is the Kolmogorov-Smirnov statistic (D), defined as the maximum vertical distance between the two empirical cumulative distribution functions (ECDFs).
- Calculation: (D_{n,m} = \sup_x |F_{1,n}(x) - F_{2,m}(x)|), where (F_{1,n}) and (F_{2,m}) are the ECDFs of the two samples.
- Interpretation: A larger D value indicates a greater divergence between the two sample distributions. The test's p-value is derived from the asymptotic distribution of D under the null hypothesis that the samples are drawn from the same distribution.
Two-Sample Test for Drift
In drift detection, the KS test is applied as a two-sample test comparing a reference window (e.g., training data) against a test window (current production data).
- Process: Compute the ECDF for each window, calculate D, and determine if the difference is statistically significant (p-value < alpha, e.g., 0.05).
- Output: A significant result signals a distribution drift, suggesting the model's operating environment has changed. It is sensitive to changes in median, spread, and shape.
Univariate Feature Monitoring
The standard KS test operates on a single feature at a time. For multivariate drift detection, it must be applied feature-wise.
- Standard Practice: Data scientists run a KS test on each model input feature and the prediction score/output separately.
- Drift Localization: This feature-wise application aids in drift localization, identifying which specific variables are responsible for the overall shift. The results are often visualized in a dashboard ranking features by their D statistic.
Strengths and Limitations
Strengths:
- Distribution-Agnostic: No assumption of normality.
- Sensitivity: Sensitive to differences in location, shape, and tails of distributions.
- Interpretability: The D statistic has a clear geometric meaning.
Limitations:
- Univariate: Does not capture multivariate/correlational drift.
- Sensitivity to Sample Size: Power increases with sample size; may flag insignificant shifts with very large batches.
- Less Sensitive to Tails: The ECDF converges more slowly in the tails, making it somewhat less sensitive to extreme outliers compared to a test like Anderson-Darling.
Related Statistical Measures
The KS test is one of several statistical distances used for drift detection. Key alternatives include:
- Population Stability Index (PSI): More common in finance, bins data and compares proportions.
- Wasserstein Distance: Measures the "work" needed to transform one distribution into another.
- Kullback-Leibler Divergence: An information-theoretic measure of distribution difference (not symmetric).
- Maximum Mean Discrepancy (MMD): A kernel-based method that can handle multivariate data directly.
The choice depends on the data type, need for sensitivity to specific distribution aspects, and computational constraints.
How the Kolmogorov-Smirnov Test Works: Calculation & Interpretation
The Kolmogorov-Smirnov (K-S) test is a foundational non-parametric statistical method for detecting distributional shifts, a critical task in monitoring machine learning models for concept drift.
The Kolmogorov-Smirnov test is a non-parametric two-sample hypothesis test that quantifies the distance between the empirical cumulative distribution functions (ECDFs) of two datasets to determine if they originate from the same underlying distribution. It calculates the Kolmogorov-Smirnov statistic (D), which is the maximum vertical distance between the two ECDFs. This test is distribution-agnostic, making it robust for comparing any continuous data without assumptions about normality, and is widely used for batch drift detection by comparing a reference dataset (e.g., training data) against a recent production batch.
Interpreting the test involves comparing the calculated D statistic to a critical value from the Kolmogorov-Smirnov distribution or computing a p-value. A significant result (p-value < alpha, e.g., 0.05) provides evidence to reject the null hypothesis that the samples are from the same distribution, signaling potential data drift. While powerful, its sensitivity diminishes in high dimensions, and it is less effective for detecting changes in distribution tails compared to metrics like the Wasserstein distance. For multi-feature drift, it is often applied per feature as part of drift localization.
Primary Use Cases in Machine Learning
The Kolmogorov-Smirnov (KS) test is a foundational non-parametric statistical test used in machine learning to compare two empirical distributions. Its primary application is in detecting and quantifying distributional shifts in data, a critical task for maintaining model performance in production.
Detecting Feature Drift (Covariate Shift)
The KS test is a standard tool for batch monitoring to detect covariate shift, where the distribution of input features P(X) changes between a reference window (e.g., training data) and a test window (e.g., recent production data).
- Mechanism: It compares the Empirical Cumulative Distribution Function (ECDF) of a single feature across two samples. A large maximum vertical distance (D-statistic) indicates a significant shift.
- Example: Monitoring the distribution of
transaction_amountin a fraud detection model. A significant KS test result would signal that recent transactions are statistically different from the training data, potentially degrading model accuracy. - Key Output: The test returns a D-statistic (the maximum difference between ECDFs) and a p-value to assess statistical significance.
Monitoring Model Scores & Predictions
Beyond raw features, the KS test is applied to monitor the distribution of a model's output scores or predicted probabilities. This is crucial for detecting prior probability shift or degradation in model confidence.
- Application: Comparing the distribution of predicted probabilities for the positive class (e.g.,
P(churn)) between the validation set and last week's production inferences. - Interpretation: A drift in the score distribution often precedes a drop in performance metrics like accuracy or F1-score, serving as an early warning system.
- Complementary Use: It is often used alongside metrics like the Population Stability Index (PSI) for comprehensive score monitoring.
Unsupervised Drift Detection in Data Streams
In online learning and streaming applications, the two-sample KS test can be adapted for unsupervised drift detection by sequentially comparing windows of data.
- Process: A fixed reference window of historical data is statistically compared against a sliding test window of recent observations.
- Challenge: The test assumes independent samples, which can be violated in time-series data. Pre-processing like differencing may be required.
- System Integration: It forms a core component of automated retraining systems, where a significant KS result can trigger retraining of a production model.
Drift Localization & Root Cause Analysis
When a global performance drop is detected, the KS test is used for drift localization—identifying which specific features are responsible for the distributional change.
- Methodology: The test is run independently on each feature (or feature group) in the dataset. Features with the smallest p-values (largest D-statistics) are the primary drivers of the observed drift.
- Actionable Insight: This directs investigative efforts. For example, if only the
customer_agefeature shows drift, the issue may be a demographic shift in the user base rather than a problem with the core model logic. - Limitation: It analyzes features in isolation and may miss multivariate interactions causing drift.
Benchmarking Against Other Detection Metrics
The KS test provides a simple, interpretable baseline against which more complex drift detection methods are often compared.
- Comparison Points:
- vs. PSI: KS is non-parametric and sensitive to any distribution difference, while PSI focuses on relative entropy and is commonly used for binned score distributions in finance.
- vs. Wasserstein Distance: KS is sensitive to the location of the maximum CDF difference, while Wasserstein distance considers the entire shape of the distribution.
- vs. MMD: KS is a univariate test, while Maximum Mean Discrepancy (MMD) can perform multivariate distribution tests using kernel embeddings.
- Role: Its computational simplicity and clear statistical interpretation make it a default first check in many monitoring pipelines.
Validating Data Preprocessing & Sampling
Before model training, the KS test is used to ensure the stability and consistency of data pipelines, a key aspect of data observability.
- Use Cases:
- Verifying that two samples (e.g., training and validation splits) are drawn from the same underlying distribution.
- Checking that data augmentation or synthetic data generation (e.g., using SMOTE) preserves the original distribution of features.
- Validating that a new batch of labeled data for active learning matches the distribution of the unlabeled pool.
- Outcome: A non-significant KS result provides confidence that observed model performance changes are due to the algorithm, not underlying data discrepancies.
KS Test vs. Other Drift Detection Methods
A technical comparison of the Kolmogorov-Smirnov test against other prominent statistical and machine learning methods for detecting distributional shift in continuous model learning systems.
| Feature / Metric | Kolmogorov-Smirnov (KS) Test | Population Stability Index (PSI) | Maximum Mean Discrepancy (MMD) | Online Methods (e.g., ADWIN, Page-Hinkley) |
|---|---|---|---|---|
Core Statistical Principle | Compares empirical cumulative distribution functions (ECDFs) | Compares binned probability distributions | Compares distributions in a kernel-induced feature space | Monitors a streaming statistic (e.g., error rate, mean) for change points |
Detection Type | Batch (two-sample) | Batch (two-sample) | Batch (two-sample) | Online (sequential) |
Data Requirement | Reference and test samples | Reference and test samples | Reference and test samples | Single data stream |
Handles Multivariate Data | ||||
Primary Use Case | Univariate feature drift detection | Univariate score or feature drift | High-dimensional/multivariate drift detection | Real-time monitoring of model metrics or raw features |
Output Interpretation | p-value & D-statistic (max vertical ECDF distance) | Index value (lower = more stable) | Test statistic & p-value | Binary alarm (drift/no-drift) & estimated change point |
Computational Complexity | O(n log n) for sorting | O(n) for binning | O(n²) for kernel matrix (naive) | O(n) or O(1) per observation |
Sensitivity to Distribution Shape | Medium (depends on binning) | Varies (configured for target statistic) |
Frequently Asked Questions
The Kolmogorov-Smirnov (K-S) test is a foundational non-parametric statistical method used in machine learning to detect distributional shifts, a critical task for maintaining model performance in dynamic environments.
The Kolmogorov-Smirnov (K-S) test is a non-parametric two-sample hypothesis test that quantifies the distance between the empirical cumulative distribution functions (ECDFs) of two datasets to determine if they are drawn from the same underlying distribution. It works by calculating the Kolmogorov-Smirnov statistic (D), which is the maximum vertical distance between the two ECDFs. A large D value indicates a significant difference between the distributions. The test then compares this D statistic to a critical value derived from the Kolmogorov distribution (or uses a p-value) to reject or fail to reject the null hypothesis that the two samples come from the same distribution. Its non-parametric nature makes it robust, as it makes no assumptions about the shape of the distributions (e.g., normality).
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
The Kolmogorov-Smirnov test is a foundational tool for detecting distributional shifts. These related terms provide the statistical and operational context for its application in monitoring machine learning systems.
Two-Sample Hypothesis Testing
Two-sample hypothesis testing is the statistical framework for determining whether two independent data samples originate from the same underlying probability distribution. The Kolmogorov-Smirnov test is a specific non-parametric implementation of this framework.
- Core Principle: It formulates a null hypothesis (H₀) that the two samples are drawn from the same distribution and an alternative hypothesis (H₁) that they are not.
- Non-Parametric vs. Parametric: Unlike parametric tests (e.g., t-test, which assumes normality), the KS test makes no assumptions about the distribution's shape, making it robust for real-world, unknown data distributions.
- Application in ML: This is the direct statistical basis for batch drift detection, where a recent production batch is tested against a reference training set.
Population Stability Index (PSI)
The Population Stability Index is a widely used metric in finance and ML monitoring to quantify the shift in the distribution of a single variable between two datasets.
- Calculation: PSI aggregates the divergence across bins of a histogram. For each bin
i, it computes(Actual% - Expected%) * ln(Actual% / Expected%). - Interpretation: Values < 0.1 indicate minimal change, 0.1-0.25 indicate some shift requiring investigation, and > 0.25 signal a significant distribution change.
- Comparison to KS: While the KS test compares entire Cumulative Distribution Functions, PSI compares binned probability distributions. PSI is often applied per-feature, while the two-sample KS test provides a single statistic for the overall sample comparison.
Maximum Mean Discrepancy (MMD)
Maximum Mean Discrepancy is a kernel-based statistical test used to determine if two samples are drawn from different distributions by comparing their embeddings in a high-dimensional feature space.
- Kernel Trick: MMD uses a kernel function (e.g., Gaussian RBF) to implicitly map data into a Reproducing Kernel Hilbert Space (RKHS), where it computes the distance between the mean embeddings of the two samples.
- High-Dimensional Data: It is particularly powerful for complex, high-dimensional data (like images or embeddings) where traditional tests like KS may lose sensitivity.
- Modern Alternative: MMD is a cornerstone of modern drift detection frameworks, often outperforming KS on structured, non-tabular data by capturing more nuanced distributional differences.
Wasserstein Distance
Wasserstein distance, also known as Earth Mover's Distance, is a metric from optimal transport theory that measures the minimum "cost" of transforming one probability distribution into another.
- Intuitive Interpretation: It conceptualizes distributions as piles of earth; the distance is the minimum amount of work (mass × distance) needed to reshape one pile into the other.
- Sensitivity to Shape: Unlike KS, which focuses on the maximum CDF difference, Wasserstein distance is sensitive to the overall geometry and shape of the distributions, including differences in local features.
- Use Case: It is valuable for drift detection when the morphology of the shift matters, such as in image data or when assessing the practical impact of a distribution change.
Batch Drift Detection
Batch drift detection is the operational process of periodically comparing the statistical properties of a recent batch of production data against a reference dataset to identify distributional shifts.
- Workflow: A scheduled job collects a window of recent data (the test window) and runs statistical tests (like KS, PSI, MMD) against a fixed reference window (often the training set).
- Contrast with Online: Unlike online drift detection (e.g., ADWIN, Page-Hinkley), which analyzes every incoming point, batch detection operates on aggregated snapshots, making it computationally efficient for scheduled monitoring.
- Primary Use: The Kolmogorov-Smirnov test is a classic algorithm deployed within batch drift detection systems to trigger alerts for model retraining.
Drift Localization
Drift localization is the diagnostic process that follows drift detection, aiming to identify the specific features or subsets of features responsible for the observed distributional shift.
- Post-Detection Analysis: After a test like KS signals a global drift, localization techniques are applied to pinpoint the root cause. This often involves running univariate tests (like KS or PSI) on each feature individually.
- Actionable Insights: Localization transforms an alert ("drift detected") into an actionable diagnosis ("drift is primarily in features X and Y"), guiding data scientists to investigate upstream data pipeline issues or feature engineering problems.
- Critical for Remediation: Effective localization is essential for efficient drift adaptation, as it informs whether retraining is needed or if the issue is isolated to specific, possibly corrupt, data channels.

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.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us