Normal Distribution Fitness Test with Chi Square on Spark

Many Machine Learning models is based on certain assumptions made about the data. For example, in ZScore based  anomaly detection, it is  assumed that the data has normal distribution. Your Machine Learning model will be as good as how those assumptions hold true. In this post, we will go over a Spark based implementation of Chi Square test for the assumptions of some distribution of the data set

The implementation  is available in my open source chombo on github. Like my all other Spark projects,  the implementation is met data driven and completely decoupled from any specific data set. Our use case will involve validation of normal distribution assumption of the data.

Chi Square Fitness Test

To test any hypothesis about data, a statistic is calculated from the data set. Using the critical value table of the statistic, we check whether the statistic falls with the rejection regions. If so, the null hypothesis is rejected  between the

Along the same line here are the steps for Chi Square fitness testChi Square statistic is based based on squared difference  between the expected count and observed count in each cell.

  1. We hypothesize about the data, stating the null and alternative hypothesis. In our use case null hypothesis is that data distribution is normal.
  2. We compute the frequencies of count of expected distribution and also the frequencies of count of observed data.
  3. For each cell, Take the square of difference beteen expected and observed prequencies and divide by the excepted frequency. Sum them up for all cells. This is Chi Squere statistic.
  4. We check the statistic value falls within the  rejection region for some chosen critical factor, we reject the null hypothesis

Next, we will discuss how all these come in to play for a real life use case with customer transaction data.

We will build a non parametric distribution of the data. The non parametric distribution i.e histogram  along with normal distribution will be used for the Chi Square fitness test.

eCommerce Customer Transaction Data

A smart group of Data Scientist in a company that successfully built and eCommerce operation, built a customer segmentation model,  to enable matketing to do more targeted marketing. After running kNN clustering algorithms multiple times,  the optimum number of clusters is 4.

Another group of Data Scientist that have thinking about building a fraud detection system. They though they could leverage the cluster already built and train an anomaly detection model for each cluster or segment.They decided to use the ZScore algorithm.  They might have used the Spark based implementation of ZScore based anomaly detection in beymani or something else.

After deploying the anomaly detection solution, they found that too many false alarms were being generated. They decided to investigate the validity of the normal distribution assumption of the data. This brings us back to the main topic of this post. Here is some sample input data


The fields are cluster ID, customer ID, transaction ID, transaction time and monetary amount.

Preparatory Steps

Since mean and standard deviation of the normal distribution are not known, they are estimated from samples. This is done through the Spark job  NumericalAttrStats, details of which are available..  Here is the output


The first 2 fields identify the key. The remaining fields are various outputs.  For the non paramteric distribution, we need to define bin or bucket width. Chi Square fitness requires at least 5 samples in each bucket.

The last 2 fields  in each line of input are min and max. We use a python script that uses these fields  to estimate bucket width. The script takes the average number of samples per bucket as one of the inputs. Here is the out put of the script. The last field is the bucket width.


Spark Job for Histogram and Fitness Test

The Spark job NumericalAttrDistrStats essentially  builds histogram. Additionally, depending on how it’s configured, it can do Chi Square of other fitness test. Here is some sample output.


The first 2 fields are key, the first field being the cluster ID. The remaining fields are related to histogram, until the last  3 fields.

The 3rd field from the end is calculated Chi Square statistic. The next field is is critical value Chi Square statistic. The last field is true when the calculated Chi Square statistic is less than the critical value Chi Square statistic, implying that null hypothesis is accepted.

We fins that cluster with ID 2, violates the assumption of normal distribution somewhat since the Chi Square statistic is marginally into the rejection region.  The cluster with ID 1,  violates the assumption of normal distribution significantly since the Chi Square statistic is well over into the rejection region.

The conclusion that the Data Science reaches based on these finding is ZScore is not the appropriate algorithm.

Summing Up

Many Machine Learning models are based on certain assumptions about the data. It’s important to verify the validity of such assumptions before proceeding. In this post, we have gone through the steps on how to check normal distribution fitness with Chi Square test. The tutorial document provides the details of the teps.


For commercial support for any solution in my github repositories, please talk to ThirdEye Data Science Services. Support is available for Hadoop or Spark deployment on cloud including installation, configuration and testing.


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 Anomaly Detection, Big Data, Data Science, 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