What Is The Kolmogorov-Smirnov Test?

Kolmogorov-Smirnov test

Useful in drift monitoring, the Kolmogorov-Smirnov test (KS test) is an efficient and general way to measure if two distributions significantly differ from one another. The Kolmogorov-Smirnov statistic quantifies the maximum distance between two cumulative distribution functions (CDFs). ML teams use this drift metric as a nonparametric technique for comparing a sample against a baseline or reference probability distribution.

If you monitor tabular features in production, the KS test is often the first distribution comparison you reach for. It returns a single scalar you can track, alert on, and explain without opening a histogram.

Try Arize AX

Build better agents with Arize

Trace, evaluate, and learn. Build agents that work with Arize AX and start tracing your runs today.

Prefer open source? Try Arize Phoenix for self-hosted, open source agent observability.

Key takeaways

  • The KS statistic is the largest vertical gap between two empirical CDFs. Larger values mean the distributions diverged somewhere along the range.
  • The test is nonparametric: you do not assume Gaussian inputs. It works on continuous numeric features when you can build two comparable CDFs.
  • KS reacts to any shape change (shift, spread, new mass in the tails). It does not tell you which change happened, only that something moved.
  • Pair KS with sample size checks and feature-type filters. Small windows and categorical columns produce misleading p-values.
  • For a full drift program, KS is one signal among several. Use it on continuous features and complement it with metrics suited to embeddings, text, and low-variance columns.

How the statistic works

Take a reference distribution (training data, a validation window, or last month’s production traffic) and a current sample (this week’s scores, latencies, or model inputs). Sort each set and build empirical CDFs: for every value x, F(x) is the fraction of points at or below x.

The Kolmogorov-Smirnov statistic D is the supremum of |F_ref(x) – F_current(x)| over all x. Intuitively, slide a vertical line along the axis and record the biggest height mismatch between the two step functions. If one distribution shifted right, spread wider, or gained a new bump in the tail, D captures that as a single number.

Most stacks also compute a p-value. Treat it as secondary. In production you usually care about effect size (how big is D?) and whether the shift persists across windows.

When to use the KS test in drift monitoring

The KS test fits continuous numeric features where histograms are meaningful: prediction scores, input lengths, retrieval distances, latency, token counts, and calibrated probabilities. It is a standard piece of model, concept, and data drift detection because it makes no distributional assumptions and compares entire shapes rather than just means.

Compare KS against alternatives when the feature type differs:

  • Population Stability Index (PSI) bins values and compares bin proportions. PSI is common in credit scoring and reads well for stakeholders who think in buckets. KS keeps full ordinal information and often catches subtle tail movement PSI smoothing hides.
  • Jensen-Shannon distance and related divergences compare binned probability mass. They behave well when variance is low or when you already histogram embeddings. KS operates on raw sorted values and avoids bin-width choices, but needs true continuous data.
  • Chi-square tests apply to categorical columns. Running KS on encoded categories treats labels as ordered numbers and produces nonsense.
  • Embedding distances (cluster mix, centroid shift) matter for LLM inputs where no single numeric column summarizes the traffic. KS does not replace those signals; it covers the numeric telemetry you already log.

A practical split: KS on continuous production features, chi-square or frequency tests on categoricals, embedding-based metrics on text. Fold the results into your broader AI model lifecycle management workflow so drift review sits next to retraining, validation, and release gates.

Caveats practitioners hit in production

Sample size. The KS statistic is sensitive to n. With tens of thousands of rows, even tiny visual differences can yield p-values near zero. With hundreds of rows, real shifts may not register. Set minimum sample thresholds before alerting, and prefer effect-size thresholds on D for high-volume features.

Continuous features only. The test assumes you can order values meaningfully. One-hot columns, UUID hashes, and nominal codes need different tools. If you must monitor a low-cardinality categorical, compare class frequencies directly.

Multiple comparisons. Scoring fifty features nightly yields false alarms unless you adjust thresholds or prioritize features by business impact. Rank alerts by D magnitude and persistence across windows, not raw p-value alone.

Reference window choice. Stale baselines produce chronic drift after a legitimate product change. Refresh reference windows after approved launches and document the baseline date.

Using KS alongside model monitoring

On a classification model, KS can track score distributions per class, confidence histograms, and input feature columns simultaneously. A score shift with stable inputs suggests concept drift or a calibration change. Input shift with stable scores suggests data drift that the model may still tolerate, for now.

For NLP pipelines, monitor numeric proxies: sequence length, token counts, language-detector confidence, embedding norm quantiles. Pair distribution tests with downstream quality metrics. Sentiment models, for example, often show input length and vocabulary drift before accuracy moves; see how NLP sentiment classification monitoring ties input shifts to performance review.

FAQ

What does a high Kolmogorov-Smirnov statistic mean?

It means the largest gap between the two empirical CDFs is large. Somewhere across the value range, the current sample assigns substantially different cumulative probability than the reference. Follow up with histograms or quantile tables to locate the shift.

Is the KS test parametric or nonparametric?

Nonparametric. It compares two empirical distributions without assuming they are normal or belong to a named family. That flexibility is why it appears so often in drift dashboards.

Can I use the KS test on categorical features?

No. Categories have no natural ordering for a CDF. Use frequency comparisons, chi-square tests, or PSI on categorical bins instead.

How is KS different from comparing means?

Means capture location only. Two distributions can share a mean but differ in variance, skew, or tail weight. KS summarizes overall shape difference in one statistic.

How much data do I need for a reliable KS drift check?

There is no universal minimum, but very small samples make both D and p-values unstable. Many teams require at least a few thousand rows per window for alerting, and treat smaller batches as directional only.

Don’t ship vibes.

Arize gives AI teams observability and evals to understand and improve agent performance.