Building KS table

50 Views Asked by At

Stimulated by the curiosity, I wanted to simulate a Kolmogorov-Smirnov table. Initially, I thought it is something that can be done easily in R, but obviously I am missing something. The critical value I am getting for $\alpha=0.05$ with $n=10$ trials is $\approx1.3$ instead of $\approx0.4$. I know there is something wrong because quantile should decrease with an increase of $n$, which does not happen in my case... so I wonder if I am missing (or misusing) some scaling factor, like $\sqrt n$. Any pointers are appreciated!

rm(list=ls())
n<-100      # number of trials
k<-1000000  # number of simulations
t<-0:n/n    # points between 0 and 1

# generate k brownian motions (BM), each with n steps
dB<-matrix(rnorm(n*k)/sqrt(n),nrow=k)
B<-cbind(rep(0,k),t(apply(dB,1,cumsum)))

# generate brownian bridges by forcing each BM to return to 0 at 1
B<-B-matrix(B[,n+1],ncol=1)%*%matrix(t,nrow=1)

# select maximum of each
supAbsB<-ecdf(apply(abs(B),1,max))
quantile(supAbsB,probs=.95)
1

There are 1 best solutions below

0
On

Based on a suggestion received on Reddit (thanks u/efrique), I tried to simulate a bunch of uniforms and compare the resulting sample CDF with the reference CDF, and apparently it works, as the critical value I am getting for $\alpha=0.05$ and $n=10$ is pretty close to $\approx0.4$.

Although this method seems to work, with any possible distribution (for practicality I used uniform(0,1)), I still think there is room for improvement if I could use a pivotal distribution (that is the Brownian bridge)... so my original question is still unanswered.

rm(list=ls())
n<-10    # number of trials
k<-1e6   # number of simulations
t<-0:n/n # intervals between 0 and 1 (+1)

# generate k uniform r.v., each with n steps
x<-matrix(runif(n*k),nrow=k)

# compute sample ranks, per each path
y<-t(apply(x,1,rank))

# compute the max, per each path
m<-apply(pmax(abs(x-(y-1)/n),abs(y/n-x)),1,max)
quantile(m,probs=.95)