# [R-SIG-Finance] Mean reversion tests - with stationary tests andtrending series

Thu Jul 1 10:47:38 CEST 2010

```The adf.test and pp.test test for the general specification of an
intercept and deterministic trend. Since bSeries and cSeries both have
deterministic trends (but no stochastic ones) both series are
trend-stationary (i.e., after removing the deterministic trend they are
mean reverting). bSeries is a special case because seq(1,numElems) has
no variation but in practice this case is probably not relevant. For
your purpose you might want to run a test without deterministic trend.
When looking into the function adf.test removing tt will do that, e.g.

#-----------------------------------------------------------------------
--------------------
adf.test2 <- function (x, alternative = c("stationary", "explosive"), k
= trunc((length(x) -
1)^(1/3)))
{
if (NCOL(x) > 1)
stop("x is not a vector or univariate time series")
if (any(is.na(x)))
stop("NAs in x")
if (k < 0)
stop("k negative")
alternative <- match.arg(alternative)
DNAME <- deparse(substitute(x))
k <- k + 1
y <- diff(x)
n <- length(y)
z <- embed(y, k)
yt <- z[, 1]
xt1 <- x[k:n]
tt <- k:n
if (k > 1) {
yt1 <- z[, 2:k]
res <- lm(yt ~ xt1 + 1 + yt1)
}
else res <- lm(yt ~ xt1 + 1)
res.sum <- summary(res)
STAT <- res.sum\$coefficients[2, 1]/res.sum\$coefficients[2,
2]
table <- cbind(c(4.38, 4.15, 4.04, 3.99, 3.98, 3.96), c(3.95,
3.8, 3.73, 3.69, 3.68, 3.66), c(3.6, 3.5, 3.45, 3.43,
3.42, 3.41), c(3.24, 3.18, 3.15, 3.13, 3.13, 3.12), c(1.14,
1.19, 1.22, 1.23, 1.24, 1.25), c(0.8, 0.87, 0.9, 0.92,
0.93, 0.94), c(0.5, 0.58, 0.62, 0.64, 0.65, 0.66), c(0.15,
0.24, 0.28, 0.31, 0.32, 0.33))
table <- -table
tablen <- dim(table)[2]
tableT <- c(25, 50, 100, 250, 500, 1e+05)
tablep <- c(0.01, 0.025, 0.05, 0.1, 0.9, 0.95, 0.975, 0.99)
tableipl <- numeric(tablen)
for (i in (1:tablen)) tableipl[i] <- approx(tableT, table[,
i], n, rule = 2)\$y
interpol <- approx(tableipl, tablep, STAT, rule = 2)\$y
if (is.na(approx(tableipl, tablep, STAT, rule = 1)\$y))
if (interpol == min(tablep))
warning("p-value smaller than printed p-value")
else warning("p-value greater than printed p-value")
if (alternative == "stationary")
PVAL <- interpol
else if (alternative == "explosive")
PVAL <- 1 - interpol
else stop("irregular alternative")
PARAMETER <- k - 1
METHOD <- "Augmented Dickey-Fuller Test"
names(STAT) <- "Dickey-Fuller"
names(PARAMETER) <- "Lag order"
structure(list(statistic = STAT, parameter = PARAMETER, alternative
= alternative,
p.value = PVAL, method = METHOD, data.name = DNAME),
class = "htest")
}

#-----------------------------------------------------------------------
--------------------

staTest = function(inSeries) {
ht <- adf.test2(inSeries, alternative="stationary")
cat("ADF p-value is", ht\$p.value, "\n")
ht\$p.value
}

ssa <- staTest(aSeries)
ssb <- staTest(bSeries)
ssc <- staTest(cSeries)
ssd <- staTest(dSeries)
cat('STA test A=', ssa, 'B = ', ssb, 'C = ', ssc, 'D =', ssd, '\n')

// Results obtained:
STA test A= 0.01 B =  0.6931798 C =  0.99 D = 0.8778309

Now, only aSeries is found to be stationary.

Zeno

-----Original Message-----
From: r-sig-finance-bounces at stat.math.ethz.ch
[mailto:r-sig-finance-bounces at stat.math.ethz.ch] On Behalf Of
julius_cesarius
Sent: Mittwoch, 30. Juni 2010 19:13
To: r-sig-finance at stat.math.ethz.ch
Subject: [R-SIG-Finance] Mean reversion tests - with stationary tests
andtrending series

I'm having an issue using PP.test or adf.test as stationarity tests. I
am
looking for the right tests to use to see if a series is mean reverting
for
a pairs-trading like strategy.

Basically if the series is trending the tests still say the series is
stationary, but this would not be something I can use in pairs trading.

Short of writing code to detrend and test for trending - what would be
recommended for a test that a series is mean-reverting tradeable.

Example code to illustrate:

aSeries <- sin(seq(1,numElems)/pi)*5
bSeries <- seq(1,numElems)
cSeries <- aSeries*10 + bSeries
dSeries <- cumsum(rnorm(numElems,sd=5))+200

staTest = function(inSeries) {
ht <- adf.test(inSeries, alternative="stationary")
cat("ADF p-value is", ht\$p.value, "\n")
ht\$p.value
}

ssa <- staTest(aSeries)
ssb <- staTest(bSeries)
ssc <- staTest(cSeries)
ssd <- staTest(dSeries)
cat('STA test A=', ssa, 'B = ', ssb, 'C = ', ssc, 'D =', ssd, '\n')

// Results obtained:

STA test A= 0.01 B =  0.99 C =  0.01 D = 0.2764712

Clearly A is the only series that should be stationary and the p test of
0.01 'confirms' this. However C is trending upwards and the P test is
still
saying it's stationary.

I would appreciate any help.
--
View this message in context:
http://r.789695.n4.nabble.com/Mean-reversion-tests-with-stationary-tests
-and-trending-series-tp2272757p2272757.html
Sent from the Rmetrics mailing list archive at Nabble.com.

_______________________________________________
R-SIG-Finance at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions
should go.

EBS European Business School gemeinnuetzige GmbH, Universitaet fuer Wirtschaft und Recht i.Gr. - Amtsgericht Wiesbaden HRB 19951 - Umsatzsteuer-ID DE 113891213 Geschaeftsfuehrung: Prof. Dr. Christopher Jahns,  President; Prof. Dr. Rolf Tilmes, Dean Business School; Sabine Fuchs, CMO; Prof. Dr. Dr. Gerrick Frhr. v. Hoyningen-Huene, Dean Law School; Verwaltungsrat: Dr. Hellmut K. Albrecht, Vorsitzender
```