Model Drift Detection with Kolmogorov Smirnov Statistic on Spark

In retail business, you may be using various business solutions based on product demand data e.g inventory management or how a newly introduced product may be performing with time. The buying behavior model may change with time rendering the those solutions ineffective. It may be necessary to periodically tune the system, i.e detect any drift in the behavior model. If the drift is significant, appropriate changes to be made in business solutions that depend on demand distribution.

In this post we will find out how to use a statistic called Kolmogorov Smirnov Statistic (KS statistic )to measure product demand model drift. The spark implementation for KS Statistic is available in my open source project chombo and avenir. The implementation uses a heavy doses of configuration and is application agnostic.

Model Drift and Kolmogorov Smirnov Statistic

Non stationary nature of data causes model drift. Data is non stationary when the statistical properties of the data change with time. There are various ramifications of model drift. For classification and regression problems there will be significant drop in accuracy.

Kolmogorov Smirnov Statistic (KS statistic) detects difference in data distributions. Classification algorithms can be broadly be divided into 2 categories, data distribution based and discrimination based. In discrimination based algorithms like Decision Tree and SVM, the algorithms are based on the class boundaries. For such algorithms, detecting drift in data distribution may not suffice.

For the particular use case we are discussing, the model is defined by the probability distribution of time elapsed since the last transaction and the sold quantity.

Kolmogorov Smirnov Statistic is based on the maximum difference between cumulative distribution between a reference distribution and a recent distribution. There are other techniques for detecting distribution drift, but KS statistic has a good statistical foundation because it’s based on significance test.

When a model is built for deployment, a distribution is calculated for the data set and saved. This distribution is used as the reference distribution. At some future time, distribution of the data could be calculated and could be used along with reference distribution to calculate KS statistic.

Sales Data

Our use case involves retail sales data counting s product ID, transaction time and sold quantity. Our goal is detect any drift in the distribution of time between transactions and the quantity.

The Spark pipeline contains 3 Spark jobs, as below for data preprocessing and calculation of KS statistic.

  • Convert time stamp to elapsed time since last transaction
  • Calculate empirical probability distribution for elapsed time and quantity
  • Calculate KS statistic

The first two jobs need to run twice once for reference data and once for recent data. Finally, the last job takes the output for reference data and recent data to compute KS statistic.

Spark Pipeline

The Spark job TimeIntervalGenerator converts time stamp converts time stamp to time interval.

The next spark job NumericalAttrDistrStats calculates histogram for time interval and transaction amount. There is seasonality in the sales data. Transactions are segmented based on whether purchases are made during the day or evening hours. Separate histograms are calculated for each product and each of the two seasonal indexes. Here is some sample output.


The last Spark job KolmogorovSmirnovModelDrift calculates KS statistic based on the distributions for reference data and recent data. Here is some output


We find that the product 4ALR756SE52E has significant deviation in distribution of quantity sold.

Drift in Demand Distribution

Demand distribution is defined by distribution of quality sold and time elapsed between sales. Smaller quantity sold and / or larger time between sales implies less demand. Significant drift in the demand distribution can be can be used in various ways in business. Here are some examples

  • Inventory management
  • To decide whether a product should be discontinued
  • To decide whether more marketing money should be spent for a product
  • To decide whether a product with increasing demand is cannibalizing a similar product

Wrapping Up

We have seen how KS statistic can be used to detect significant drift in probability distribution. To run the use case in this post, please follow the steps in the tutorial document.

You may be wondering why we need Spark to calculate KS statistic. It’s only necessary if you want to do at scale computation as in the example in this post.

About Pranab

I am Pranab Ghosh, a software professional in the San Francisco Bay area. I manipulate bits and bytes for the good of living beings and the planet. I have worked with myriad of technologies and platforms in various business domains for early stage startups, large corporations and anything in between. I am an active blogger and open source project owner. I am passionate about technology and green and sustainable living. My technical interest areas are Big Data, Distributed Processing, NOSQL databases, Machine Learning and Programming languages. I am fascinated by problems that don't have neat closed form solution.
This entry was posted in Data Science, Machine Learning, Spark, Statistics and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s