# HypothesisTests, rand and very low pvalue

Consider the following code:

using Distributions
import HypothesisTests as HT
HT.pvalue(HT.ApproximateOneSampleKSTest(rand(Normal(), 1_000_000), Normal()))

I expected that the answer will be very close to one, but depending on the run it can be anywhere from 0.01 to 0.98. Why it is so? The same is with OneSampleADTest and other tests.

Using DiscreteUniform is even worse.

routinely gives values like 6e-10.

So what is wrong here?

I donβt think anything is wrong: when the null hypothesis is true, the p-value is uniformly distributed. So you have in particular 1% chance of observing a p-value <= 0.01, and 2% of observing something >= 98%.

For example, repeating your experiment 10000 times (with a much smaller sample size to avoid long computations, but this doesnβt change the result):

using UnicodePlots
using Distributions
import HypothesisTests as HT

get_p() = HT.pvalue(HT.ApproximateOneSampleKSTest(rand(Normal(), 1000), Normal()))
p = [get_p() for _ in 1:10_000]

histogram(p)

# Output:
β                                        β
[0.0, 0.1) β€ββββββββββββββββββββββββββββββ 956
[0.1, 0.2) β€ββββββββββββββββββββββββββββββββ 1 025
[0.2, 0.3) β€ββββββββββββββββββββββββββββββ 977
[0.3, 0.4) β€βββββββββββββββββββββββββββββββ 978
[0.4, 0.5) β€βββββββββββββββββββββββββββββ 943
[0.5, 0.6) β€ββββββββββββββββββββββββββββββββ 1 025
[0.6, 0.7) β€βββββββββββββββββββββββββββββββ 979
[0.7, 0.8) β€βββββββββββββββββββββββββββββββ 1 002
[0.8, 0.9) β€ββββββββββββββββββββββββββββββββ 1 040
[0.9, 1.0) β€βββββββββββββββββββββββββββββββββ  1 075
β                                        β

The histogram is approximtely flat as expected, because all values of the p-value are equally likely.

4 Likes