hi: you should look at the unit root chapter in hamilton for a more detailed explanation of what zeno did. there are many differnet<br />cases for what the null of a unit root test is and hamilton lays them out in a beautiful and organized way.<br /><br /><br /><br /><p>On Jul 1, 2010, <strong>Yihao Lu aeolus_lu</strong> <aeolus_lu@hotmail.com> wrote: </p><div class="replyBody"><blockquote style="border-left: 2px solid #267fdb; margin: 0pt 0pt 0pt 1.8ex; padding-left: 1ex"><br />Great job.<br />Could you please give some details/explanations on this adf.test2? Thank you.<br /><br />yihao<br /><br /><br />Date: Thu, 1 Jul 2010 10:47:38 +0200<br />From: "Adams, Zeno" <Zeno.Adams@ebs.edu><br />To: "julius_cesarius" <maynaaze@yahoo.com>,<br /> <r-sig-finance@stat.math.ethz.ch><br />Subject: Re: [R-SIG-Finance] Mean reversion tests - with stationary<br /> tests andtrending series<br />Message-ID:<br /> <9064522880125945B98983BBAECBA1CC985791@exchsrv001.ebs.local><br />Content-Type: text/plain; charset="us-ascii"<br /> <br />The adf.test and pp.test test for the general specification of an<br />intercept and deterministic trend. Since bSeries and cSeries both have<br />deterministic trends (but no stochastic ones) both series are<br />trend-stationary (i.e., after removing the deterministic trend they are<br />mean reverting). bSeries is a special case because seq(1,numElems) has<br />no variation but in practice this case is probably not relevant. For<br />your purpose you might want to run a test without deterministic trend.<br />When looking into the function adf.test removing tt will do that, e.g.<br /> <br /> <br />#-----------------------------------------------------------------------<br />--------------------<br />adf.test2 <- function (x, alternative = c("stationary", "explosive"), k<br />= trunc((length(x) - <br /> 1)^(1/3))) <br />{<br /> if (NCOL(x) > 1) <br /> stop("x is not a vector or univariate time series")<br /> if (any(is.na(x))) <br /> stop("NAs in x")<br /> if (k < 0) <br /> stop("k negative")<br /> alternative <- match.arg(alternative)<br /> DNAME <- deparse(substitute(x))<br /> k <- k + 1<br /> y <- diff(x)<br /> n <- length(y)<br /> z <- embed(y, k)<br /> yt <- z[, 1]<br /> xt1 <- x[k:n]<br /> tt <- k:n<br /> if (k > 1) {<br /> yt1 <- z[, 2:k]<br /> res <- lm(yt ~ xt1 + 1 + yt1)<br /> }<br /> else res <- lm(yt ~ xt1 + 1)<br /> res.sum <- summary(res)<br /> STAT <- res.sum$coefficients[2, 1]/res.sum$coefficients[2, <br /> 2]<br /> table <- cbind(c(4.38, 4.15, 4.04, 3.99, 3.98, 3.96), c(3.95, <br /> 3.8, 3.73, 3.69, 3.68, 3.66), c(3.6, 3.5, 3.45, 3.43, <br /> 3.42, 3.41), c(3.24, 3.18, 3.15, 3.13, 3.13, 3.12), c(1.14, <br /> 1.19, 1.22, 1.23, 1.24, 1.25), c(0.8, 0.87, 0.9, 0.92, <br /> 0.93, 0.94), c(0.5, 0.58, 0.62, 0.64, 0.65, 0.66), c(0.15, <br /> 0.24, 0.28, 0.31, 0.32, 0.33))<br /> table <- -table<br /> tablen <- dim(table)[2]<br /> tableT <- c(25, 50, 100, 250, 500, 1e+05)<br /> tablep <- c(0.01, 0.025, 0.05, 0.1, 0.9, 0.95, 0.975, 0.99)<br /> tableipl <- numeric(tablen)<br /> for (i in (1:tablen)) tableipl[i] <- approx(tableT, table[, <br /> i], n, rule = 2)$y<br /> interpol <- approx(tableipl, tablep, STAT, rule = 2)$y<br /> if (is.na(approx(tableipl, tablep, STAT, rule = 1)$y)) <br /> if (interpol == min(tablep)) <br /> warning("p-value smaller than printed p-value")<br /> else warning("p-value greater than printed p-value")<br /> if (alternative == "stationary") <br /> PVAL <- interpol<br /> else if (alternative == "explosive") <br /> PVAL <- 1 - interpol<br /> else stop("irregular alternative")<br /> PARAMETER <- k - 1<br /> METHOD <- "Augmented Dickey-Fuller Test"<br /> names(STAT) <- "Dickey-Fuller"<br /> names(PARAMETER) <- "Lag order"<br /> structure(list(statistic = STAT, parameter = PARAMETER, alternative<br />= alternative, <br /> p.value = PVAL, method = METHOD, data.name = DNAME), <br /> class = "htest")<br />}<br /> <br />#-----------------------------------------------------------------------<br />--------------------<br /> <br /> <br />staTest = function(inSeries) {<br /> ht <- adf.test2(inSeries, alternative="stationary")<br /> cat("ADF p-value is", ht$p.value, "\n")<br /> ht$p.value<br />}<br /> <br />ssa <- staTest(aSeries)<br />ssb <- staTest(bSeries)<br />ssc <- staTest(cSeries)<br />ssd <- staTest(dSeries)<br />cat('STA test A=', ssa, 'B = ', ssb, 'C = ', ssc, 'D =', ssd, '\n')<br /> <br /> <br />// Results obtained:<br />STA test A= 0.01 B = 0.6931798 C = 0.99 D = 0.8778309<br /> <br />Now, only aSeries is found to be stationary.<br /> <br />Zeno<br /> <br /> <br />-----Original Message-----<br />From: r-sig-finance-bounces@stat.math.ethz.ch<br />[mailto:r-sig-finance-bounces@stat.math.ethz.ch] On Behalf Of<br />julius_cesarius<br />Sent: Mittwoch, 30. Juni 2010 19:13<br />To: r-sig-finance@stat.math.ethz.ch<br />Subject: [R-SIG-Finance] Mean reversion tests - with stationary tests<br />andtrending series<br /> <br /> <br />I'm having an issue using PP.test or adf.test as stationarity tests. I<br />am<br />looking for the right tests to use to see if a series is mean reverting<br />for<br />a pairs-trading like strategy.<br /> <br />Basically if the series is trending the tests still say the series is<br />stationary, but this would not be something I can use in pairs trading.<br /> <br />Short of writing code to detrend and test for trending - what would be<br />recommended for a test that a series is mean-reverting tradeable.<br /> <br />Example code to illustrate:<br /> <br />aSeries <- sin(seq(1,numElems)/pi)*5 <br />bSeries <- seq(1,numElems)<br />cSeries <- aSeries*10 + bSeries<br />dSeries <- cumsum(rnorm(numElems,sd=5))+200<br /> <br /> <br /> <br />staTest = function(inSeries) {<br /> ht <- adf.test(inSeries, alternative="stationary")<br /> cat("ADF p-value is", ht$p.value, "\n")<br /> ht$p.value<br />}<br /> <br />ssa <- staTest(aSeries)<br />ssb <- staTest(bSeries)<br />ssc <- staTest(cSeries)<br />ssd <- staTest(dSeries)<br />cat('STA test A=', ssa, 'B = ', ssb, 'C = ', ssc, 'D =', ssd, '\n')<br /> <br /> <br />// Results obtained:<br /> <br />STA test A= 0.01 B = 0.99 C = 0.01 D = 0.2764712 <br /> <br /> <br />Clearly A is the only series that should be stationary and the p test of<br />0.01 'confirms' this. However C is trending upwards and the P test is<br />still<br />saying it's stationary.<br /> <br />I would appreciate any help. <br /> [[alternative HTML version deleted]]<br /><br />_______________________________________________<br />R-SIG-Finance@stat.math.ethz.ch mailing list<br />https://stat.ethz.ch/mailman/listinfo/r-sig-finance<br />-- Subscriber-posting only. If you want to post, subscribe first.<br />-- Also note that this is not the r-help list where general R questions should go.<br /></blockquote></div>