'Kolmogorov-Smirnov test for uniformity giving unexpected results
I'm trying to check if my random variables are from uniform distribution or not. In my case, though, I have arrays like [0, 0, 0.44, 0, ... 0] that I need to check. This vector looks somewhat like uniform if you ask me, but KS-test doesn't seem to agree. Here's what I do:
from scipy.stats import kstest, uniform
x = np.zeros((1,30))[0]
x[3] = 0.44 # created an array that i'm talking about
ks_statistic, p_value = kstest(x, 'uniform')
print(ks_statistic, p_value)
And here's what I get:
0.9666666666666667 9.713871499237658e-45
So the verdict is 'not uniform'. As far as I got the math underneath the KS-test, there is a possibility that my all-zeros arrays are far from what stats.uniform will generate to compare, and therefore the distance between the distributions is huge. But there is also a huge chance that I've got something wrong.
What do I do in order to test my variables for uniformity correctly?
Update: I checked out the Chi-square test and I got something a lot more like what I expected.
from scipy.stats import chisquare
stat, p_value = chisquare(x)
print(stat, p_value)
>>> 12.760000000000002 0.9960743669884126
So my question is why are these results so different and what am I missing?
Solution 1:[1]
I see where I'm wrong, that was easy.
What I claimed to look like uniformly distributed variables, looks exactly nothing like it. The whole point of uniform distribution is that you will most definitely never see two equivalent samples from it. And I have zeros, zeros, zeros all over the place.
I still don't get the difference between the p-value for KS and for Chi^2 though. But that's probably another question.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Olya Agapova |
