
Thank you and thank Prof. Zivot

Best,
Yihao





From: cmdr_rogue@hotmail.com
To: r-sig-finance@stat.math.ethz.ch
Date: Tue, 19 Oct 2010 06:59:37 -0400
Subject: Re: [R-SIG-Finance] Mean reversion

Yihao,
 
Prof. Zivot is right. The ADF test isn't a great way to test for
mean-reversion; it's merely a way to test for stationarity to specify
the degrees in an ARIMA model.
 
I dug up some code from my misspent youth as a consultant which you
might find useful:
 
StochasticProcessTest <- function(PriceSeries, delta.t, p.value,
diagnostics = TRUE, from = NULL, to = NULL,
                                  by = NULL, currency = '($)'){
  require(stats)
  require(fSeries)
 
  m <- length(PriceSeries)
  S1 <- PriceSeries[1:(m-1)]
  S2 <- PriceSeries[2:m]
 
  #Fit a GBM process:
  Y <- diff(log(PriceSeries)); X <- S1
  GBMfit <- lm(Y ~ X)
 
  #Fit a mean-reverting GBM process:
  Y <- diff(PriceSeries) / S1; X <- log(S1)
  MRGBMfit <- lm(Y ~ X)
 
  #Fit a Vasicek (O-U) process:
  Y <- diff(PriceSeries); X <- S1
  Vfit <- lm(Y ~ X)
 
  #Fit a Cox-Ingersoll-Ross process:
  Y <- diff(PriceSeries) / sqrt(S1); X <- S1 / sqrt(S1)
  CIRfit <- lm(Y ~ X)
 
  #Gather the parameter estimates:
  kappa <- as.vector((c(0, -MRGBMfit$coef[2], -Vfit$coef[2], -CIRfit
$coef[2])) / delta.t)
  mu <- as.vector((c(mean(log(S2/S1)), MRGBMfit$coef[1] / kappa[2], Vfit
$coef[1] / kappa[3],
                     CIRfit$coef[1] / kappa[4])) / delta.t)
  sigma <- (c(sd(GBMfit$resid), sd(MRGBMfit$resid), sd(Vfit$resid),
sd(CIRfit$resid))) / sqrt(delta.t)
  tstat <- as.vector(c(summary(GBMfit)$coef[2,3],
summary(MRGBMfit)$coef[2,3], summary(Vfit)$coef[2,3],
                       summary(CIRfit)$coef[2,3]))
  fstat <- list(summary(GBMfit)$fstatistic,
summary(MRGBMfit)$fstatistic, summary(Vfit)$fstatistic,
                summary(CIRfit)$fstatistic)
  names(fstat) <- c('GBM', 'Mean.Revert.GBM', 'Vasicek', 'CIR')
  AICs <- c(AIC(GBMfit), AIC(MRGBMfit), AIC(Vfit), AIC(CIRfit))
  paramframe <- data.frame(rbind(kappa, mu, sigma, tstat, AICs))
  names(paramframe) <- names(fstat)
 
  #Now figure out what the actual process is, using AIC:
  crit <- ifelse(m > 30, qnorm(1 - p.value), qt(1 - p.value, df = n))
  tmp <- which.min(AICs)
  Processes <- names(fstat)
  Verdict <- Processes[tmp]
  FinalSummary <- switch(Verdict, GBM = summary(GBMfit), CIR =
summary(CIRfit), Mean.Revert.GBM = MRGBMfit,
                         Vasicek = Vfit)
  fitobj <- switch(Verdict, GBM = GBMfit, CIR = CIRfit, Mean.Revert.GBM
= MRGBMfit, Vasicek = Vfit)
  Output <- list(Parameters = paramframe, Critical.Value = crit, Verdict
= Verdict, FinalSummary = FinalSummary,
                 fstat = fstat, fitted.object = fitobj)
 
  if (diagnostics){
    op <- par(ask = TRUE)
    on.exit(op)
 
    if(!all(c(class(from), class(to)) == 'Date')){
      S <- timeSeries(PriceSeries)
    } else S = timeSeries(PriceSeries, seq(from, to, length.out = m))
 
    plot(S, type = 'l', xlab = 'Date', ylab = paste('Price Series',
currency), main = 'Time Series Plot of Data',
         lwd = 2, col = 'blue')
    rets <- returns(PriceSeries, 'continuous')[-1]
    hist(rets, xlab = paste('Log-Returns', currency), col = 'blue',
border = 'white', main = 'Histogram of Return Series',
         freq = FALSE)
    x <- seq(min(rets), max(rets), length.out = max(m, 1000))
    lines(x, dnorm(x, mean(rets), sd(rets)), col = 'magenta', lwd = 2)
    lines(density(rets), col = 'green', lwd = 2)
    legend('topright', legend = c('Log-Returns', 'Observed CDF',
'Gaussian Fit'), lwd = rep(2, 3),
                         col = c('blue', 'green', 'magenta'))
    plot(fitobj, 1:6)
  }
 
  return(Output)
 
}
    
 
(I don't claim that it's necessarily great code, but it does seem to
work.)
 
On Mon, 2010-10-18 at 21:35 -0400, Yihao Lu aeolus_lu wrote:
 
> I am doing rolling ADF test on some time series to check mean reversion. When I use short period rolling, I find the residue is not stationary at all. However, when I use horizon longer than 5 years, I find very significant stationary. On the other hand, I find the half life is only around 30 days.
> Is there anyone who can give me some possible explanation or guide me to some reference? thanks
> 
> Best,
> Yihao

 		 	   		  
	[[alternative HTML version deleted]]

