K-S goodness of fit test in Python

The Kolmogorov-Smirnov test is used to evaluate if histogram distributions deviated from a simulated null hypothesis (H0), comparing to the alternative hypothesis (H1). The distributions with P value < 0.05 are normally considered that deviated significantly from a null hypothesis.

Packages required:

  • SciPy (installation guide)
    SciPy (pronounced “Sigh Pie”) is a Python-based ecosystem of open-source software for mathematics, science, and engineering.

More detailed usage:

One sample

http://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.stats.kstest.html

Two samples

http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.ks_2samp.html
Code:

#!/usr/bin/python
#Performs KS-test
#Usage: python script.py sample1.txt sample2.txt
#Input: distribution values separated by break line in a text file

from scipy import stats
import numpy as np
import sys

x = sys.argv[1]
y = sys.argv[2]

sample1 = np.loadtxt(x,delimiter=",")
sample2 = np.loadtxt(y,delimiter=",")

one_sample = stats.kstest(sample1, 'norm', alternative = 'greater') #null vs alternative hypothesis for sample1. Dont reject equal distribution against alternative hypothesis: greater
two_samples = stats.ks_2samp(sample1, sample2) #sample1 vs sample2

print one_sample,two_samples

#output: sample1 sample2 (D, pvalue) (D, pvalue)
This entry was posted in Local Tools. Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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