Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 88

Statistical Data Drift Detection

Recommended resources

Hello! Welcome to the final lesson in the "MLOps and Model Deployment" module.

In our previous lesson, we established the critical importance of monitoring models in production. We saw that a model's performance isn't static and can degrade due to phenomena like data and concept drift. We also explored high-level tools like Evidently AI and operational monitoring stacks like Prometheus/Grafana that help us detect these issues.

Today, we'll peel back another layer. This lesson focuses on the core learning outcome: to detect data drift using statistical methods. We will investigate the "how" behind the drift detection reports you saw previously. Understanding these methods is crucial for an MLOps practitioner, as they are the fundamental engine for monitoring model health when ground truth labels are not immediately available.


1. The Need for Statistical Proxies

As we discussed, the most direct way to measure model performance is by comparing predictions to ground truth labels (e.g., accuracy, precision, recall). However, in many real-world scenarios, these labels are delayed or expensive to obtain.

  • A loan default model: You only know if a loan actually defaulted months later.
  • A user churn prediction: You have to wait to see if the user truly stops using the service.
  • A medical diagnosis model: A definitive diagnosis may require further tests over time.

When ground truth is absent, we can't directly measure performance. So, what can we do? We monitor a proxy: the data itself. The core assumption is that if the input data's statistical properties change significantly, the model's performance is likely to degrade.

This is where statistical drift detection comes in. It's the practice of comparing the distribution of incoming production data to a stable, reference dataset (e.g., the training or validation set). A significant divergence between these distributions signals data drift.

Visualizing Data Drift in Input Data and Predictions
This image illustrates data drift by comparing a reference distribution (baseline) with a current distribution (production). Drift is detected when the shape or properties of the current distribution deviate significantly from the reference for either input features or model predictions.

The following video provides an excellent overview of the different types of drift and sets the stage for why we need different monitoring strategies.

ML Drift: Identifying Issues Before You Have a Problem

The video 'ML Drift: Identifying Issues Before You Have a Problem' from Fiddler AI clearly distinguishes between the types of drift and the monitoring approaches for each. Pay close attention to the distinction between performance monitoring (with labels) and data drift monitoring (without labels).

Watch the following segments: What is ML Drift? (0:00 - 3:27): This section defines drift and explains its business impact. Types of Drift (3:27 - 7:18): This part details concept drift, label drift, and feature drift. Understanding these types is key to diagnosing issues. Detecting Drift (8:18 - 10:06): This is the crucial part. It frames the problem: if you have labels, you use performance monitoring. If you don't, you must rely on data drift monitoring using statistical methods.

As the video explains, we primarily focus on detecting feature drift (also called covariate drift) when we don't have labels. We are looking for changes in the input data ().



2. A Toolkit of Statistical Tests

To quantify the difference between two data distributions (reference vs. current), we use statistical tests. Each test measures "distance" or "divergence" in a different way. Let's explore some of the most common ones used in MLOps.

The following article provides Python implementations and explanations for several key methods. We'll use it as a reference for the code.

How to Build Data Drift Detection

The article 'How to Build Data Drift Detection' from OneUptime is a great practical guide that includes code snippets for the statistical tests we will discuss.

Please read the section 'Statistical Methods for Drift Detection'. It introduces and provides Python code for the Population Stability Index (PSI), Kolmogorov-Smirnov (KS) Test, and Jensen-Shannon Divergence. We will walk through each of these.

a) Population Stability Index (PSI)

  • What it is: A popular metric, especially in the financial services industry, that measures how much a variable's distribution has shifted between two points in time.
  • Intuition: It works by bucketing the data into bins (e.g., 10 bins for age ranges). It then calculates the percentage of data points in each bin for both the reference and current datasets. Finally, it combines these percentages into a single number, the PSI score.
  • Interpretation: The score is interpreted using common rules of thumb:
    • PSI < 0.1: No significant shift. The variable is stable.
    • 0.1 ≤ PSI < 0.2: Minor shift. Requires attention.
    • PSI ≥ 0.2: Major shift. The variable has changed significantly, likely indicating drift.
  • Python Implementation: The OneUptime article provides a clear calculate_psi function. It bins the data, computes percentages, and applies the formula.

b) Kolmogorov-Smirnov (KS) Test

  • What it is: A non-parametric test that compares the cumulative distribution functions (CDFs) of two samples. It's used for numerical (continuous) variables.
  • Intuition: The KS test finds the point where the two CDFs are furthest apart. This maximum distance is the KS statistic. If this distance is large enough, the test concludes that the samples are from different distributions.
  • Interpretation: The test returns a p-value. In statistics, a small p-value (typically < 0.05) suggests that you can reject the null hypothesis.
    • Null Hypothesis (): The two samples are drawn from the same distribution.
    • p-value < 0.05: Reject . The distributions are different (drift detected).
    • p-value ≥ 0.05: Fail to reject . There's no statistical evidence of a difference (no drift detected).
  • Python Implementation: You can easily perform this test using scipy.stats.ks_2samp, as shown in the OneUptime article.

c) Jensen-Shannon (JS) Divergence

  • What it is: A method to measure the similarity between two probability distributions. It's a symmetrized and smoothed version of the more famous Kullback-Leibler (KL) divergence.
  • Intuition: Like PSI, it works on binned data (histograms). It effectively calculates how different the "shape" of the current data's histogram is from the reference data's histogram.
  • Interpretation: The JS Divergence score is bounded between 0 and 1.
    • Score = 0: The distributions are identical.
    • Score = 1: The distributions are completely different.
    • In practice, you set a threshold (e.g., 0.1 or 0.2) to alert for drift. A score above this threshold indicates significant divergence.
  • Python Implementation: The OneUptime article shows how to implement this using scipy.spatial.distance.jensenshannon.


3. Choosing the Right Test for the Job

You now have a toolkit with PSI, KS, and JS divergence. But which one should you use? There is no single "best" test; the choice depends on your data, your sample size, and how sensitive you need your drift detection to be.

This is a critical decision in practice. A test that is too sensitive will fire alerts constantly ("alert fatigue"), while a test that is not sensitive enough will miss important changes. The following article provides an excellent experimental comparison of these tests.

Which test is the best? We compared 5 methods to detect data drift ...

The article 'Which test is the best?' from Evidently AI is a masterclass in comparing drift detection methods. It explores how sample size and drift magnitude affect the results of different tests, providing practical heuristics for choosing one.

Read the following sections to understand the trade-offs: Kolmogorov-Smirnov (KS) test: Read this section to understand its high sensitivity, especially with large datasets. Population Stability Index (PSI): Note its lower sensitivity and how its results are less dependent on sample size. Jensen-Shannon divergence and Wasserstein distance: Skim these sections to see how they compare. Note that Wasserstein distance is particularly interpretable. Summing up: This final section provides excellent, actionable heuristics for choosing a test based on your problem.

Summary of Heuristics

Here is a simplified table to help you decide, based on the insights from the article:

Test Name Sensitivity Key Characteristic When to Use
Kolmogorov-Smirnov (KS) Test Very High Returns a p-value. Very sensitive to small changes, especially in large datasets. When accuracy is paramount and even small drifts are critical. Best for smaller datasets (< 10,000 samples) or when you can sample your large dataset down.
Population Stability Index (PSI) Low Returns a score (0 to ∞). Interpretable with common thresholds. Less sensitive to sample size. For very large datasets where you only want to be alerted on significant changes. Common in finance/banking.
Jensen-Shannon (JS) Divergence Moderate Returns a score (0 to 1). Symmetric and bounded. Good all-arounder. A good default for larger datasets when you want a more stable metric than KS but more sensitivity than PSI.
Wasserstein Distance Moderate Returns a distance metric. Very interpretable, especially when normalized by std dev. When you want a good balance of sensitivity and interpretability. The "normed" value gives a sense of "how many standard deviations" the data has shifted.

As a full stack AI developer, your role isn't just to implement one of these but to understand the trade-offs and select the appropriate tool for the specific business context and data characteristics.



Conclusion

Congratulations on completing the MLOps module! You've gone from Python code to a containerized, cloud-deployed, and monitored ML application. This final lesson armed you with the statistical foundations to ensure your models remain healthy in production.

Key Takeaways:

  • When ground truth is unavailable, we use statistical methods to detect data drift as a proxy for performance degradation.
  • Common methods for detecting drift in numerical features include the Kolmogorov-Smirnov (KS) Test, Population Stability Index (PSI), and Jensen-Shannon (JS) Divergence.
  • Each test has different properties. The KS test is very sensitive, while PSI is better for detecting major shifts in large datasets. JS Divergence and Wasserstein Distance offer a good balance.
  • The choice of a drift detection method is a practical decision based on your data volume, the cost of a model error, and your desired sensitivity.

Preview of the Next Module:
You have now covered the entire lifecycle of a single model, from development to deployment and monitoring. In our final module, Full Stack AI Application Development, we will tie everything together. You'll learn to build interactive user interfaces, connect them to backend APIs that serve ML models, manage asynchronous tasks for long-running predictions, and ultimately assemble a complete, production-ready AI application.