It is well known that the distribution of the p-value in a statistical test is $U(0,1)$ under the null. Now, consider the following implementation of the two-samples Kolmogorov Smirnov test on normal samples:
K = 1e4
n = 100
p.val = sapply(1:K,FUN = function(x) ks.test(rnorm(n),rnorm(n),exact=TRUE)$p.value)
mean(p.val<0.05)
plot(p.val,main = 'Plot of p.val')
hist(p.val,freq=FALSE)
p.val is far from being uniformly distributed and takes only discrete values. In addition, the proportion of p.val < 0.05 does not seem to be 0.05, but slightly less, around 0.037 during my testings.
What is going on here? In the 1 sample case, we get the results as expected though, this problems is specific to the two-samples case.
EDIT: The problem lies in the fact that the p-value is not uniformly distributed under null in general. Thanks @whuber for pointing it out.
Let $\boldsymbol{X}^n = (X_1, \dots, X_n)$ be a vector of observation. In general a statistical test at level $\alpha \in (0,1)$ has the form:
\begin{equation} \phi_{\alpha} = 1_{T(\boldsymbol{X}^n) \in \mathcal{R}_{\alpha}} \end{equation} Where $T$ is a real functional of the observations and $\mathcal{R}_{\alpha}$ is the critical region, i.e. $\mathcal{R}_{\alpha} \subset \mathbb{R}$ such that $\mathbb{P}(T(\boldsymbol{X}^n) \in \mathcal{R}_{\alpha}) \leq \alpha$ under the null hypothesis. Now let $P$ be the p-value of the test, i.e. by definition: \begin{equation} P = \inf \{x \in (0,1) | \phi_{x} = 1\}. \end{equation} Then for any $\alpha \in (0,1)$ we have: \begin{align} \mathbb{P}(P \leq \alpha) &= \mathbb{P}(\inf \{x \in (0,1) | \phi_{x} = 1\} \leq \alpha)\\ & = \mathbb{P}(\phi_{\alpha} = 1). \end{align} The last equation is because the two events $\{ \inf \{x \in (0,1) | \phi_{x} = 1\} \leq \alpha \}$ and $\{ \phi_{\alpha} = 1 \}$ are equal. But under the null, the only guarantee is that $\mathbb{P}(\phi_{\alpha} = 1) \leq \alpha$, i.e. that $\mathbb{P}(P \leq \alpha) \leq \alpha$, hence $P$ is not uniform in general.
In order to get uniformly distributed p-values, the familly $\{\phi_{\alpha}|0\leq \alpha \leq 1\}$ must be exactly of level $\alpha$ (in the sense the the type I error must be exactly $\alpha$) the distribution of the test statistic must be absolutely continuous and the null must be simple. Because the 2-samples KS test has discret test statistic, the p-value cannot be uniformly distributed.
K
to 1,000,000 returned the same shaped distribution as the histogram on the right. $\endgroup$plot(ecdf(p.val));abline(0,1,col=2)
to see the connection between the distribution of p-values in the discrete case and a uniform distribution. $\endgroup$