-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Possible bugs in the calculation of p-values of kstest and ks_2samp #12212
Comments
AFAICS, you never increase the sample size in the first case of the loop, values never change |
@josef-pkt I think there may have been a point, here, but it looks like it has been resolved. Can you check my understanding before we close? @pablogr lists three separate problems, but the first two:
There might have been a bug here in SciPy 1.3, but there doesn't seem to be a problem now. The third problem was that the p-values of the two-sample test were not converging to the values of the continuous test. This no longer seems to be a problem either. I think this script shows that these issues are resolved:
I get:
|
@pvanmulbregt I saw your new PR about |
stats.ks_2samp and stats.mstats.ks_2samp all resolve to the same code so there should be no difference. |
OK, thanks for submitting @pablogr, but we think this has been resolved. Please re-open if you disagree. |
My issue is about the calculation of the p-values using the scipy.stats functions kstest and ks_2samp.
Reproducing code example:
I obtain the output below:
Continuous scipy greater: 0.518857086814109 0.007904829594641602
Continuous scipy less: 0.3531858417269593 0.10657179549539793
Continuous scipy 2-sided: 0.518857086814109 0.015809659189283204
Size: 10
Continuous scipy: 0.518857086814109 0.015809659189283204
; p-value from PGR formula 0.02693690263781148
Discrete scipy1: Ks_2sampResult(statistic=0.7, pvalue=0.012020659079482687)
Discrete scipy2: 0.7 0.025670559472125747
Discrete PGR: 0.7 ; p-value PGR: 0.025670559472125747
Size: 100
Continuous scipy: 0.518857086814109 0.015809659189283204
; p-value from PGR formula 0.02693690263781148
Discrete scipy1: Ks_2sampResult(statistic=0.49, pvalue=0.03797240632133647)
Discrete scipy2: 0.4899999999999997 0.05704510190829596
Discrete PGR: 0.49 ; p-value PGR: 0.05704510190829575
Size: 1000
Continuous scipy: 0.518857086814109 0.015809659189283204
; p-value from PGR formula 0.02693690263781148
Discrete scipy1: Ks_2sampResult(statistic=0.493, pvalue=0.026752661574814707)
Discrete scipy2: 0.4929999999999997 0.0422233090117905
Discrete PGR: 0.493 ; p-value PGR: 0.04222330901179031
Size: 10000
Continuous scipy: 0.518857086814109 0.015809659189283204
; p-value from PGR formula 0.02693690263781148
Discrete scipy1: Ks_2sampResult(statistic=0.5207, pvalue=0.015325026615326087)
Discrete scipy2: 0.5207000000000227 0.026214482841187867
Discrete PGR: 0.5206999999999999 ; p-value PGR: 0.026214482841197786
Size: 100000
Continuous scipy: 0.518857086814109 0.015809659189283204
; p-value from PGR formula 0.02693690263781148
Discrete scipy1: Ks_2sampResult(statistic=0.52012, pvalue=0.026386417174008917)
Discrete scipy2: 0.5201199999997861 0.02638641717410287
Discrete PGR: 0.52012 ; p-value PGR: 0.026386417174008938
Size: 1000000
Continuous scipy: 0.518857086814109 0.015809659189283204
; p-value from PGR formula 0.02693690263781148
Discrete scipy1: Ks_2sampResult(statistic=0.5186660000000001, pvalue=0.027023415027206087)
Discrete scipy2: 0.5186660000026049 0.027023415026037792
Discrete PGR: 0.5186660000000001 ; p-value PGR: 0.027023415027206062
The problems are that:
The p-values do not seem to be consistent with the Smirnov's formula (which are the results above with the text "p-value from PGR formula", which match with the result given for large sizes by ks_twosamp, which appears above as "Discrete scipy2").
The ks_2samp function seems to provide unstable p-values. In the example above (reproduced below) you can see that a tiny variation in the test statistics leads to a large change in the p-value:
(Size: 100: Discrete scipy1: Ks_2sampResult(statistic=0.49, pvalue=0.03797240632133647);
Size: 1000: Discrete scipy1: Ks_2sampResult(statistic=0.493, pvalue=0.026752661574814707))
Size: 10000 Discrete scipy1: Ks_2sampResult(statistic=0.5207, pvalue=0.015325026615326087)
Size: 100000 Discrete scipy1: Ks_2sampResult(statistic=0.52012, pvalue=0.026386417174008917). Moreover, the test statistics monotonically increases with the size, but the p-values decrease and increase.
The results of kstest and ks_2samp are inconsistent with each other. As it can be viewed above:
Continuous scipy: 0.518857086814109 0.015809659189283204
Size: 10000 Discrete scipy1: Ks_2sampResult(statistic=0.5207, pvalue=0.015325026615326087)
Size: 100000 Discrete scipy1: Ks_2sampResult(statistic=0.52012, pvalue=0.026386417174008917)
Scipy/Numpy/Python version information:
Scipy version: 1.3.1
Numpy version: 1.16.5
The text was updated successfully, but these errors were encountered: