# Quantile Kolmogorov-Smirnov based on Koenker and Xiao (2004) for a QAR(1)

my goal is to investigate if a process has a unit root at different quantiles, hence, I use a QAR(1) model. I checked the PACF and only the first lag seems to be relevant. All my coding is done in R using the quantreg package by Roger Koenker. The estimation part is no problem, but I am somewhat stuck on how to start with the Quantile Kolmogorov-Smirnov (QKS) test.

I read this http://www.econ.uiuc.edu/~roger/research/qar/Uqar4.pdf especially sections 3.1 - 3.4, where Koenker describes how to do it. I wonder if there is some package implementation or code that I could use as a starting point.

Since I felt that there is a lack of material on quantile autoregression and especially on how to implement testing, I will try to update this post on a continous basis, once I get on with my problem.

require(xts)
require(quantreg)
set.seed(123)
dat <- rnorm(100)
yt  <- xts(dat, order.by = seq(as.Date("2000/1/1"), by = "days", length.out = length(dat)))
dyt <- diff.xts(yt)
dateseq <- seq(as.Date("2000/1/1"), by = "days", length.out = length(dat))

#### Step 1 ####
# fit AR(1) by OLS
ols   <- lm(yt[-1] ~ 0 + dyt[-1])
bhat  <- summary(ols)$coeff[1, 1] uhat <- xts(as.numeric(summary(ols)$resid), order.by = dateseq[-1])

#### Step 2 ####
# draw i.i.d. variables from the centered residuals
n <- length(uhat)
q <- 1
uhatcentered  <- as.numeric(uhat - (1/(n-q)) * sum(uhat[(q+1):n]) )
ustar         <- sample(x = uhatcentered, replace = TRUE, prob = NULL, size = 100)
#### Step 3 ####
# generate the bootstrap sample of dyt recursively using the fitted     autoregression
dytstar <- xts(rep(NA, length(dateseq)), order.by = dateseq)
for(i in 3:length(dytstar)) {
dytstar[2] <- dyt[2]
dytstar[i] <- bhat*dytstar[i-1] + ustar[i]
}
#### Step 4 ####
# obtain a bootstrap sample of ytstar
ytstar <- xts(rep(NA, length(dateseq)), order.by = dateseq)
ytstar[1] <- yt[1]
for(j in 2:length(ytstar)) {
ytstar[j] <- as.numeric(ytstar[j-1]) + as.numeric(dytstar[j])
}


The point I am struggling with at the moment ist the estimation of the nuisance parameters (3.4 in Koenker's paper).

I know that I'm a little late, but here goes. According to Koenker and Xiao's (2004) methodology, we express the quantile Kolmogorov–Smirnov test as $$\text{QKS} = sup_{\tau_i\in T}|t_n(\tau)|$$. The QKS test can be implemented by taking the maximum over T. So you just have to perform the test for each quantile in set T and then take the upper bound. The difficult part is the estimation of the limiting distribution for the QKS since the statistic is non-standard. The authors recommend a re-sampling procedure where we use the empirical distribution of the bootstraps as an approximation for the cumulative distribution of the test statistic.
from quantileADF import bootstraps

and use py_capture_output (or source_python with some modifications) to capture and save the output into a dataframe.