4

I have some data that I have to test to see if it comes from a Weibull distribution with unknown parameters. In R I could use https://cran.r-project.org/web/packages/KScorrect/index.html but I can't find anything in Python.

Using scipy.stats I can fit parameters with:

scipy.stats.weibull_min.fit(values)

However in order to turn this into a test I think I need to perform some Monte-Carlo simulation (e.g. https://en.m.wikipedia.org/wiki/Lilliefors_test) I am not sure what to do exactly.

How can I make such a test in Python?

Michael Baudin
  • 1,022
  • 10
  • 25
Simd
  • 19,447
  • 42
  • 136
  • 271

2 Answers2

4

The Lilliefors test is implemented in OpenTURNS. To do this, all you have to use the Factory which corresponds to the distribution you want to fit. In the following script, I simulate a Weibull sample with size 10 and perform the Kolmogorov-Smirnov test using a sample size equal to 1000. This means that the KS statistics is simulated 1000 times.

import openturns as ot
sample=ot.WeibullMin().getSample(10)
ot.ResourceMap.SetAsUnsignedInteger("FittingTest-KolmogorovSamplingSize",1000)
distributionFactory = ot.WeibullMinFactory()
dist, result = ot.FittingTest.Kolmogorov(sample, distributionFactory, 0.01)
print('Conclusion=', result.getBinaryQualityMeasure())
print('P-value=', result.getPValue())

More details can be found at:

Michael Baudin
  • 1,022
  • 10
  • 25
-1

One way around: estimate distribution parameters, draw data from the estimated distribution and run KS test to check that both samples come from the same distribution.

Let's create some "original" data:

>>> values = scipy.stats.weibull_min.rvs( 0.33, size=1000)

Now,

>>> args = scipy.stats.weibull_min.fit(values)
>>> print(args)
(0.32176317627928856, 1.249788665927261e-09, 0.9268793667654682)

>>> scipy.stats.kstest(values, 'weibull_min', args=args, N=100000)

KstestResult(statistic=0.033808945722737016, pvalue=0.19877935361964738)

The last line is equivalent to:

 scipy.stats.ks_2samp(values, scipy.stats.weibull_min.rvs(*args, size=100000))

So, once you estimate parameters of the distribution, you can test it pretty reliably. But the scipy estimator is not very good, it took me several runs to get even "close" to the original distribution.

igrinis
  • 12,398
  • 20
  • 45
  • I don't think this works. The reason you need the Lillefors test is that when the parameters are estimated the critical values from the KS test will be too conservative. See eg https://influentialpoints.com/Training/lilliefors_test.htm So the main task seems to be to estimate these critical values by simulation somehow. – Simd Aug 27 '19 at 12:51