Package: scikit-gof
Provides variants of Kolmogorov-Smirnov, Cramer-von Mises and Anderson-Darling
goodness of fit tests for fully specified continuous distributions.Example>>>fromscipy.statsimportnorm,uniform>>>fromskgofimportks_test,cvm_test,ad_test>>>ks_test((1,2,3),uniform(0,4))GofResult(statistic=0.25,pvalue=0.97...)>>>cvm_test((1,2,3),uniform(0,4))GofResult(statistic=0.04...,pvalue=0.95...)>>>data=norm(0,1).rvs(random_state=1,size=100)>>>ad_test(data,norm(0,1))GofResult(statistic=0.75...,pvalue=0.51...)>>>ad_test(data,norm(.3,1))GofResult(statistic=3.52...,pvalue=0.01...)Simple testsScikit-gof currently only offers three nonparametric tests that let you
compare a sample with a reference probability distribution. These are:ks_test()Kolmogorov-Smirnov supremum statistic; almost the same asscipy.stats.kstest()withalternative='two-sided'but with
(hopefully) somewhat more precise p-value calculation;cvm_test()Cramer-von Mises L2 statistic, with a rather crude estimation of the
statistic distribution (but seemingly the best available);ad_test()Anderson-Darling statistic with a fair approximation of its distribution;
unlike the compositescipy.stats.anderson()this one needs a fully
specified hypothesized distribution.Simple test functions use a common interface, taking as the first argument the
data (sample) to be compared and as the second argument a frozenscipy.statsdistribution.
They return a named tuple with two fields:statisticandpvalue.For a simple example consider the hypothesis that the sample (.4, .1, .7) comes
from the uniform distribution on [0, 1]:ifks_test((.4,.1,.7),unif(0,1)).pvalue<.05:print("Hypothesis rejected with 5% significance.")If your samples are very large and you have them sorted ahead of time, passassume_sorted=Trueto save some time that would be wasted resorting.ExtendingSimple tests are composed of two phases: calculating the test statistic and
determining how likely is the resulting value (under the hypothesis).
New tests may be defined by providing a new statistic calculation routine or an
alternative distribution for a statistic.Functions calculating statistics are given evaluations of the reference
cumulative distribution function on sorted data and are expected to return
a single number.
For a simple test, if the sample indeed comes from the hypothesized (continuous)
distribution, the values passed to the function should be uniformly distributed
over [0, 1].Here is a simplistic example of how a statistic function might look like:defex_stat(data):returnabs(data.sum()-data.size/2)Statistic functions for the provided tests,ks_stat(),cvm_stat(),
andad_stat(), can be imported fromskgof.ecdfgof.Statistic distributions should derive fromrv_continuousand implement
at least one of the abstract_cdf()or_pdf()methods (you might
also consider directly coding_sf()for increased precision of results
close to 1). For example:fromnumpyimportsqrtfromscipy.statsimportnorm,rv_continuousclassex_unif_gen(rv_continuous):def_cdf(self,statistic,samples):return1-2*norm.cdf(-statistic,scale=sqrt(samples/12))ex_unif=ex_unif_gen(a=0,name='ex-unif',shapes='samples')The provided distributions live in separate modules, respectivelyksdist,cvmdist, andaddist.Once you have a statistic calculation function and a statistic distribution the
two parts can be combined usingsimple_test:fromfunctoolsimportpartialfromskgof.ecdfgofimportsimple_testex_test=partial(simple_test,stat=ex_stat,pdist=ex_unif)Exercise: The example test has a fundamental flaw. Can you point it out?Installationpipinstallscikit-gofRequires recent versions of Python (> 3), NumPy (>= 1.10) and SciPy.Please fix or point out any errors, inaccuracies or typos you notice.