
Great job.
Could you please give some details/explanations on this adf.test2? Thank you.

yihao


Date: Thu, 1 Jul 2010 10:47:38 +0200
From: "Adams, Zeno" <Zeno.Adams@ebs.edu>
To: "julius_cesarius" <maynaaze@yahoo.com>,
	<r-sig-finance@stat.math.ethz.ch>
Subject: Re: [R-SIG-Finance] Mean reversion tests - with stationary
	tests	andtrending series
Message-ID:
	<9064522880125945B98983BBAECBA1CC985791@exchsrv001.ebs.local>
Content-Type: text/plain;	charset="us-ascii"
 
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@stat.math.ethz.ch
[mailto:r-sig-finance-bounces@stat.math.ethz.ch] On Behalf Of
julius_cesarius
Sent: Mittwoch, 30. Juni 2010 19:13
To: r-sig-finance@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. 		 	   		  
	[[alternative HTML version deleted]]

