简体   繁体   中英

How to compare a sample to a self-defined probability distribution using scipy.stats.kstest?

From the scipy documentation on scipy.stats.kstest, it seems that the function only allows a comparison between a sample and a pre-defined probability distribution. Can it compare between a sample and a self-defined probability distribution?

I could use the two-sample Kolmogorov-Smirnov test, scipy.stats.ks_2samp, to compare a theoretical sample generated from the self-defined function to the actual sample.

I tried the following code:

from scipy.stats import kstest
sample = [1 for i in range(10)]
ks_stat, p_value = kstest(sample, lambda x: 1)
print ks_stat, p_value
>> 1.0, 0.0

The p_value above should give 1 as the sample matches exactly to the distribution.

Links for convenience

One sample KS test: https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.kstest.html

Two sample KS test: https://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.stats.ks_2samp.html

The KS test is only applicable to continuous distributions. One property of continuous distributions is that samples cannot have repeats.

With a sounder example the test works as expected:

import numpy as np
from scipy.stats import kstest
sample = [i for i in range(10)]
ks_stat, p_value = kstest(sample, lambda x: np.clip(0.1*(x+0.5),0,1))
print ks_stat, p_value

Prints:

0.05 1.0

Oops, @Niklas sorry only just now read your comment properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM