Posted on January 10, 2012 by Vik Paruchuri in Uncategorized | 0 Comments
[This article was first published on R, Ruby, and Finance, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here) Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Cointegration can be a valuable tool in determining the mean reverting properties of 2 time series. A full description of cointegration can be found on Wikipedia. Essentially, it seeks to find stationary linear combinations of the two vectors.
The below R code, which has been modified from here, will test two series for integration and return the p-value indicating the likelihood of correlation. It runs significantly faster than the original code, however. I used this for relatively short time series(50 observations), and while it functioned relatively quickly for small numbers of series, it became cumbersome to use when attempting to serially cointegrate over 100k pairs of bid-ask price series when using it with an mapply function. So scaling up may be an issue.
library(tseries) cointegrationThis runs an augmented Dickey-Fuller test and will return a p-value indicating whether the series are mean-reverting or not. You can use the typical p-value as a test of significance if you like(ie, a p-value below .05 indicates a mean-reverting spread), or you can use an alternate value. This assumes that your two series were observed at the same time points. The original post that this code as modified from contains a further description of cointegration, along with more time series data type handling.
To leave a comment for the author, please follow the link and comment on their blog: R, Ruby, and Finance.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job. Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.