Random walk

set.seed(1234)
n=1000
p = rep(0,n)
a = rep(0,n)
p[1] = 0
a[1] = 1
for (t in 2:n){
  a[t] = sample(c(-1,1),size=1)
  p[t] = p[t-1] + a[t]
}

par(mfrow=c(2,2))
ts.plot(p[1:50],ylab="");abline(h=0,lty=2)
ts.plot(p[1:100],ylab="");abline(h=0,lty=2)
ts.plot(p[1:200],ylab="");abline(h=0,lty=2)
ts.plot(p[1:1000],ylab="");abline(h=0,lty=2)

Autocorrelation function

par(mfrow=c(1,1))
acf(p,main="",lag=200)

Random walk with drift

set.seed(1234)
n     = 10000
a     = rnorm(n,0,0.0637)
p     = rep(0,n)
p2    = rep(0,n)
p[1]  = 0
p2[1] = 0
for (t in 2:n){
  p[t]  = 0.0103 + p[t-1] + a[t]
  p2[t] = p2[t-1] + a[t]
}
par(mfrow=c(2,2))
for (t in c(100,500,1000,10000)){
  ts.plot(p[1:t],ylab="",ylim=range(p[1:t],p2[1:t]))
  lines(p2[1:t],col=2)
  abline(h=0,lty=2)
}

Example: US quarterly GDP

Consider the log series of U.S. quarterly GDP from 1947.I to 2008.IV.

The series exhibits an upward trend, showing the growth of the U.S. economy, and has high sample serial correlations.

The first differenced series, representing the growth rate of U.S. GDP, seems to vary around a fixed mean level, even though the variability appears to be smaller in recent years.

With \(p=10\), the ADF test statistic is \(-1.61\) with a p-value \(0.45\), indicating that the unit-root hypothesis cannot be rejected.

library("tseries")
data = "http://faculty.chicagobooth.edu/ruey.tsay/teaching/fts3/q-gdp4708.txt"
da=read.table(data,header=T)
n = nrow(da)
gdp=log(da[,4])

par(mfrow=c(1,2))
plot(da[,1]+da[,2]/12,gdp,xlab="Year",ylab="log-gdp",type="l")
plot(da[2:n,1]+da[2:n,2]/12,diff(gdp),xlab="Year",ylab="diff log-gdp",type="l")

par(mfrow=c(1,2))
acf(gdp,main="log-gdp")
pacf(diff(gdp),main="diff log-gdp")

m1=ar(diff(gdp),method="mle")

adf.test(gdp,k=10)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  gdp
## Dickey-Fuller = -0.37047, Lag order = 10, p-value = 0.9872
## alternative hypothesis: stationary

Example: S&P500 returns

Consider the log series of the SP500 index from January 3, 1950, to April 16, 2008, for 14,462 observations.

Testing for a unit root in the index is relevant if one wishes to verify empirically that the index follows a random walk with drift. To this end, we use \(c_t = \omega_0 + \omega_1 t\) in applying the ADF test.

Furthermore, we choose \(p=15\) based on the sample PACF of the first differenced series.

The resulting test statistic is \(-1.998\) with a p-value of \(0.602\).

Thus, the unit-root hypothesis cannot be rejected at any reasonable significance level.

data = "http://faculty.chicagobooth.edu/ruey.tsay/teaching/fts3/d-sp55008.txt"
da=read.table(data,header=T)
sp5=log(da[,7])

date = da[,1]+(da[,2]*30+da[,3]/31)/366

par(mfrow=c(1,2))
plot(date,da[,7],xlab="Days",ylab="SP500",type="l",main="Log price")
plot(date,sp5,xlab="Days",ylab="SP500",type="l",main="Log returns")

m2=ar(diff(sp5),method="mle")
m2$order
## [1] 2
adf.test(sp5,k=2)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  sp5
## Dickey-Fuller = -2.0179, Lag order = 2, p-value = 0.5708
## alternative hypothesis: stationary